Created
July 21, 2024 20:46
-
-
Save nickfrates/3be9fc4df4afcd38dfc3bd8d663a5f3c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ConvertLeadApex { | |
| @InvocableMethod(label='Convert Lead' description='Converts a lead into an account, a contact, and optionally an opportunity.') | |
| public static List<LeadConversionResult> convertLead(List<LeadConversionRequest> requests) { | |
| List<LeadConversionResult> results = new List<LeadConversionResult>(); | |
| for (LeadConversionRequest request : requests) { | |
| LeadConversionResult result = new LeadConversionResult(); | |
| try { | |
| // Set the default handling for booleans. | |
| if (request.overWriteLeadSource == null) request.overWriteLeadSource = false; | |
| if (request.createOpportunity == null) request.createOpportunity = true; | |
| if (request.sendEmailToOwner == null) request.sendEmailToOwner = false; | |
| // Convert the lead by passing it to a helper method. | |
| Map<String,String> conversionResult = convertLead(request.leadID, request.contactID, request.accountID, | |
| request.convertedStatus, request.overWriteLeadSource, request.createOpportunity, | |
| request.opportunityName, request.sendEmailToOwner); | |
| result.accountID = conversionResult.get('AccountID'); | |
| result.contactID = conversionResult.get('ContactID'); | |
| result.opportunityID = conversionResult.get('OpportunityID'); | |
| } catch (Exception e) { | |
| result.errorMessage = e.getMessage(); | |
| } | |
| results.add(result); | |
| } | |
| return results; | |
| } | |
| public static Map<String,String> convertLead ( | |
| String leadID, | |
| String contactID, | |
| String accountID, | |
| String convertedStatus, | |
| Boolean overWriteLeadSource, | |
| Boolean createOpportunity, | |
| String opportunityName, | |
| Boolean sendEmailToOwner | |
| ) { | |
| Map<String,String> result = new Map<String,String>(); | |
| if (leadId == null) throw new ConvertLeadPluginException('Lead Id cannot be null'); | |
| // Check for multiple leads with the same ID | |
| Lead[] leads = [Select Id, FirstName, LastName, Company From Lead where Id = :leadID]; | |
| if (leads.size() > 0) { | |
| Lead l = leads[0]; | |
| // CheckAccount = true, checkContact = false | |
| if (accountID == null && l.Company != null) { | |
| Account[] accounts = [Select Id, Name FROM Account where Name = :l.Company LIMIT 1]; | |
| if (accounts.size() > 0) { | |
| accountId = accounts[0].id; | |
| } | |
| } | |
| // Perform the lead conversion. | |
| Database.LeadConvert lc = new Database.LeadConvert(); | |
| lc.setLeadId(leadID); | |
| lc.setOverwriteLeadSource(overWriteLeadSource); | |
| lc.setDoNotCreateOpportunity(!createOpportunity); | |
| lc.setConvertedStatus(convertedStatus); | |
| if (sendEmailToOwner != null) lc.setSendNotificationEmail(sendEmailToOwner); | |
| if (accountId != null && accountId.length() > 0) lc.setAccountId(accountId); | |
| if (contactId != null && contactId.length() > 0) lc.setContactId(contactId); | |
| if (createOpportunity) { | |
| lc.setOpportunityName(opportunityName); | |
| } | |
| Database.LeadConvertResult lcr = Database.convertLead(lc, true); | |
| if (lcr.isSuccess()) { | |
| result.put('AccountID', lcr.getAccountId()); | |
| result.put('ContactID', lcr.getContactId()); | |
| if (createOpportunity) { | |
| result.put('OpportunityID', lcr.getOpportunityId()); | |
| } | |
| } else { | |
| String error = lcr.getErrors()[0].getMessage(); | |
| throw new ConvertLeadPluginException(error); | |
| } | |
| } else { | |
| throw new ConvertLeadPluginException('No leads found with Id : "' + leadId + '"'); | |
| } | |
| return result; | |
| } | |
| public class LeadConversionRequest { | |
| @InvocableVariable(required=true) | |
| public String leadID; | |
| @InvocableVariable | |
| public String contactID; | |
| @InvocableVariable | |
| public String accountID; | |
| @InvocableVariable(required=true) | |
| public String convertedStatus; | |
| @InvocableVariable | |
| public Boolean overWriteLeadSource; | |
| @InvocableVariable | |
| public Boolean createOpportunity; | |
| @InvocableVariable | |
| public String opportunityName; | |
| @InvocableVariable | |
| public Boolean sendEmailToOwner; | |
| } | |
| public class LeadConversionResult { | |
| @InvocableVariable | |
| public String accountID; | |
| @InvocableVariable | |
| public String contactID; | |
| @InvocableVariable | |
| public String opportunityID; | |
| @InvocableVariable | |
| public String errorMessage; | |
| } | |
| // Utility exception class | |
| public class ConvertLeadPluginException extends Exception {} | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @isTest | |
| public class ConvertLeadApexTest { | |
| @isTest | |
| static void testConvertLead() { | |
| // Create a test lead | |
| Lead testLead = new Lead( | |
| LastName = 'TestLastName', | |
| Company = 'TestCompany', | |
| Status = 'Open - Not Contacted' | |
| ); | |
| insert testLead; | |
| // Create a test account | |
| Account testAccount = new Account( | |
| Name = 'TestCompany' | |
| ); | |
| insert testAccount; | |
| // Set up the request parameters | |
| ConvertLeadApex.LeadConversionRequest request = new ConvertLeadApex.LeadConversionRequest(); | |
| request.leadID = testLead.Id; | |
| request.accountID = testAccount.Id; | |
| request.convertedStatus = 'Closed - Converted'; | |
| request.overWriteLeadSource = true; | |
| request.createOpportunity = true; | |
| request.opportunityName = 'Test Opportunity'; | |
| request.sendEmailToOwner = false; | |
| // Add the request to a list | |
| List<ConvertLeadApex.LeadConversionRequest> requests = new List<ConvertLeadApex.LeadConversionRequest>{ request }; | |
| // Invoke the method | |
| Test.startTest(); | |
| List<ConvertLeadApex.LeadConversionResult> results = ConvertLeadApex.convertLead(requests); | |
| Test.stopTest(); | |
| // Verify the results | |
| System.assertEquals(1, results.size()); | |
| ConvertLeadApex.LeadConversionResult result = results[0]; | |
| System.assertNotEquals(null, result.accountID); | |
| System.assertNotEquals(null, result.contactID); | |
| System.assertNotEquals(null, result.opportunityID); | |
| System.assertEquals(null, result.errorMessage); | |
| // Verify that the lead was converted | |
| Lead convertedLead = [SELECT Id, IsConverted, ConvertedAccountId, ConvertedContactId, ConvertedOpportunityId FROM Lead WHERE Id = :testLead.Id]; | |
| System.assert(convertedLead.IsConverted); | |
| System.assertEquals(testAccount.Id, convertedLead.ConvertedAccountId); | |
| System.assertNotEquals(null, convertedLead.ConvertedContactId); | |
| System.assertNotEquals(null, convertedLead.ConvertedOpportunityId); | |
| // Verify that the contact was created | |
| Contact newContact = [SELECT Id, LastName, AccountId FROM Contact WHERE Id = :convertedLead.ConvertedContactId]; | |
| System.assertEquals(testLead.LastName, newContact.LastName); | |
| System.assertEquals(testAccount.Id, newContact.AccountId); | |
| // Verify that the opportunity was created | |
| Opportunity newOpportunity = [SELECT Id, Name, AccountId FROM Opportunity WHERE Id = :convertedLead.ConvertedOpportunityId]; | |
| System.assertEquals(request.opportunityName, newOpportunity.Name); | |
| System.assertEquals(testAccount.Id, newOpportunity.AccountId); | |
| } | |
| @isTest | |
| static void testConvertLeadWithoutOpportunity() { | |
| // Create a test lead | |
| Lead testLead = new Lead( | |
| LastName = 'TestLastName', | |
| Company = 'TestCompany', | |
| Status = 'Open - Not Contacted' | |
| ); | |
| insert testLead; | |
| // Set up the request parameters | |
| ConvertLeadApex.LeadConversionRequest request = new ConvertLeadApex.LeadConversionRequest(); | |
| request.leadID = testLead.Id; | |
| request.convertedStatus = 'Closed - Converted'; | |
| request.overWriteLeadSource = false; | |
| request.createOpportunity = false; | |
| request.sendEmailToOwner = false; | |
| // Add the request to a list | |
| List<ConvertLeadApex.LeadConversionRequest> requests = new List<ConvertLeadApex.LeadConversionRequest>{ request }; | |
| // Invoke the method | |
| Test.startTest(); | |
| List<ConvertLeadApex.LeadConversionResult> results = ConvertLeadApex.convertLead(requests); | |
| Test.stopTest(); | |
| // Verify the results | |
| System.assertEquals(1, results.size()); | |
| ConvertLeadApex.LeadConversionResult result = results[0]; | |
| System.assertNotEquals(null, result.accountID); | |
| System.assertNotEquals(null, result.contactID); | |
| System.assertEquals(null, result.opportunityID); | |
| System.assertEquals(null, result.errorMessage); | |
| // Verify that the lead was converted | |
| Lead convertedLead = [SELECT Id, IsConverted, ConvertedAccountId, ConvertedContactId, ConvertedOpportunityId FROM Lead WHERE Id = :testLead.Id]; | |
| System.assert(convertedLead.IsConverted); | |
| System.assertNotEquals(null, convertedLead.ConvertedAccountId); | |
| System.assertNotEquals(null, convertedLead.ConvertedContactId); | |
| System.assertEquals(null, convertedLead.ConvertedOpportunityId); | |
| } | |
| @isTest | |
| static void testConvertLeadWithErrors() { | |
| // Set up the request parameters | |
| ConvertLeadApex.LeadConversionRequest request = new ConvertLeadApex.LeadConversionRequest(); | |
| request.leadID = null; // Invalid Lead ID | |
| request.convertedStatus = 'Closed - Converted'; | |
| // Add the request to a list | |
| List<ConvertLeadApex.LeadConversionRequest> requests = new List<ConvertLeadApex.LeadConversionRequest>{ request }; | |
| // Invoke the method | |
| Test.startTest(); | |
| List<ConvertLeadApex.LeadConversionResult> results = ConvertLeadApex.convertLead(requests); | |
| Test.stopTest(); | |
| // Verify the results | |
| System.assertEquals(1, results.size()); | |
| ConvertLeadApex.LeadConversionResult result = results[0]; | |
| System.assertEquals(null, result.accountID); | |
| System.assertEquals(null, result.contactID); | |
| System.assertEquals(null, result.opportunityID); | |
| System.assertNotEquals(null, result.errorMessage); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment