Thursday, 9 September 2021

How to clone Lead record in apex ?

 We have standard clone button where we can clone only lead record, what if we need to clone all the child records and attachments along with Lead then apex is only options.

1. Define Quick Action "Clone", call lightning component.

2. Change layout add custom clone button in the place of Standard clone button.

We have to make sure, if any custom fields added in Lead in future then cloned should have that new field populated  and should be able to be converted.  That being said we cant hardcode field name. We have to make it dynamic.

Here is code block to get all fields dynamically in SOQL.

// This is the set where all fields name will be stored
Set<String> fieldNames = new Set<String>();
Map<StringSchema.SObjectField> fieldMap = Lead.sObjectType.getDescribe().fields.getMap();
// Get all of the fields on the object
for(String sFielName:fieldMap.keySet()){
  fieldNames.add(sFielName);
}
String fieldsName = String.join((Iterable<String>) fieldNames, ',');
// Build a Dynamic Query String.
String soqlQuery = ' SELECT ' + fieldsName + ' FROM Lead Where Id =: leadRecId';
system.debug('soqlQuery---'+soqlQuery);

The above code has one caveat, all the fields will be included, for example IsConverted field also be updated
which will stop lead conversion, we would not want that.
We have to tweak above code little bit not to include some of the fields.

Set<String> excludedFieldsName = new Set<String>{'isconverted','converteddate','convertedaccountid','convertedcontactid',
            'convertedopportunityid','createdbyid','lastmodifieddate','createddate','lastmodifiedbyid','systemmodstamp',
            'lastactivitydate','lastvieweddate','lastreferenceddate','ownerid'};

for(String sFielName:fieldMap.keySet()){
    if(excludedFieldsName.contains(sFielName)) continue;
  fieldNames.add(sFielName);
}

Now query is ready to pull lead record with desired fields , Lets talk about clone method. 

In salesforce the sObject class has a clone method that can be used. When using the clone method, there are two types of cloning: a shallow clone or a deep clone. Clone method has 4 parameters. We will get very good article to talk bout each parameters.

clone(Boolean preserveId, Boolean isDeepClone, Boolean preserveReadonlyTimestamps, Boolean preserveAutonumber)

https://www.levelupsalesforce.com/clone-method-in-salesforce

Here is my code to clone Lead only the fields available in SOQL.

List<Lead> Leads = Database.query(getSOQLStament());
System.debug('Leads--'+Leads);
if(!Leads.IsEmpty()){
    Lead newLead = Leads[0].clone(false, true);
    newLead.status = 'Active';
    Insert newLead;
    System.debug('newLead--'+newLead.id);
}

private static String getSOQLStament(){
        // This is the object for which we required data.
        Set<String> fieldNames = new Set<String>();
        Set<String> excludedFieldsName = new Set<String>{'isconverted','converteddate','convertedaccountid','convertedcontactid',
            'convertedopportunityid','createdbyid','lastmodifieddate','createddate','lastmodifiedbyid','systemmodstamp',
            'lastactivitydate','lastvieweddate','lastreferenceddate','ownerid'};
    Map<StringSchema.SObjectField> fieldMap = Lead.sObjectType.getDescribe().fields.getMap();
        // Get all of the fields on the object
        for(String sFielName:fieldMap.keySet()){
            if(!excludedFieldsName.contains(sFielName)){
                fieldNames.add(sFielName);
            }
             
        }
        String fieldsName = String.join((Iterable<String>) fieldNames, ',');
        // Build a Dynamic Query String.
    String soqlQuery = ' SELECT ' + fieldsName + ' FROM Lead Where Id =: leadRecId';
        system.debug('soqlQuery---'+soqlQuery);
        return soqlQuery;

    } 



  

No comments: