Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

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

}

1 Answer

0 votes
by (32.1k points)
edited by

Hey!

Try using the code below it seems to work well!

// Trigger creates a new product record with specific record type

trigger Trigger_InsertNewYProduct  on Opportunity (after insert, after update) 

{

boolean runOnce = true ; 

if(runOnce){

for(Opportunity mem:trigger.New)

{

List<Yushin_Product__c> pro = new List<Yushin_Product__c>();

if(mem.Robot__c == true)

{

Yushin_Product__c it = new Yushin_Product__c(RecordTypeId='012180000004huP', Opportunity__c= mem.Id);

pro.add(it);

// Inserts yushin product and opportunity ids          

   

}

if(pro .size()>0){

insert pro ;

}

}

}

runOnce = false ;

}

Want to become a Salesforce Expert? join this Salesforce certification now!! 

Browse Categories

...