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 

need help bulkifying this trigger


trigger SSSUpdate on User (before Update)
{

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

u1.SSS__c= u.UserId;
}
}
Daniel B ProbertDaniel B Probert
trigger SSSUpdate on User (before Update){

Set<id> userIds = new Set<id>();
    for (User u : Trigger.new)
        userIds.add(u.Id);   

Map<id, User> users = new Map<id, User>([SELECT UserId, User.Name FROM UserTeamMember where TeamMemberRole= 'Sales Support Specialist' 
AND OwnerId=:u.ID]);

for (user u1 : Trigger.new)
        u1.SSS__c = users.get(u.Id).id;
}
}
try this
sfdcChi2sfdcChi2
got this error: Error Error: Compile Error: Variable does not exist: u.ID at line 8 column 14
Daniel B ProbertDaniel B Probert
doh

trigger SSSUpdate on User (before Update){
Set<id> userIds = new Set<id>();
    for (User u : Trigger.new)
        userIds.add(u.Id);  
Map<id, User> users = new Map<id, User>([SELECT UserId, User.Name FROM UserTeamMember where TeamMemberRole= 'Sales Support Specialist'
AND OwnerId in: userids]);
for (user u : Trigger.new)
        u1.SSS__c = users.get(u.Id).id;
    }
}


sfdcChi2sfdcChi2
haha. i did that already. also noticed on line 8 you have u1.sss__c instead of u.SSS__c which i corrected. however it prompted me to change the map before i can save to Map<id,UserTeamMember>. that allowed me save the code but when i tested it it gave me a null pointer exception on line 8
Daniel B ProbertDaniel B Probert
ok i think that's again a typo on my part 

users.get(u.id).id; is wrong

it should be

users.get(u.id).userid;

or you need to update line 5 select command to include ID.

Cheers
Dan
sfdcChi2sfdcChi2
thanks Dan. but still having the null pointer exception
Daniel B ProbertDaniel B Probert
try removing the get(u.id)

and leave it as users.userid

this may be something odd with the userteammember object that i've not come across before as the code i've given you is near identical to what I use on my accounts and opportunities..


sfdcChi2sfdcChi2
Thanks Dan. it's definitely something with the UserTeamMember object as i also have a similar  code on the opportunity object that works just fine. but cant get it to work trying to retrieve a value on the utm object from the user object. tried your suggestion but still no luck. Thanks for your time Dan. really appreciate