-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
global class ConvertLeadAction { | ||
@InvocableMethod(label='Convert Leads') | ||
global static List<ConvertLeadActionResult> convertLeads(List<ConvertLeadActionRequest> requests) { | ||
List<ConvertLeadActionResult> results = new List<ConvertLeadActionResult>(); | ||
for (ConvertLeadActionRequest request : requests) { | ||
results.add(convertLead(request)); | ||
} | ||
return results; | ||
} | ||
public static ConvertLeadActionResult convertLead(ConvertLeadActionRequest request) { | ||
Database.LeadConvert lc = new Database.LeadConvert(); | ||
lc.setLeadId(request.leadId); | ||
lc.setConvertedStatus(request.convertedStatus); | ||
if (request.accountId != null) { | ||
lc.setAccountId(request.accountId); | ||
} | ||
if (request.contactId != null) { | ||
lc.setContactId(request.contactId); | ||
} | ||
if (request.overWriteLeadSource != null && request.overWriteLeadSource) { | ||
lc.setOverwriteLeadSource(request.overWriteLeadSource); | ||
} | ||
if (request.createOpportunity != null && !request.createOpportunity) { | ||
lc.setDoNotCreateOpportunity(!request.createOpportunity); | ||
} | ||
if (request.opportunityName != null) { | ||
lc.setOpportunityName(request.opportunityName); | ||
} | ||
if (request.ownerId != null) { | ||
lc.setOwnerId(request.ownerId); | ||
} | ||
if (request.sendEmailToOwner != null && request.sendEmailToOwner) { | ||
lc.setSendNotificationEmail(request.sendEmailToOwner); | ||
} | ||
Database.LeadConvertResult lcr = Database.convertLead(lc, true); | ||
if (lcr.isSuccess()) { | ||
ConvertLeadActionResult result = new ConvertLeadActionResult(); | ||
result.accountId = lcr.getAccountId(); | ||
result.contactId = lcr.getContactId(); | ||
result.opportunityId = lcr.getOpportunityId(); | ||
return result; | ||
} else { | ||
throw new ConvertLeadActionException(lcr.getErrors()[0].getMessage()); | ||
} | ||
} | ||
global class ConvertLeadActionRequest { | ||
@InvocableVariable(required=true) | ||
global ID leadId; | ||
@InvocableVariable(required=true) | ||
global String convertedStatus; | ||
@InvocableVariable | ||
global ID accountId; | ||
@InvocableVariable | ||
global ID contactId; | ||
@InvocableVariable | ||
global Boolean overWriteLeadSource; | ||
@InvocableVariable | ||
global Boolean createOpportunity; | ||
@InvocableVariable | ||
global String opportunityName; | ||
@InvocableVariable | ||
global ID ownerId; | ||
@InvocableVariable | ||
global Boolean sendEmailToOwner; | ||
} | ||
global class ConvertLeadActionResult { | ||
@InvocableVariable | ||
global ID accountId; | ||
@InvocableVariable | ||
global ID contactId; | ||
@InvocableVariable | ||
global ID opportunityId; | ||
} | ||
public class ConvertLeadActionException extends Exception {} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>59.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
@isTest | ||
public class ConvertLeadActionTest { | ||
@isTest static void testConvertLeads() { | ||
// Create a Lead | ||
Lead testLead = new Lead(LastName='Test Lead', Company='Test Company'); | ||
insert testLead; | ||
|
||
// Create a ConvertLeadActionRequest | ||
ConvertLeadAction.ConvertLeadActionRequest request = new ConvertLeadAction.ConvertLeadActionRequest(); | ||
request.leadId = testLead.Id; | ||
request.convertedStatus = 'Closed - Converted'; | ||
|
||
// Call the ConvertLeadAction method | ||
List<ConvertLeadAction.ConvertLeadActionRequest> requests = new List<ConvertLeadAction.ConvertLeadActionRequest>(); | ||
requests.add(request); | ||
List<ConvertLeadAction.ConvertLeadActionResult> results = ConvertLeadAction.convertLeads(requests); | ||
|
||
// Verify the Lead was converted | ||
System.assertEquals(1, results.size(), 'Expected one result'); | ||
ConvertLeadAction.ConvertLeadActionResult result = results[0]; | ||
System.assertNotEquals(null, result.accountId, 'Expected an Account ID'); | ||
System.assertNotEquals(null, result.contactId, 'Expected a Contact ID'); | ||
System.assertEquals(null, result.opportunityId, 'Did not expect an Opportunity ID'); | ||
|
||
// Verify the Lead is converted | ||
testLead = [SELECT IsConverted FROM Lead WHERE Id = :testLead.Id]; | ||
System.assertEquals(true, testLead.IsConverted, 'Expected the Lead to be converted'); | ||
} | ||
|
||
@isTest static void testConvertLeadWithInvalidLeadId() { | ||
// Create a ConvertLeadActionRequest with an invalid leadId | ||
ConvertLeadAction.ConvertLeadActionRequest request = new ConvertLeadAction.ConvertLeadActionRequest(); | ||
request.leadId = 'invalidLeadId'; | ||
request.convertedStatus = 'Closed - Converted'; | ||
|
||
// Call the ConvertLeadAction method | ||
List<ConvertLeadAction.ConvertLeadActionRequest> requests = new List<ConvertLeadAction.ConvertLeadActionRequest>(); | ||
requests.add(request); | ||
try { | ||
ConvertLeadAction.convertLeads(requests); | ||
System.assert(false, 'Expected an exception to be thrown'); | ||
} catch (ConvertLeadAction.ConvertLeadActionException ex) { | ||
System.assertEquals('Invalid leadId', ex.getMessage(), 'Expected a specific exception message'); | ||
} | ||
} | ||
|
||
@isTest static void testConvertLeadWithAlreadyConvertedLead() { | ||
// Create a Lead and convert it | ||
Lead testLead = new Lead(LastName='Test Lead', Company='Test Company'); | ||
insert testLead; | ||
Database.LeadConvert lc = new Database.LeadConvert(); | ||
lc.setLeadId(testLead.Id); | ||
lc.setConvertedStatus('Closed - Converted'); | ||
Database.LeadConvertResult lcr = Database.convertLead(lc); | ||
System.assert(lcr.isSuccess(), 'Expected the Lead to be successfully converted'); | ||
|
||
// Create a ConvertLeadActionRequest for the already converted Lead | ||
ConvertLeadAction.ConvertLeadActionRequest request = new ConvertLeadAction.ConvertLeadActionRequest(); | ||
request.leadId = testLead.Id; | ||
request.convertedStatus = 'Closed - Converted'; | ||
|
||
// Call the ConvertLeadAction method | ||
List<ConvertLeadAction.ConvertLeadActionRequest> requests = new List<ConvertLeadAction.ConvertLeadActionRequest>(); | ||
requests.add(request); | ||
try { | ||
ConvertLeadAction.convertLeads(requests); | ||
System.assert(false, 'Expected an exception to be thrown'); | ||
} catch (ConvertLeadAction.ConvertLeadActionException ex) { | ||
System.assertEquals('Lead has already been converted', ex.getMessage(), 'Expected a specific exception message'); | ||
} | ||
} | ||
|
||
@isTest static void testConvertLeadWithInvalidConvertedStatus() { | ||
// Create a Lead | ||
Lead testLead = new Lead(LastName='Test Lead', Company='Test Company'); | ||
insert testLead; | ||
|
||
// Create a ConvertLeadActionRequest with an invalid convertedStatus | ||
ConvertLeadAction.ConvertLeadActionRequest request = new ConvertLeadAction.ConvertLeadActionRequest(); | ||
request.leadId = testLead.Id; | ||
request.convertedStatus = 'Invalid Status'; | ||
|
||
// Call the ConvertLeadAction method | ||
List<ConvertLeadAction.ConvertLeadActionRequest> requests = new List<ConvertLeadAction.ConvertLeadActionRequest>(); | ||
requests.add(request); | ||
try { | ||
ConvertLeadAction.convertLeads(requests); | ||
System.assert(false, 'Expected an exception to be thrown'); | ||
} catch (ConvertLeadAction.ConvertLeadActionException ex) { | ||
System.assertEquals('Invalid convertedStatus', ex.getMessage(), 'Expected a specific exception message'); | ||
} | ||
} | ||
|
||
@isTest static void testConvertLeadsEmptyRequestList() { | ||
// Call the ConvertLeadAction method with an empty list of requests | ||
List<ConvertLeadAction.ConvertLeadActionRequest> requests = new List<ConvertLeadAction.ConvertLeadActionRequest>(); | ||
List<ConvertLeadAction.ConvertLeadActionResult> results = ConvertLeadAction.convertLeads(requests); | ||
|
||
// Verify no results are returned | ||
System.assertEquals(0, results.size(), 'Expected no results'); | ||
} | ||
|
||
@isTest static void testConvertLeadWithOpportunityName() { | ||
// Create a Lead | ||
Lead testLead = new Lead(LastName='Test Lead', Company='Test Company'); | ||
insert testLead; | ||
|
||
// Create a ConvertLeadActionRequest with opportunityName | ||
ConvertLeadAction.ConvertLeadActionRequest request = new ConvertLeadAction.ConvertLeadActionRequest(); | ||
request.leadId = testLead.Id; | ||
request.convertedStatus = 'Closed - Converted'; | ||
request.opportunityName = 'Test Opportunity'; | ||
|
||
// Call the ConvertLeadAction method | ||
List<ConvertLeadAction.ConvertLeadActionRequest> requests = new List<ConvertLeadAction.ConvertLeadActionRequest>(); | ||
requests.add(request); | ||
List<ConvertLeadAction.ConvertLeadActionResult> results = ConvertLeadAction.convertLeads(requests); | ||
|
||
// Verify the Lead was converted | ||
System.assertEquals(1, results.size(), 'Expected one result'); | ||
ConvertLeadAction.ConvertLeadActionResult result = results[0]; | ||
System.assertNotEquals(null, result.accountId, 'Expected an Account ID'); | ||
System.assertNotEquals(null, result.contactId, 'Expected a Contact ID'); | ||
System.assertEquals(null, result.opportunityId, 'Did not expect an Opportunity ID'); | ||
|
||
// Verify the Lead is converted and an Opportunity with the specified name is created | ||
testLead = [SELECT IsConverted FROM Lead WHERE Id = :testLead.Id]; | ||
System.assertEquals(true, testLead.IsConverted, 'Expected the Lead to be converted'); | ||
Opportunity opportunity = [SELECT Name FROM Opportunity WHERE AccountId = :result.accountId]; | ||
System.assertEquals('Test Opportunity', opportunity.Name, 'Expected Opportunity with the specified name'); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>59.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |