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
FB AdminFB 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;
}
}
}
Best Answer chosen by FB Admin
Steven NsubugaSteven Nsubuga
Try this
trigger UpdateDistrictRegion on User (after update) {
	List<Lead> leadsToUpdate = [select id,CenterDistrict__c,CenterRegion__c, OwnerID from Lead where OwnerID IN :Trigger.NewMap.keyset()];
	System.debug(leadsToUpdate);
	List<Lead> leads = new List<Lead>();
	
    for(Lead Leadz : leadsToUpdate) {
		leadz.CenterDistrict__c = Trigger.NewMap.get(Leadz.OwnerID).District__c;
		leads.add(leadz);	
	}
	update leads;
}

 

All Answers

Steven NsubugaSteven Nsubuga
Try this
trigger UpdateDistrictRegion on User (after update) {
	List<Lead> leadsToUpdate = [select id,CenterDistrict__c,CenterRegion__c, OwnerID from Lead where OwnerID IN :Trigger.NewMap.keyset()];
	System.debug(leadsToUpdate);
	List<Lead> leads = new List<Lead>();
	
    for(Lead Leadz : leadsToUpdate) {
		leadz.CenterDistrict__c = Trigger.NewMap.get(Leadz.OwnerID).District__c;
		leads.add(leadz);	
	}
	update leads;
}

 
This was selected as the best answer
FB AdminFB Admin
Thanks for the try Steven, but it is still only updated the first record.User-added image
Raj VakatiRaj Vakati
I dnt see any issue in Steven Nsubuga code .It will work for all leads related to one user ..

IF YOU WANTED TO DO  IT FOR Existing users also go and update the ALL USERS OR USE BATCH TO UPDATE 
Steven NsubugaSteven Nsubuga
Thanks Raj. 
Actually the query 
List<Lead> leadsToUpdate = [select id,CenterDistrict__c,CenterRegion__c, OwnerID from Lead where OwnerID IN :Trigger.NewMap.keyset()];
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
    for (Lead Leadz : leadsToUpdate) {
		leadz.CenterDistrict__c = Trigger.NewMap.get(Leadz.OwnerID).District__c;
		leads.add(leadz);	
	}
	update leads;
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?


 
Raj VakatiRaj Vakati
That is true .. May be one Lead Owned By user and Reminning is OWNED BY QUEUE 
Raj VakatiRaj Vakati

TRY THIS CODE



 

trigger UpdateDistrictRegion on User (after update) {
	
	GroupMember dm =[Select Group.Name,userOrGroupId from GroupMember where UserOrGroupId =:Trigger.NewMap.keyset()  and Group.Type = 'Queue'];

	
	List<Lead> leadsToUpdate = [select id,CenterDistrict__c,CenterRegion__c, OwnerID from Lead where OwnerID IN :Trigger.NewMap.keyset() OR OwnerID IN  :dm.userOrGroupId];
	System.debug(leadsToUpdate);
	List<Lead> leads = new List<Lead>();
	
    for(Lead Leadz : leadsToUpdate) {
		leadz.CenterDistrict__c = Trigger.NewMap.get(Leadz.OwnerID).District__c;
		leads.add(leadz);	
	}
	update leads;
}
FB AdminFB Admin
Raj, Steven,

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:
 
trigger UpdateDisReg on Account (after update) {

        Account a = Trigger.New[0];
        String alias = 'a.center__c';
        System.debug(a);
        List<Lead> leadsToUpdate = [select id,CenterDistrict__c,CenterRegion__c from Lead where Owner_Alias__c = 'alias'];

        System.debug(leadsToUpdate);
        List<Lead> leads = new List<Lead>();
        
    for(Lead Leadz : leadsToUpdate) {
         leadz.CenterDistrict__c = a.District__c;
         leads.add(leadz);
         {
       
       update leads;
}
}
}

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