1

I would like to call a custom APEX Class that converts a lead. I have successfully been able to create the LWC, Call the Apex Class, & Convert the lead. I am having trouble returning the new Account Id & Contact Id to the LWC to then handle redirects and other post create/convert actions.

Here is the APEX CLASS:

public class AutoConvertLeads {  
   @AuraEnabled
   Public static id doConvert(Id leadId){

        System.debug('doConvert LeadId: ' + leadId);
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(leadId);
        lc.setDoNotCreateOpportunity(True);
        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
        System.debug('doConvert ConvertStatus: ' + convertStatus);
        lc.setConvertedStatus(convertStatus.MasterLabel);
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.debug('doConvert LeadConvertResult: ' + lcr);

        id accountId = lcr.getAccountId();
        id contactId = lcr.getcontactId();

        System.debug('doConvert AccountId Result: ' + accountId);
        System.debug('doConvert ContactId Result: ' + contactId);

        return accountId;
    }
}

When checking debug logs I am able to see that the Account Id & Contact Id are being set and are correct.

Here is the JS for the LWC:

// Lead Convert Functionality
convert(){

console.log('Convert Button was Clicked!');
console.log('Record Id:');
console.log(this.recordId);

leadConvert({leadId : this.recordId})
    .then(result => {

        console.log('Convert Result:');
        console.log(result);

    })
    .catch(error => {
        console.log('There was an Error :( ...');
        this.error = error;
        console.log(error);
    })
}

Any assistance you can provide would be appreciated.

1

Instead of returning just the Id, return a map instead

 @AuraEnabled
   public static Map<String,String> doConvert(Id leadId){

    System.debug('doConvert LeadId: ' + leadId);
    Map<String,Id> mapResult = new Map<String,Id>();
    Database.LeadConvert lc = new database.LeadConvert();
    lc.setLeadId(leadId);
    lc.setDoNotCreateOpportunity(True);
    LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
    System.debug('doConvert ConvertStatus: ' + convertStatus);
    lc.setConvertedStatus(convertStatus.MasterLabel);
    Database.LeadConvertResult lcr = Database.convertLead(lc);
    System.debug('doConvert LeadConvertResult: ' + lcr);

    id accountId = lcr.getAccountId();
    id contactId = lcr.getcontactId();

    mapResult.put('accountId', accountId);

    mapResult.put('contactId', contactId);

    System.debug('doConvert AccountId Result: ' + accountId);
    System.debug('doConvert ContactId Result: ' + contactId);

    return mapResult;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.