0

Requirement--

I have one custom Object(Lead_map__c) where values are stored Like leadSource={'Web','Email','Phone'} and respective userIds in userId_assignee__c column

Whenever lead is updated with Checkbox Need Assistance=true then ownerId will be change based on Lead_map__c table and needs to create and assgine task to owner and when Lead is updated to false then userid needs to be change with currentUser

I have written a code which is working but if anyone can suggest a best practice or let me know any issue,mistake,any other simple way to write this logic.

public class LeadMapHandler {
public static boolean isrun =true;
public static void updateLeadOwner(List<lead> leadList,map<id,lead>oldmap) {
    set<string> leadSource = new set<string>();
    try{
        for(lead l:leadList) {
            leadSource.add(l.leadsource);  //get the source of lead
        }

        map<string,lead_map__c> leadMap = new map<string,lead_map__c>();

        for(lead_map__c lm : [select id,UserId_to_assign__c,Lead_Source__c from Lead_Map__c where Lead_Source__c in:leadSource]) {
            leadMap.put(lm.lead_source__c,lm); 
        }

        List<lead> updateLead = new List<lead>();
        for(lead leadRecord:leadList) {
            lead oldlead =oldmap.get(leadRecord.id);
            if((oldlead.Need_Assistance__c!=leadRecord.Need_Assistance__c) && leadRecord.Need_Assistance__c) {
                if(leadMap.get(leadRecord.LeadSource)!=null) {
                    lead ls = new lead();
                    ls.id=leadRecord.Id;
                    ls.OwnerId =leadMap.get(leadRecord.LeadSource).UserId_to_assign__c;
                    updateLead.add(ls);
                    createTask(leadMap.get(leadRecord.LeadSource).UserId_to_assign__c);  
                }
            }
            else if(leadRecord.Need_Assistance__c== false){
                updateLeadBack(leadRecord);  //call a method to update Owner with CurrentLoged in User
            }    
        }
        if(updateLead.size()>0) {
            update updateLead;
        }
    }
    catch(exception e) {
        system.debug('EXCEPTION OCCURED-->'+e);
    } 
}
public static void updateLeadBack(lead l) {
    lead ld = new lead();
    ld.Id=l.id;
    ld.OwnerId=userinfo.getUserId();
    List<lead> updateLeadback = new List<lead>();
    updateLeadback.add(ld); 
    update updateLeadback;
}

1 Answer 1

0
updateLeadBack(leadRecord);  //call a method to update Owner with CurrentLoged in User

...

update updateLeadback;

Do not DML in a loop.


    if(updateLead.size()>0) {
        update updateLead;
    }

DML on an empty list is okay, and actually faster than checking yourself on a non-empty list. Just do:

update updateLead;

catch(exception e) {
    system.debug('EXCEPTION OCCURED-->'+e);
} 

Do not try-catch if you don't intend to handle the error. Either let the exception throw, or, preferably, handle the errors in bulk:

    List<lead> updateLead = new List<lead>();
    List<lead> sourceLead = new List<Lead>();
    for(lead leadRecord:leadList) {
        lead oldlead =oldmap.get(leadRecord.id);
        // omitted if logic //
        updateLead.add(ls);
        sourceLead.add(leadRecord);
    }
    Database.SaveResult[] results = Database.update(updateLead, false);
    for(Integer i = 0, s = results.size(); i < s; i++) {
      if(!results[i].isSuccess()) {
        sourceLead[i].addError('Error updating lead: '+results[i].getErrors()[0].getMessage());
      }
    }

A Boolean is already a Boolean:

        else if(leadRecord.Need_Assistance__c== false){

should be just:

        else if(!leadRecord.Need_Assistance__c){

List<lead> updateLeadback = new List<lead>();
updateLeadback.add(ld); 
update updateLeadback;

If you ever need to update one record, just update it:

update ld;

No need to have a list of records (but note you should be using bulk updates here).


If you use a before update trigger instead of after update, you can update these fields without the need for a recursive DML operation (and need for the isrun flag).

5
  • Thanks sfdxFox.How can i avoid this statment out of for loop if(leadRecord.Need_Assistance__c== false){ updateLeadBack(leadRecord); //call a method to update Owner with CurrentLoged in User }
    – sandy
    Commented Apr 21, 2020 at 12:47
  • yes. i need my code run for bulk records
    – sandy
    Commented Apr 21, 2020 at 12:47
  • @sandy As I said, change your trigger to before update, and you don't need any of those DML operations.
    – sfdcfox
    Commented Apr 21, 2020 at 12:50
  • thanks..will do that.
    – sandy
    Commented Apr 21, 2020 at 12:50
  • thanks for help.i like your way of help to others. learning many things from you.
    – sandy
    Commented Apr 21, 2020 at 12:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .