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
Tad MatjusenkoTad Matjusenko 

Trigger to prevent account deletion if 2 pick-lists have certain values

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.

 
Banwari KevatBanwari Kevat
Hi Tad, 
    Please try with this code
 
trigger preventSigAccDeletion on Account (before delete) {
        for(Account acc : trigger.old){
            if( acc.Contact_type__c.contains ('Signatory')
             && acc.Application_status__c.equalsIgnoreCase.('Approved')){
           
            
            acc.adderror('Signatory Accounts cannot be deleted. Please contact administrator if you need it to be deleted or merged');
}
}


Thanks,
Banwari
Deepali KulshresthaDeepali Kulshrestha
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
Tad MatjusenkoTad Matjusenko
@Deepali and @Banwari

Thank yo ufor your help, unfortunately not of the codes worked.

Benwari your code gives an error: There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger preventSigAccDeletion caused an unexpected exception, contact your administrator: preventSigAccDeletion: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.preventSigAccDeletion: line 4, column 1". 

Deepali your code did not work, maybe it is because Contact_type__c is a multi select picklist and not a text field.

Thank you one more time.
Tad MatjusenkoTad Matjusenko
Maybe I need to clarify, error should be shown only when Contact_type__c CONTAINS 'Signatory' AND Application_status__c EQUALS 'Approved'
Contact_type__c is a Multi select Picklist
Application_status__c is a standard Picklist

Hope that helps
Banwari KevatBanwari Kevat
Hi Tad,
  Can you please check that the Application Status should not be blank or null. I am assuming that The Apllication Status will always have the value.

Thanks,
Banwari
Skype: bkevat92
Tad MatjusenkoTad Matjusenko
Hi Banwari,

Application status for some types of Accounts can be left blank.

Thank you
Banwari KevatBanwari Kevat
Hi Tad, 
please try with below code
trigger preventSigAccDeletion on Account (before delete) {
        for(Account acc : trigger.old){
            if( acc.Contact_type__c.contains ('Signatory')
             && acc.Application_status__c!= null && acc.Application_status__c.equalsIgnoreCase.('Approved')){
           
            
            acc.adderror('Signatory Accounts cannot be deleted. Please contact administrator if you need it to be deleted or merged');
}
}



let me know if its not working.

Thanks,
Banwari
Tad MatjusenkoTad Matjusenko
Hi Banwari, unfortunately it does not work isn't there an easier vay to validate picklist value? something li ISPICVAL in formulas?
Banwari KevatBanwari Kevat
Hi Tad, 

   Can you please send the screeshot of account record. Please!

Thanks,
Banwari
Tad MatjusenkoTad Matjusenko
User-added image

Account Type is a multi select picklist (its API name is Contact_type__c)
Status is a standard picklist (its API name is Application_status__c

Thank you
Tad MatjusenkoTad Matjusenko
Thank you both. I figured it out. First of all I wqas using wrong field.

Code which worked:

trigger preventSigAccDeletion on Account (before delete) {
        for(Account acc : trigger.old){
            if( acc.Contact_type__c.contains ('Signatory')
             &&  acc.Status__c == 'Approved'){
           
            
            acc.adderror('Signatory Accounts cannot be deleted. Please contact administrator if you need it to be deleted or merged');
}}}