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
F SmoakF Smoak 

before delete trigger to prevent deleting a specific record type of an object

Hi,

I have a custom object A and record types A1,A2,A3 
I want to write a before delete trigger and it's supporting test class so that it prevents deleting of A1 record type records but can delete A2,A3 record type records.
Please help!
Pankaj MehraPankaj Mehra
Hi F,

It would be something like this :
Trigger ObjectATrigger on ObjectA (Before Delete) {


    for(ObjectA a : Trigger.Old) {
    
        if(a.RecordTypeId != A1.RecordTypeId) {
         a.addError('You Cannot delete this A1 Record type record!!!'); 
       }
    }
}

 
F SmoakF Smoak
Hi Pankaj,

I did try to use the above code and it is giving me validation error on A2 and A3 deletion whereas when I am deleting A1 records it gives me Insufficient privileges error.
Can you please help?
 
Pankaj MehraPankaj Mehra
Hi F,

Just update the if condition to be
 
if(a.RecordTypeId == A1.RecordTypeId)

so when recordtype is equal to A1 , it will prevent, To sort out Insufficient privileges error check the following:

1. Record is accessible by you (In case Private sharring see if the record is shared with you)
2. See if the record type is accessible on your user's profile.
3. You have delete permission on A1.

 
F SmoakF Smoak
Hi Pankaj,

I tried after updating IF condition and for sorting out privileges error below observations:

1. Record is accessible by you (In case Private sharring see if the record is shared with you) ----- Record is PRIVATE and user profile has CRUD access on Object
2. See if the record type is accessible on your user's profile.----  Yes, because the user creates the records and needs access to delete the same records.
3. You have delete permission on A1.--- A1 is record type is there some way to give delete permission on record type?

Please help!

Thanks,
FSmoak