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
GobbledigookGobbledigook 

UserInfo in a trigger

Hey all,

 

I'm trying to write a trigger that takes the current user's profile id and assign a recordtype automatically depending on the profile, for data filtering processes in the future, but I was wondering how the UserInfo would actually run during a bulk fire.  Who would be the Current User in a bulk trigger?  Since triggers fire in bulk of 200 each, and since the UserInfo would take the current User, and since the trigger itself can fired by multiple Users, what would happen to the getProfileId() method itself? Would it even fill?  

 

I can't really test this since I don't have multiple Users trying to fire the trigger in bulk, hence me coming to the boards.

Cory CowgillCory Cowgill

During a Bulk Fire, it will be the same as if it was a Single User. I.E. It will be the user who requested the DML Operations.

 

For example, if you run Data Loader and try to load 1000 Account records as User A, the UserInfo will retrieve User A Information.

 

For example, if you run a DML Statement which inserts a list of Account Records as User A, the Userinfor will retrieve User A Information.

 

If this is an issue, you may want to consider using the OwnerID, Assigned To ID, or some other field on the record which points to a User to allow the filtering to take place instead of using the UserInfo object.

GobbledigookGobbledigook

But from what I understand the Bulk Fire of triggers wasn't tied to any one user.  For example, if you have 5 Users enacting the trigger within a few cycles of one another (which happens often in large operations), does that mean that it takes the UserInfo of the last User who added to the stack before the trigger was called?

Cory CowgillCory Cowgill

If you have 5 Users enacting a save operation on a Account object, that should invoke 5 seperate trigger executions. It doesn't "Bulk" those operations into one trigger context to my knowledge. I've never seen that occur.

 

I only see multiple records in a Trigger when someone performs a Bulk API operation, a Bulk DML statement, or run via Data Loader (which runs Bulk API behind the scenes I think).

 

You could test this out by System.runAs() during a unit test method and creating serval different users during the testMethod to try and test this out.

 

http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods