You need to sign in to do that
Don't have an account?
Tad 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.
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.
Please try with this code
Thanks,
Banwari
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
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.
Contact_type__c is a Multi select Picklist
Application_status__c is a standard Picklist
Hope that helps
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
Application status for some types of Accounts can be left blank.
Thank you
please try with below code
let me know if its not working.
Thanks,
Banwari
Can you please send the screeshot of account record. Please!
Thanks,
Banwari
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
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');
}}}