Search code examples
triggerssalesforceapex-code

Salesforce Lead Trigger CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY


I want to clone the Profile__c record. The lead has a profile__c associated with it. When conversion happens, the Profile_c on the lead is copied to the account created. What I need to do is a deep clone of the Profile__c on the new account created after the conversion. I am able to copy the profile_c over but cloning throws this error:

Error: System.DmlException: Update failed. First exception on row 0 with id 00QJ0000007dDmHMAU; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, profile: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_UPDATE_CONVERTED_LEAD, cannot reference converted lead: [] Trigger.profile:, column 1: [] (System Code)

trigger profile on Lead (after update) {

Map<Id, Lead> cl = new Map<Id,Lead>();
Lead parent;
List<Contact> clist = new List<Contact>();
Set<Id> convertedids = new Set<Id>();

//list of converted leads
for (Lead t:Trigger.new){
    Lead ol = Trigger.oldMap.get(t.ID);
    if(t.IsConverted == true && ol.isConverted == false)
    {
        cl.put(t.Id, t);
        convertedids.add(t.ConvertedContactId);
    }
}
 Set<Id> leadIds = cl.keySet();  

  List<Profile__c> mp = [select Id, lock__c, RecordTypeId, reason__c, End_Date__c,startus__c , Opportunity__c, Account__c, Lead__c from Profile__c where Lead__c in :leadIds];
  List<ID>AccountIDs = new List<ID>();
  List<Profile__c>clonedList = new list<Profile__c>();
  for (Profile__c mpi:mp){
    parent = cl.get(mpi.Lead__c );
    mpi.opportunity__c = parent.ConvertedOpportunityId;
    mpi.account__c = parent.ConvertedAccountId;
    AccountIDs.add(parent.ConvertedAccountId);
    Profile__c profile = mpi.clone(false,true,false,false);
    clonedList.add(profile);
    mpi.lock__c= true;
    mpi.reason__c= 'Converted';
  }
 update mp;
 insert clonelist
}

Solution

  • You are doing insert operation(insert clonelist) in which you are accessing Converted lead Id value in a field. You can't use converted LeadId field in DML operations. Below is the Sample code that will work-

    trigger ConvertedLead_Trigger on Lead (after update) {
    Map<Id, Lead> cl = new Map<Id,Lead>();
    Lead parent;
    List<Contact> clist = new List<Contact>();
    Set<Id> convertedids = new Set<Id>();
    
    //list of converted leads
    for (Lead t:Trigger.new){
        Lead ol = Trigger.oldMap.get(t.ID);
        if(t.IsConverted == true && ol.isConverted == false)
        {
            cl.put(t.Id, t);
            convertedids.add(t.ConvertedContactId);
        }
    }
     Set<Id> leadIds = cl.keySet();
         List<ConvertLeadTest__c> mp =[Select Id,Name,Lead__c, Account__c,Opportunity__c from ConvertLeadTest__c where Lead__c in :leadIds];
        List<ConvertLeadTest__c> mp1=new List<ConvertLeadTest__c>();
        List<ConvertLeadTest__c> mp2=new List<ConvertLeadTest__c>();
        for(ConvertLeadTest__c cc:mp)
        {
            if(cl.containsKey(cc.Lead__c))
            {
    
              cc.Account__c=cl.get(cc.Lead__c).ConvertedAccountId;
              cc.Opportunity__c=cl.get(cc.Lead__c).ConvertedOpportunityId;
                mp1.add(cc);
                mp2.add(new ConvertLeadTest__c(Account__c=cl.get(cc.Lead__c).ConvertedAccountId,Opportunity__c=cl.get(cc.Lead__c).ConvertedOpportunityId));
            }
        }
    
        update mp;
        insert mp2;
    }
    

    But if you write ConvertLeadTest__c(Lead__c=cc.Lead__c,Account__c=cl.get(cc.Lead__c).ConvertedAccountId,Opportunity__c=cl.get(cc.Lead__c).ConvertedOpportunityId)); then it will throw error.

    Hope this will help you.

    Thanks :)