Skip to main content
Hi all,

I am trying to write trigger which throws an error on account delete if Contact Type contains Signatory and Application Status is Approved.

Code works fine if I only try to write trigger based on Contact Type field, but when I try to add second condition it does nothing.

trigger preventSigAccDeletion on Account (before delete) {

        for(Account acc : trigger.old){

            if( acc.Contact_type__c.contains ('Signatory')

             && acc.Application_status__c == 'Approved')

           

            

            acc.adderror('Signatory Accounts cannot be deleted. Please contact administrator if you need it to be deleted or merged');

}

}

Thanks in advance.

 
11 answers
  1. Jan 17, 2019, 12:12 PM
    Hi Tad ,

    The reason you are not able to use your trigger for both the conditions is due to contains method that that is trying to compare null value of your Contact_type__c field.

    Simply make use of == operator to get result for the same. 

    Please try this simple code to get expected results.

    trigger ShowError on Account (before delete) { 

        for(Account acc : trigger.old){

            String str= acc.Contact_type__c;

           

            if(str=='Signatory' && acc.Application_status__c == 'Approved'){

                

                acc.adderror(':::Signatory Accounts cannot be deleted. Please contact administrator if you need it to be deleted or merged');

       }

            }

    I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

    Thanks and Regards,

    Deepali Kulshrestha
0/9000