+ Start a Discussion
ruchika Nayyarruchika Nayyar 

Write a trigger, only system admin user should be able to delete the task.

how to write this code??
buggs sfdcbuggs sfdc
hi

Why dont you remove the delete button on task layout for other profiles,and make it visible for only system admin profile.
ruchika Nayyarruchika Nayyar
but i want to write A code in trigger??
Navee RahulNavee Rahul
Hi Ruchika,

the below will work
 
trigger DeleteCheck on Task (before delete) {
    Id profileid=Userinfo.getProfileId();  
  profile profilname=[select Name from Profile where id=:profileid];
    for(Task accountDuplicate:Trigger.old){
        if(profilname.Name!='System Administrator'){
          accountDuplicate.addError('No Access for Deletion');                                    
      }
                            
    }
    
}
Thanks
D Naveen Rahul
 
ruchika Nayyarruchika Nayyar
thnk you sir
but how i check it after login from user??
Navee RahulNavee Rahul
sorry could not able to understand.
what you need to check ??
buggs sfdcbuggs sfdc
HI ruchika

Just try to login with other user and create a task and try to delete the tas, then you can see the following error message 'No Access for Deletion' as per the trigger.