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
JNicJNic 

Lead Convert After Update Trigger

Hey guys,

 

Contacts are a child of a Lead, so a Lead can have multiple contacts (Contacts >> Lead). Once the user converts the lead, I want to associate all of the child Contacts with the converted Account. I have a trigger as follows:

trigger leadAfter on Lead (after update) {

    //create a list of Contacts to update with Converted Account ID
    List< Contact > contacts = new list< Contact >();
   
  for ( Lead l : Trigger.new )
  {
    if (Trigger.old[0].isConverted == false && Trigger.new[0].isConverted == true) {
        System.debug('******Entering LeadAfter******');

        //Create a list of contacts that matches the Converted LeadId
        Contact c = [SELECT id, Lead__c, Name, AccountId FROM Contact c WHERE c.Lead__c = :l.Id AND c.AccountId = null];
       
        c.AccountId = l.ConvertedAccountId;
        contacts.add(c);
    }
        update contacts;
}
}

 

This trigger doesn't work and throws the following error: List has more than 1 row for assignment to SObject Trigger.leadAfter: line 12, column 21.

 

How can I update all of the child Contacts in a bulk manner?

 

Your input is appreciated.

 

Best Answer chosen by Admin (Salesforce Developers) 
JNicJNic

I figured it out. I reprogrammed it per below:

trigger leadAfter 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, Contact> contacts = new Map<Id, Contact>([select Lead__c, AccountId from Contact where lead__c in :leadIds]);      
            if(!Trigger.new.isEmpty()) {
                for (Lead lead : Trigger.new)  {
                    for (Contact c : contacts.values()) {
                        if (c.Lead__c == lead.Id) {
                            c.AccountId = lead.ConvertedAccountId;
                            update c;
                        }
                    }
                }
            }
        }
    }
}

All Answers

JNicJNic

I figured it out. I reprogrammed it per below:

trigger leadAfter 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, Contact> contacts = new Map<Id, Contact>([select Lead__c, AccountId from Contact where lead__c in :leadIds]);      
            if(!Trigger.new.isEmpty()) {
                for (Lead lead : Trigger.new)  {
                    for (Contact c : contacts.values()) {
                        if (c.Lead__c == lead.Id) {
                            c.AccountId = lead.ConvertedAccountId;
                            update c;
                        }
                    }
                }
            }
        }
    }
}

This was selected as the best answer
VictorBLVictorBL

I wrote the following trigger, but I need to convert it to an after update one! HELP PLEASE 

It just adds the price items of the work Order. (Price Items are afected by a parameter in the work order)

 

trigger WOROrder on Orden_de_Servicio__C (before update) {

 

List<Orden_de_Servicio__C> toUpdate=new List<Orden_de_Servicio__C>();
for(Orden_de_Servicio__C myOrder : trigger.new){ 

 

 myOrder.Total_Orden__C = 0; 


// Populate the list of items based on trigger typeList

 

<AIT_Item__C> itemList = [SELECT j.Precio_Item__C, j.Orden_de_Servicio__C FROM AIT_Item__C j WHERE j.Orden_de_Servicio__r.id IN : Trigger.new FOR UPDATE];


// Process the list of items

 

for(AIT_Item__C itemToProcess : itemList){           

myOrder.Total_Orden__C += itemToProcess.Precio_Item__C;  }                         

 

toUpdate.add(myOrder);}                        

 

}.