function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nirmal9114Nirmal9114 

When a Lead is Converted, all Custom Object Record should become associated to the Account, if they are not already

My requirement is :
When a Lead is Converted, all Custom Object (Key Staff) records should become associated to the Account, if they are not already (e.g. the Account lookup is unpopulated).

NOTE: The system should not allow duplicate Custom Object(Key Staff members) on an Account during conversion. If a Key Staff record already exists on the Account with the same Contact and Role values, do not update the Key Staff from lead to be associated with the Account

I have written this trigger : PLEASE HELP 


trigger UpdateKeyStaff_Trigger on Lead (after update) {

    for (Integer i = 0; i < Trigger.new.size(); i++){
        if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false){
            Set<Id> leadIds = new Set<Id>();
            for (Lead lead : Trigger.new) 
                leadIds.add(lead.Id);

            Map<Id, Key_Staff__c> entries = new Map<Id, Key_Staff__c>([select Account__c, Accenture_Resource__c, Board_Member__c, Email__c, Role__c, Lead__c from Key_Staff__c where lead__c in :leadIds]);        
            if(!Trigger.new.isEmpty()) {
                for (Lead lead : Trigger.new)  {
                    for (Key_Staff__c KeyStaff : entries.values()) {
                        if (KeyStaff.Lead__c == lead.Id) {
                            KeyStaff.Accenture_Resource__c = lead.ConvertedContactId;
                            KeyStaff.Grant__c = lead.ConvertedOpportunityId;
                            KeyStaff.Account__c = lead.ConvertedAccountId;
                            update KeyStaff;                            
                        }
                    }
                }
            }
        }
    }

}