You need to sign in to do that
Don't have an account?
FB Admin
Trigger only updating one record
Newbie here. Trying to update multiple records when the user record of the owner is updated. It is only updating the first record and not properly looping through/updating all as I would expect. Any advise appreciated, been googling for hours.
trigger UpdateDistrictRegion on User (after update) {
List<Lead> leadsToUpdate = [select id,CenterDistrict__c,CenterRegion__c from Lead where OwnerID IN :Trigger.New];
System.debug(leadsToUpdate);
List<Lead> leads = new List<Lead>();
User u = Trigger.New[0];
for(Lead Leadz : leadsToUpdate) {
leadz.CenterDistrict__c = u.District__c;
leads.add(leadz);
{
update leads;
}
}
}
trigger UpdateDistrictRegion on User (after update) {
List<Lead> leadsToUpdate = [select id,CenterDistrict__c,CenterRegion__c from Lead where OwnerID IN :Trigger.New];
System.debug(leadsToUpdate);
List<Lead> leads = new List<Lead>();
User u = Trigger.New[0];
for(Lead Leadz : leadsToUpdate) {
leadz.CenterDistrict__c = u.District__c;
leads.add(leadz);
{
update leads;
}
}
}
All Answers
IF YOU WANTED TO DO IT FOR Existing users also go and update the ALL USERS OR USE BATCH TO UPDATE
Actually the query will return all Leads owned by all the User records that have been updated.
Then for each of those Leads, l update the lead's CenterDistrict__c to the District__c of the Lead's Owner If this code is only updating 1 Lead then that means that there is only 1 Lead that is owned by the User records that have been updated.
Did I miss anything?
TRY THIS CODE
Thanks for the help here. I marked the answer from Steven as best. It turns out another Apex class/trigger was interfering. I moved the trigger to fire on account as District is updated on Account as well and was able to get it to work. Here is the code presently working:
Now having trouble with the test class though. After trying all day, finally decided to post to the forum.
https://developer.salesforce.com/forums/?id=9060G000000I1LdQAK#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=OPENQUESTIONS&id=9060G0000005llUQAQ