+ Start a Discussion
Guru Prasad 31Guru Prasad 31 

how to run trigger for particular profiles

Run trigger for only few profiles only
eric talleyeric talley
Triggers always run, if you want to limit to certain profiles you need to put conditional logic in the trigger that checks if the current user profile is part of the profile set you want the code to run. 
 
if(profiles.contains(CurrentUser.Profile)){
 //execute logic
}

 
Ashif KhanAshif Khan

Hi Guru,

There is no direct way to execute trigger for particular profiles. But there is a workaround which you can implement in your code.
Workaround is as follows.
Create a custom setting, where you will define profiles for which your trigger will execute.
Allowed Profile For Trigger custom setting of List type, Store each profile as a record in this custom setting. You can store profile name.
Then In your Apex trigger, before doing anything first confirm that current user's profile is present in custom setting or not?
If present then
go with your logic.

In trigger 
  • you have to get current user's profile name.
  • Grab all Profiles from custom setting in a set.
  • Check if current profile exist in custom setting profiles then write you further logic.

Thanks
Ashif


 
Guru Prasad 31Guru Prasad 31
how to run trigger for particular Users
Ashif KhanAshif Khan
Hi Guru,

Trigger will fire for all profiles but you can run your particular code as per profile for which you want to run. If user profile exists in the custom setting your code will run otherwise not. You need to put this condition in your trigger code.

Regards
Ashif