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
System 181System 181 

Account deletion trigger

Hi All, 
I am new to creating triggers and struggeling with this trigger, before deleting an account record to check TWO criterias, 
if the field : acc.ninja_store_id is not blank  -  ( string )
if the a checkbox field : (deleted__c) is UNchecked -( boleean )  

when I modify the trigger as below, it works only on the condition (deleted) but not on the string is blank
so, I want the rigger to prevent the deletion if one of the criterias is met! 

here is the trigger:

trigger test on account

(before delete){

    for(Account acc: Trigger.old){

        if(Acc.Account_Status__c!='Deleted' || Acc.Ninja_Store_Id__c != '' ){

            acc.AddError('To be able to delete this account in SF, It has to be deleted first in Ninja');

        }

    }

}



 
mukesh guptamukesh gupta
Hi,

Can you debug Ninja_Store_Id__c, what value you are getting in debug

Let me know then
CharuDuttCharuDutt
Hii System 
Try Below Trigger
trigger test on account(before delete){
    for(Account acc: Trigger.old){
        if(Acc.deleted__c != true || Acc.Ninja_Store_Id__c != '' ){
            acc.AddError('not able to delete');
        }
    }
}
-------------OR-----------------
trigger test on account(before delete){ 
     for(Account acc: Trigger.old){ 
         if(Acc.deleted__c != true && Acc.Ninja_Store_Id__c != '' ){ 
             acc.AddError('not able to delete'); 
        } 
    } 
}
Please Mark It As Best Answer If it Helps
Thank You!

 
PriyaPriya (Salesforce Developers) 
Hi,
 

Check below example that matches your requirement :- 

 

https://developer.salesforce.com/forums/?id=906F0000000AW2HIAW

https://salesforce.stackexchange.com/questions/267416/trigger-for-preventing-account-deletion-with-related-opportunities

Please mark it as the Best Answer so that it can help others in the future.

Regards,

Priya Ranjan


 
Suraj Tripathi 47Suraj Tripathi 47
Hi,

Please follow the below code:-
  trigger test on account(before delete){ 
  if(trigger.isbefore && trigger.isDelete){
     for(Account acc: Trigger.old){ 
         if(Acc.deleted__c != true && Acc.Ninja_Store_Id__c != '' ){ 
             acc.AddError('not able to delete'); 
        } 
    } 
  }
}



Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi


 
System 181System 181
Thanks all, the triggers above same as mine are preventing the account deletion even if account is not meeting the criteria, the trigger is preventing deletion no matter what are the records values! 
So in my testing, if the account ninja status is blank it is still preventing me from deletion. Same as if the deleted check is false.