Skip to main content

Good day, can someone please assist with the following: 

 

It is code to Convert leads in our Org.

This is the error I get when trying to convert the lead:

 

Error: ConvertLead failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, There was an error converting the lead. Please resolve the following error and try again: LeadTrigger: execution of AfterUpdate caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: CANNOT_UPDATE_CONVERTED_LEAD, This lead was already converted to the contact [Lead name & Last name} on 3/30/2020.: [] Class.Convertlead.afterInsertAction: line 23, column 1 Class.Convertlead.afterUpdateAction: line 83, column 1 Class.Convertlead.afterUpdate: line 70, column 1 Trigger.LeadTrigger: line 8, column 1: []

 

Here is my code:

 

public class Convertlead{

    public  static void afterInsert(List<Lead> newListOfLead){

        afterInsertAction(newListOfLead);

    }

    

    private  static void afterInsertAction(List<Lead> newOfLead){

       Set<Id> convertedsetAccountId = new set<id>();

       set<id> convertedOpportunityId = new set<id>();

       String OppName;

       String companyName;

       list<opportunity> newListofOpp = new List<Opportunity>();

        for(Lead objLead: newOfLead){

            if(objLead.Status == 'Closed - Converted'){

                    Database.LeadConvert lc = new Database.LeadConvert();

                    lc.setLeadId(objLead.id);

                

                    LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true limit 1 ];

                    lc.setConvertedStatus(convertStatus.MasterLabel);

            

                    Database.LeadConvertResult lcr = Database.convertLead(lc);

                    System.assert(lcr.isSuccess());

                    convertedsetAccountId.add(lcr.getAccountId());

                    

                    companyName = objLead.Company;

                    OppName =  objLead.City + ' '+ objLead.State;

             

             }       

        }

         List<Account> updateLstOfAccount = new List<Account>();

        if(convertedsetAccountId.size() >0 ){

          

           list<Account> listOfAcc = [select id, Name,(select id from opportunities where AccountId in :convertedsetAccountId ) from Account where id in:  convertedsetAccountId limit 50000];

            for(Account objAcc :listOfAcc){

              

              Account obj = new Account();

              obj.Name = companyName; // 'Venue Lead' ;

              obj.Id = objAcc.id;

              updateLstOfAccount.add(obj);

              for(Opportunity opp : objAcc.opportunities){

                    Opportunity objOppo = new Opportunity();

                    objOppo.Name =  OppName ;

                    objOppo.id = opp.Id;

                    objOppo.StageName = 'Interested';

                    newListofOpp.add(objOppo);

               }

    

            }

        

        }

        try{

            if(updateLstOfAccount.size() > 0){

                update updateLstOfAccount;

            }

            

            if(newListofOpp.size() > 0){

                Update newListofOpp;

            }

        }Catch(Exception ex){

        

            System.debug('Exception ex'+ex.getMessage()+' Get line Number :'+ex.getlineNumber());

        }    

        

     }  

 

     public static void afterUpdate(Map<id,Lead> newMapOfLead, Map<id, Lead> oldMapOfLead){

            afterUpdateAction(newMapOfLead, oldMapOfLead);

      }

     

      private static void afterUpdateAction(Map<id,Lead> newMapOfLead, Map<id, Lead> oldMapOfLead){

           

           List<Lead> upDateLead = new List<Lead>();

            for(Lead ObjLead : newMapOfLead.values()){

                if(ObjLead.Status != oldMapOfLead.get(ObjLead.id).Status  && objLead.Status == 'Closed - Converted'){

                    upDateLead.add(ObjLead);

                }

            

            } 

            if(upDateLead.size() > 0){

                afterInsertAction(upDateLead);

            }   

        

     }

 

}

0/9000