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
sfdcChi2sfdcChi2 

I need help bulkifying this trigger


trigger SampleTriggger on User (before Update)
{

For (User u1 : Trigger.New)
{
UserTeamMember u =  [SELECT UserId, User.Name FROM UserTeamMember where TeamMemberRole= 'xxx' 
AND OwnerId=:u1.ID];

u1.SSS__c= u.UserId;
}
}
Best Answer chosen by sfdcChi2
sfdcChi2sfdcChi2
this finally worked for me. Many thanks to all you guys and a special thanks to you @ashi!

trigger SSSUpdate on User (before Update)
{

Map<Id,Id> mapUserTeamIdVsuserId = new Map<Id,Id>();
for(UserTeamMember u :  [SELECT id,UserId,ownerid, User.Name FROM UserTeamMember where TeamMemberRole= 'xxx' AND OwnerId IN:Trigger.New]){
  mapUserTeamIdVsuserId.put(u.OwnerId,U.UserId);
}

for(User u1 : Trigger.New){
  if(mapUserTeamIdVsuserId.containsKey(u1.id)){
   u1.SSS__c= mapUserTeamIdVsuserId.get(u1.id);
  }
}
}

All Answers

Ramesh KallooriRamesh Kalloori
Hi,

to overcome the recursive error. create anothe class as below.
public class LocationControl{
public static boolean inFutureContext = false;
}
modify the code below.

trigger SampleTriggger on User (before Update)
{
if(!LocationControl.inFutureContext)
{
For (User u1 : Trigger.New)
{
UserTeamMember u =  [SELECT UserId, User.Name FROM UserTeamMember where TeamMemberRole= 'xxx' 
AND OwnerId=:u1.ID];
LoactionControl.inFeatureContext=true;
u1.SSS__c= u.UserId;

}
}
}

thanks,
Ramesh

Pavan Kumar KajaPavan Kumar Kaja
Try below Code. This will work for bulk data.


trigger SampleTriggger on User (before Update){
	Map<Id,Id> mapUserTeamIdVsuserId = new Map<Id,Id>();
	for(UserTeamMember u =  [SELECT id,UserId, User.Name FROM UserTeamMember where TeamMemberRole= 'xxx' AND OwnerId IN:Trigger.New]){
		mapUserTeamIdVsuserId.put(u.UserId,u.id);
	}

	for(User u1 : Trigger.New){
		if(mapUserTeamIdVsuserId.containsKey(u1.id)){
			u1.SSS__c= mapUserTeamIdVsuserId.get(u1.id);
		}
	}
}




sfdcChi2sfdcChi2
@ Ramesh!-will the code as written previously cause a recursive error?

@ashi- thanks. but when i copied this code i got the following error and cant seem to fix it :) - Error: Compile Error: expecting a semi-colon, found ')' at line 3 column 135
Pavan Kumar KajaPavan Kumar Kaja
Hi typo error in the above code.
find the below updated code and try ths one.
trigger SampleTriggger on User (before Update){
	Map<Id,Id> mapUserTeamIdVsuserId = new Map<Id,Id>();
	for(UserTeamMember u : [SELECT id,UserId, User.Name FROM UserTeamMember where TeamMemberRole= 'xxx' AND OwnerId IN:Trigger.New]){
		mapUserTeamIdVsuserId.put(u.UserId,u.id);
	}

	for(User u1 : Trigger.New){
		if(mapUserTeamIdVsuserId.containsKey(u1.id)){
			u1.SSS__c= mapUserTeamIdVsuserId.get(u1.id);
		}
	}
}


Ramesh KallooriRamesh Kalloori
If the recurence occured that will be hadled by the below class.

public class LocationControl{
public static boolean inFutureContext = false;
}
sfdcChi2sfdcChi2
@ashi - the code does not change the SSS__c Field on the user object. there must be some kind of error somewhere. Mine works but i cant seem to figure out what is not working right with yours. please if you can, kindly take a look at it again. thanks.
Pavan Kumar KajaPavan Kumar Kaja
Hi SfdcChi2,

the code is working i have tested.

let me know where its not working fo ryou

connect 
pavanthetech@gmail.com
sfdcChi2sfdcChi2
when i go to a user detail page and edit, it is supposed to look at the default opportunity team on the related list of the user's detail page and pick the member with TeamRole = xxx and then write the name of that member in the sss field located on the user detail page. it's not doing this. i just tested it again

sfdcChi2sfdcChi2
this finally worked for me. Many thanks to all you guys and a special thanks to you @ashi!

trigger SSSUpdate on User (before Update)
{

Map<Id,Id> mapUserTeamIdVsuserId = new Map<Id,Id>();
for(UserTeamMember u :  [SELECT id,UserId,ownerid, User.Name FROM UserTeamMember where TeamMemberRole= 'xxx' AND OwnerId IN:Trigger.New]){
  mapUserTeamIdVsuserId.put(u.OwnerId,U.UserId);
}

for(User u1 : Trigger.New){
  if(mapUserTeamIdVsuserId.containsKey(u1.id)){
   u1.SSS__c= mapUserTeamIdVsuserId.get(u1.id);
  }
}
}
This was selected as the best answer