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
Mike @ BlackTabMike @ BlackTab 

Do Not Allow A Specific User To Run A Trigger

Inside of a trigger i'm trying to get information about the user updating the record. Is this possible? 

 

Here is what my trigger looks like:

 

trigger ContactTriggers on Contact (after insert, after update) {
	
	if(Trigger.isAfter) {
		ContactTriggers.updateAccountLastCallAttemptFields(Trigger.new);
	}
	
}

 Here is what I need:

 

trigger ContactTriggers on Contact (after insert, after update) {
	
	if(Trigger.isAfter && [RunningUser != 'a user') {
		ContactTriggers.updateAccountLastCallAttemptFields(Trigger.new);
	}
	
}

 

Best Answer chosen by Admin (Salesforce Developers) 
TheIntegratorTheIntegrator

You can use the LastModifiedById field

 

trigger ContactTriggers on Contact (after insert, after update) {
User blocked = new User();
try{
blocked = [Select Id From User Where Name = 'a user'];
}catch(Exception e){
}
if(Trigger.isAfter && blocked.Id!=null && Trigger.new[0].LastModifiedById != blocked.Id) {
ContactTriggers.updateAccountLastCallAttemptFields(Trigger.new);
}

}

 

this code assumes that trigger.new has multiple records, they are modified by the same user. Also, if a list of users need to be used, the blocked variable could be made a set and comparison done using contains.