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
Dman100Dman100 

lead trigger help

I'm creating a trigger on a lead to auto-populate several fields when the lead is updated based on a lookup field to a custom object.
 
I've tried both a before update and after update trigger.
 
Here is the trigger code:
 
Code:
trigger ImportMDRDataTrigger on Lead (after update) {
 
 Lead[] leadList = [Select Id, MDR_Account__r.Enrollment__c, MDR_Account__r.MDR_School_I_D__c, MDR_Account__r.MDR_District_I_D__c from Lead WHERE MDR_Account__c != null and Id in :Trigger.new];
 
 List<Lead> leadsToUpdate = new List<Lead>();
 
 for (Lead l : leadList) {
     l.Enrollment__c = l.MDR_Account__r.Enrollment__c;
     l.MDR_District_PID__c = l.MDR_Account__r.MDR_District_I_D__c;
     l.MDR_School_PID__c = l.MDR_Account__r.MDR_School_I_D__c;
     leadsToUpdate.add(l);
  }
  update (leadsToUpdate);

}

 
Here is the error I get when using a before update trigger:
 
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger ImportMDRDataTrigger caused an unexpected exception, contact your administrator: ImportMDRDataTrigger: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00Q7000000NibUREAZ; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 00Q7000000NibUR) is currently being updated in trigger ImportMDRDataTrigger: [Id]: Trigger.ImportMDRDataTrigger: line 13, column 5
 
Here is the error I get when using an after update trigger:
 
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger ImportMDRDataTrigger caused an unexpected exception, contact your administrator: ImportMDRDataTrigger: maximum trigger depth exceeded Lead trigger event AfterUpdate for 00Q7000000NibUR Lead trigger event AfterUpdate for 00Q7000000NibUR Lead trigger event AfterUpdate for 00Q7000000NibUR Lead trigger event AfterUpdate for 00Q7000000NibUR Lead trigger event AfterUpdate for 00Q7000000NibUR Lead trigger event AfterUpdate for 00Q7000000NibUR Lead trigger event AfterUpdate for 00Q7000000NibUR Lead trigger event AfterUpdate for 00Q7000000NibUR Lead trigger event AfterUpdate for 00Q7000000NibUR Lead trigger event AfterUpdate for 00Q7000000NibUR Lead trigger event AfterUpdate for 00Q7000000NibUR Lead trigger event AfterUpdate for 00Q7000000NibUR Lead trigger event AfterUpdate for 00Q7000000NibUR Lead trigger event AfterUpdate for 00Q7000000NibUR Lead trigger event AfterUpdate for 00Q7000000NibUR Lead trigger event AfterUpdate for 00Q7000000NibUR
 
Any help is appreciated.
Thank you.
jrotensteinjrotenstein
The error is indicating that you are 'inside' a trigger due to the update of a Lead, and that within the Trigger code you are calling update that impacts the same Lead object.

Have you considered changing to a before update trigger? That way, you can modify the values of the 'updated' objects without having to call update. It would be something like this:

Code:
trigger ImportMDRDataTrigger on Lead (before update) {

// Get additional information on the Leads in Trigger.new
Lead[] leadList = [Select Id, MDR_Account__r.Enrollment__c, MDR_Account__r.MDR_School_I_D__c, MDR_Account__r.MDR_District_I_D__c from Lead WHERE MDR_Account__c != null and Id in :Trigger.new];

// Convert this into a Map, indexed by Id
Map<Id, Lead> leadMap = new Map<Id, Lead>(leadList);

for (Lead l : Trigger.new) {
if (l.MDR_Account__c == null)
continue;
 the_lead = leadMap.get(l.Id); // Find the additional data for this leads
l.Enrollment__c = the_lead.MDR_Account__r.Enrollment__c;
l.MDR_District_PID__c = the_lead.MDR_Account__r.MDR_District_I_D__c;
l.MDR_School_PID__c = the_lead.MDR_Account__r.MDR_School_I_D__c;
}

}
Note: I haven't tested this code!


Message Edited by jrotenstein on 10-10-2008 02:58 PM
Dman100Dman100

Thank you John,

That got it working.

I'm not clear on maps, can you explain what these lines of code are doing?

Map<Id, Lead> leadMap = new Map<Id, Lead>(leadList);

  for (Lead l : Trigger.new) {  // You are looping every lead passed in by the trigger
     if (l.MDR_Account__c == null) // Why the check if MDR_Account__c is null ?
       continue;  // only continue if MDR_Account__c is equal to null ?
     the_lead = leadMap.get(l.Id);

Thanks again!
Regards.
jrotensteinjrotenstein
Wow! It really worked? I'm amazed.

Have a look at "map methods" in the Apex reference manual. It's basically a lookup key/value array.

The first line converts the returned objects into a Map, indexed by Id.

leadMap.get retrieves the record indexed by the specified Id.
jrotensteinjrotenstein
By the way, did you think of just creating Formula fields, to access the fields on the Account object? You might not need the Trigger.
Dman100Dman100

No, I didn't know formula fields could autopopulate from another object?  Is there an example that demonstrates how to write a formula to do that?

Thanks again for your help.

Regards.

Jack InsJack Ins

I am new to this but also would like to know.  I believe that i have the same situation.  I need to update a Lead field based on a field from the selected Account.

Thanks Dwayne

Jack InsJack Ins
 
Please take a look at this.  It helped me to find my solution.  Formula fields will work great.
Thanks Dwayne
arnt72arnt72
should work fine without the update. Trigger implies the update. update would be only necessary for records that are not included in the trigger.
However, you have to loop through Trigger.New to create the leadlist instead of creating it with a Select.

for (Lead l:: Trigger.New){
  if (criteria){
     leadlist.add(l);
  }
}