+ Start a Discussion
girbot56girbot56 

Validation Trigger to Prevent Delete

Hi all,

 

I have been looking into using a trigger to prevent Case deletion, the reason being that delete is required in order for the users to be able to mass update Cases from list views but we don't want any Cases deleted.

 

I'm not a developer by trade but my current trigger is below. It works as intended but I wanted to check if this is the best approach/ask for opmitisation advice, e.g. the best way to bulkify.

 

Thanks in advance.

 

trigger trg_CasePreventDelete on Case (before delete) {
//Trigger to prevent deletion of Cases other by profiles named below  
   
	String profileId=userinfo.getProfileId();
	String profileName=[Select Id,Name from Profile where Id=:profileId].Name;
	system.debug('ProfileName'+profileName);
    
	if(profileName != 'System Administrator')
    
	for (Case CasetoPrevent: trigger.old)
     
		{
        		CasetoPrevent.addError('You do not have permission to delete Cases');
		}
}

 

Kiran  KurellaKiran Kurella

The trigger looks good to me but you can acheive this functionality without  a trigger. You can simply remove the "Delete" permission for all the required profiles.

girbot56girbot56

Thanks for the reply. The reason I am not using the profile/object permission are that the users are required to be able to mass update the Case status from the Case list view. I believe when this is activated the profile must have the delete permission.