You need to sign in to do that
Don't have an account?

Lead Trigger to update multiple custom objects
I have multiple Lead Remarks that need to get updated when a lead is converted. Below is my code, however when it triggers, it only updates the first lead remark. I need it to update all of them. I am guessing that I need a for loop in here somewhere, but not sure where.
trigger UpdateCustomeObject_Trigger on Lead (before update) {
//This trigger will associate a Custom Object record with the contact and opportunity associated to the
//lead after it has been converted.
//The Custom Object is associated to an opportunity only if an opportunity record exist on the Lead.
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, Lead_Remark__c> entries = new Map<Id, Lead_Remark__c>([select Contact__c, Lead__c from Lead_Remark__c where lead__c in :leadIds]);
if(!Trigger.new.isEmpty()) {
for (Lead lead : Trigger.new) {
for (Lead_remark__c Lead_Remark : entries.values()) {
if (Lead_Remark.Lead__c == lead.Id) {
Lead_Remark.contact__c = lead.ConvertedContactId;
update Lead_Remark;
break;
}
}
}
}
}
}
}
Thanks in advance,
David
Not sure what the issue is - you can probably figure it out by adding System.debug() lines to figure out where the problem is happening (count # records to make sure the query works, then see if the loop works, then the update, etc).
btw-you should take the update out of the loop as you'll run into issues otherwise. Add the custom object records to a list & then call update once against the whole list outside of the for loop.