+ Start a Discussion
Angela SchloederAngela Schloeder 

Trigger only for specified profile?

Is it possible to have this trigger only for specified profiles?


trigger EventDelete on Event (before delete)

    {

     if (Trigger.isDelete) {

           for (Event sb : Trigger.old)

      {

             sb.addError('Cannot delete an Event');

      }

     }}
Best Answer chosen by Angela Schloeder
Angela SchloederAngela Schloeder
This is what ended up being the solution... Thank you everyone for your help, it is very much appreciated.

trigger EventDelete on Event (before delete) {
    if([SELECT PermissionsModifyAllData FROM Profile WHERE Id = :UserInfo.getProfileId()].PermissionsModifyAllData) {
        return;
    }
    for (Event sb : Trigger.old) {
        sb.addError('Cannot delete an Event');
    }
}

All Answers

Rajiv Bhatt 16Rajiv Bhatt 16
Here is the solution: 
 
trigger EventDelete on Event (before delete)

    {
  
 // you could query for mulitple profiles in the query here and update the if condition accordingly.  
  Profile userProfile = [SELECT Id FROM Profile WHERE Name='Marketing User']; 
if(userProfile.Id != UserInfo.getProfileId()) 
​return;
 
     if (Trigger.isDelete) {

           for (Event sb : Trigger.old)

      {

             sb.addError('Cannot delete an Event');

      }

     }}

 
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Angela Schloeder,

Ravi bhatt has given one solution or you don't give CRUD permission to that profile if needed.

Let us know if it helps you.
Angela SchloederAngela Schloeder
OK, I have several profiles, is it easier to give access to one profile, (Sys Admin)?
Rajiv Bhatt 16Rajiv Bhatt 16
Strangely I could not find CRUD permission for events object and hence the trigger solution.
ManojjenaManojjena
Hi Anjela,

You can create class and add one static method from where you can return the profile name and use that inside trigger to check for which all profiles you need to throw error .

Please try with below code it will help .
 
//Class
public class DataAccessLayer{
   public static string getCurrentUserProfileName(){
        return [SELECT Name FROM Profile where id = :Userinfo.getProfileId()].name;
    }
}

//Trigger 

trigger EventDelete on Event (before delete) {
  if(DataAccessLayer.getCurrentUserProfileName =='System Administator' || DataAccessLayer.getCurrentUserProfileName =='Some Other Profile Name'){ 
	​if (Trigger.isDelete) {
			for (Event evt : Trigger.old) {
				evt.addError('Cannot delete an Event');
			}
		}
	}
}

Thnaks 
Manoj
Angela SchloederAngela Schloeder
This is what ended up being the solution... Thank you everyone for your help, it is very much appreciated.

trigger EventDelete on Event (before delete) {
    if([SELECT PermissionsModifyAllData FROM Profile WHERE Id = :UserInfo.getProfileId()].PermissionsModifyAllData) {
        return;
    }
    for (Event sb : Trigger.old) {
        sb.addError('Cannot delete an Event');
    }
}
This was selected as the best answer