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?

  • If you can post some code we can probably give you a more detailed answer – Jon Hazan Oct 11 '12 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 '16 at 13:50
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.

| improve this answer | |
  • 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. – Michael Welburn Oct 11 '12 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 '12 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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.