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
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.
Prasanya KPrasanya K
hi navee rahul
im getting error while executing the code