0

I am new to triggers, and I have tried creating a trigger to prevent deleting account if :

  1. a string field Ninja_store_ID is NOT blank
  2. a Boolean field (check box ) is not checked (False)

The trigger I created below is preventing the deletion but not checking either of the criterias, because we want to allow the acc deletion if one of the criterias is false.

so if the string field is blank allow deletion OR if the boolean field is TRUE, allow deletion

Here is the demo trigger:

trigger Demo 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'); 
        } 
    } 
}
1
  • Hi, Could you please modify the if loop as follows and then check if you are getting same result. Also check the default of the checkbox if it is checked or unchecked. if( !Acc.deleted__c && !String.isBlank(Acc.Ninja_Store_Id__c) ) { acc.AddError('not able to delete'); }
    – Kalpana
    Jul 5, 2021 at 11:47

1 Answer 1

1

I think your trigger is pretty close to what you're looking for, you just don't have the correct conditions in your if statement.

Right now, your code says "prevent deletion (by adding an error) when deleted__c is false and Ninja_Store_Id__c is not an empty string"

An empty string is not the same as blank. Salesforce doesn't store completely empty strings '' in SObject records (except, and instead converts the value to null. null != '').

Salesforce gives us String.isBlank() and String.isNotBlank() to handle such situations.

Also, <boolean value> == true can be reduced to simply <boolean value>. Likewise, <boolean value> != true can be reduced to !<boolean value>.

So given what you've told us, your if statement should be
if(!acc.deleted__c && String.isNotBlank(acc.Ninja_Store_Id__c))

Running through the various possible scenarios, writing down what they are and what result they give (not what you want to happen, what actually does happen given the code you have) can be a helpful tool. If I were to do that, this is what that would look like:

  • deleted__c == true
    • then the first condition is false. false && anything = false, so we don't need to evaluate the other condition
    • don't enter the if block.
    • Deletion is allowed
  • deleted__c == false, Ninja Store is blank.
    • we satisfy the first condition
    • Ninja Store is blank, so the second condition results in false
    • true && false = false, so we don't enter the if block
    • Deletion is allowed
  • deleted__c == false, Ninja Store has a value
    • Both conditions are satisfied, and we enter the if block
    • Deletion is not allowed
3
  • Thanks a million , seems to be working perfectly , to be able to push this trigger to production I need 75% test coverage, any advise? Jul 5, 2021 at 12:20
  • @ShadiRabie Questions on writing tests are common enough here that we created some questions and answers under the canonical-qa tag. They'll go over the basics and point you to some good resources (like the trailhead modules on unit testing). Also, if your problem has been resolved, you should accept the answer that you think is most correct (or helped you the most). It gives both you and the answerer reputation points, and signals to the community that your problem has been resolved.
    – Derek F
    Jul 5, 2021 at 12:33
  • @DerekFDev I am trying :) this is my first post here, thanks for the tip and the advise! Much appreciated Jul 5, 2021 at 12:40

Not the answer you're looking for? Browse other questions tagged .