Write a trigger with its test class for only system admin user should be able to delete the task.

....Trigger....

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');                                    
        }
        
    }
}

....Test Class....

@istest
public class DeleteCheckTest  {
    @istest
    static  void TestAccountTrigger(){
        Task Tcc = new Task();
        Tcc.Subject =  'Email';
        Tcc.Priority =  'Low';
        Tcc.Status = 'in progress';
        insert Tcc;
        
        Id profileid=Userinfo.getProfileId();
        profile profilname=[select Name from Profile where id=:profileid];
        if(profilname.Name!='System Administrator')
        {
            Tcc.addError('No Access for Deletion');
        }
        else{
            delete Tcc;
        }
    }
}

Comments

Popular posts from this blog

Write a trigger and it's Test class on Account when Account is update check all opportunity inside the account.