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
Jackline MusyokiJackline Musyoki 

how to write a trigger to prevent a profile from deleting a change request

Hello Guys
am new to Apex code. I would like a trigger to prevent members of a profile from deleting  a change. I have come up with this code but i get an error. Could you help me refine it.
trigger RestrictDelete on BMCServiceDesk__Change_Request__c (before delete) {

String ProfileId = UserInfo.getProfileId();  
for (Change_Request a : Trigger.old)      
            
IF(ProfileId!='profileid'))
{
     a.addError('You cannot delete this record!');
     
            }
}

 
Manoj jenaManoj jena
HI Jackline,
Try with below code it wil help !!
 
trigger RestrictDelete on BMCServiceDesk__Change_Request__c (before delete) {
	String profileName=[SELECT id,Name FROM Profile WHERE Id =:UserInfo.getProfileId()].Name ;  
	for (Change_Request a : Trigger.old){      
		If(profileName=='Your ProfileName')){
			a.addError('You cannot delete this record!');
		}
    }
}
if you want you can store the profile name in a custom lable .
String profileNameDynamic=System.Label.LabelName;
If(profileName==profileNameDynamic)


Let me know if it helps !!
Thanks
Manoj
DavidGantDavidGant
Jackline,

Can I ask why a trigger is needed for this instead of just removing the delete privlege at the Profile-level (admin task)? If the limitation is that you cannot edit the standard Profile provided by BMC, then I would recommend cloning the Profile in question to create a custom version of the standard Profile and use the custom version for all users needing the access instead writing code to address this need. Hope this helps.

Thanks,
David
Jackline MusyokiJackline Musyoki
@David removing the delete privilege also removes the modify all permission which I want to maintain. I want the users in the profile to be able to do everything else except delete the changes.
DavidGantDavidGant
@Jackline makes sense. Unfortunately code may be your only option and in that case, Manoj's code above looks like it should work.
DavidGantDavidGant
@Jackline Another discussion I was just participating in has some more code that I know works to prevent deletion of Accounts, so perhaps it will help if Manoj's solution doesn't work. See https://developer.salesforce.com/forums/ForumsMain?id=906F0000000MH1YIAW
Jackline MusyokiJackline Musyoki
Hello Manoj, Thanks so much for trigger, I tested it and its working perfectly. Is there way we can only get the second line in the error message Best regards Jackline Musyoki
Jackline MusyokiJackline Musyoki
Hello Manoj, Thanks so much for trigger, I tested it and its working perfectly. Is there way we can only get the second line in the error message This is the error message displayed Delete failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Delete Failed. Please contact your Systems Administrator!: [] Jackline Musyoki