Skip to main content
Fixed code formatting, adding relevant tags
Source Link
Derek F
  • 64.2k
  • 15
  • 55
  • 104
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;
}

class ConvertLeadActionException extends Exception {}
}
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;
    }

    class ConvertLeadActionException extends Exception {}
}
@isTest
private class ConvertLeadActionTest {
      
    static testMethod void testLeadConv() {
        Test.StartTest();  
        Lead myLead = new Lead(LastName = 'Fry', Company='Fry And Sons', Industry = 'Multifamily Residential');
insert myLead;

Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(myLead.id);

LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
lc.setConvertedStatus('SQL');

Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());
        Test.StopTest();  
    }
}
@isTest
private class ConvertLeadActionTest {
      
    static testMethod void testLeadConv() {
        Test.StartTest();  
        Lead myLead = new Lead(LastName = 'Fry', Company='Fry And Sons', Industry = 'Multifamily Residential');
        insert myLead;

        Database.LeadConvert lc = new Database.LeadConvert();
        lc.setLeadId(myLead.id);

        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
        lc.setConvertedStatus('SQL');

        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());
        Test.StopTest();  
    }
}
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;
}

class ConvertLeadActionException extends Exception {}
}
@isTest
private class ConvertLeadActionTest {
      
    static testMethod void testLeadConv() {
        Test.StartTest();  
        Lead myLead = new Lead(LastName = 'Fry', Company='Fry And Sons', Industry = 'Multifamily Residential');
insert myLead;

Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(myLead.id);

LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
lc.setConvertedStatus('SQL');

Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());
        Test.StopTest();  
    }
}
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;
    }

    class ConvertLeadActionException extends Exception {}
}
@isTest
private class ConvertLeadActionTest {
      
    static testMethod void testLeadConv() {
        Test.StartTest();  
        Lead myLead = new Lead(LastName = 'Fry', Company='Fry And Sons', Industry = 'Multifamily Residential');
        insert myLead;

        Database.LeadConvert lc = new Database.LeadConvert();
        lc.setLeadId(myLead.id);

        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
        lc.setConvertedStatus('SQL');

        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());
        Test.StopTest();  
    }
}
Source Link

Cannot Increase Code Coverage for Salesforce supplied Lead Conversion Apex Class

I am trying to utilize this Apex Class supplied by Salesforce but when i try to deploy to production i get a code coverage of 68%:

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;
}

class ConvertLeadActionException extends Exception {}
}

I tried to create a test class to just simulate the Lead conversion and get coverage above 75%, but am having issues calling the method Salesforce supplied.

@isTest
private class ConvertLeadActionTest {
      
    static testMethod void testLeadConv() {
        Test.StartTest();  
        Lead myLead = new Lead(LastName = 'Fry', Company='Fry And Sons', Industry = 'Multifamily Residential');
insert myLead;

Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(myLead.id);

LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
lc.setConvertedStatus('SQL');

Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());
        Test.StopTest();  
    }
}

I am not sure if this is even necessary. I am a bit new to Apex so any insight would be greatly appreciated and apologies if I am not formatting this question correctly.