3

I recently had to implement some logic in a trigger to stop users from being able to add Attachments to a locked record of a particular type, and now I need to be able to create a "loophole" for a certain user (essentially, admin power to override the trigger). What I can't figure out is how to determine within a trigger (that could have multiple items in it) who the user is that invoked the action. I'm currently running before insert/update/delete, and think I could leverage the modifiedby of after insert/update for those, but I don't see a good way to know anything about delete. Any ideas?

2
  • If you can post some code we can probably give you a more detailed answer
    – Jon Hazan
    Oct 11, 2012 at 14:59
  • In any case I need your code that you have written for your above mentioned problem. I'm new to SF it will be a great help.
    – Superman
    Jul 19, 2016 at 13:50

2 Answers 2

9

You can get the current Users Id by using

UserInfo.getUserId()

From there you should be able to retrieve any extra info about the User that you need using queries.

2
  • For clarification, every time a trigger is invoked, is it only one user's "batch" of updates? I wasn't sure if a trigger could be invoked with updates from multiple users at once -- if its one user per trigger invocation, this seems like the easy solution. Oct 11, 2012 at 16:04
  • A Trigger is called separately for each Users actions. Even if two users do the same action at the same time you will get one invocation for each :)
    – Jon Hazan
    Oct 11, 2012 at 16:06
2

Another trick is to add a custom field on User (potentially a Checbox) - ByPassValidations__c or such. You can then check the User using UserInfo.getUserId() and query to see if the User has this checkbox set. This is slightly more scalable if you need to apply this for multiple users.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .