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
JAckerman09JAckerman09 

Trigger is Failing Validation Rule, Can't Figure Out Why

My test class runs this code:

 

test.starttest();
        insert Css;
        
        Css[1].Disbursement_Destination__c = Null;
        Css[3].Disbursement_Destination__c = Null;
        Css[5].Disbursement_Destination__c = Null;
                                
        Css[1].Existing_or_New_Disbursement_Destination__c = 'Use/Create a New Disbursement Destination';
        Css[3].Existing_or_New_Disbursement_Destination__c = 'Use/Create a New Disbursement Destination';
        Css[5].Existing_or_New_Disbursement_Destination__c = 'Use/Create a New Disbursement Destination';
        update Css;
        system.debug('Updated Css');
        test.stoptest();

  which is causing this validation rule to fail:

 

AND( 
OR( 
ISPICKVAL( Existing_or_New_Disbursement_Destination__c , 'Leave Disbursement Destination Blank'), 
ISPICKVAL( Existing_or_New_Disbursement_Destination__c , 'Use/Create a New Disbursement Destination') 
), 
Disbursement_Destination__c <> Null)

 despite the fact that the code (attempts to) set the value of Disbursement_Desintation__c to Null.

 

Any insight as to why this validation rule is failing?

 

Thanks!

tomcollinstomcollins

So, the validation rule evaluates to AND( OR( false, true), false), right?  And that's false.

 

When you say the rule is failing, are you saying that it's allowing the records to be updated?

 

I'm still learning the platform, but is "<> Null" treated the same as "!= Null"?

 

Have you tried changing the rule to be just the ISPICKVAL OR and then just the Null check to isolate which of the two tests is failing?

JAckerman09JAckerman09

When I say failing, I meant that it is NOT allowing the records to be updated, but because test case sets the field to Null, it should ALLOW the record to be created.

 

As far I know, <> and != are the same.

 

Thanks!

tomcollinstomcollins

Is your AND/OR logic correct?

 

It sounds like the field can only be NULL if the pick is one of two values.  So, a non-null Disbursement Destination is always valid, and a null destination is OK for both of the pick vals, right?  If that's the case, then it should be a three-way OR instead of the outer AND.

 

OR( 
  OR( 
    ISPICKVAL( Existing_or_New_Disbursement_Destination__c , 'Leave Disbursement Destination Blank'), 
    ISPICKVAL( Existing_or_New_Disbursement_Destination__c , 'Use/Create a New Disbursement Destination') 
  ), 
  Disbursement_Destination__c <> Null
)
JAckerman09JAckerman09
The OR tests a different field than the outer AND. One is a picklist and the other is a lookup to another object.
tomcollinstomcollins

That's fine.  Maybe I don't understand the business logic.  Aren't you trying to handle the following:

 

rowValid = (Disbursement_Destination__c != NULL)

   || (Existing_or_New_Disbursement_Destination__c == 'Leave Disbursement Destination Blank')

   || (Existing_or_New_Disbursement_Destination__c == 'Use/Create a New Disbursement Destination')

 

So, that looks like all OR logic to me -- no AND.

JAckerman09JAckerman09
The reason that I think I need the AND is because if one of the current OR statements are true, but the AND is false, eg. AND(OR(false, true),false) the statement will be FALSE and the record will validate, whereas if it? was all OR statements OR(false, true, false) would evaluate to TRUE and the record would not validate. Does that make sense?
tomcollinstomcollins

Sorry, I forgot that validation rules need to be TRUE to indicate an error, not to indicate a valid record.  The problem with your logic is that the inner OR is returning TRUE instead of FALSE when either of the pickvals is selected (the proper logic for your business rule).

 

Use either of the following:

 

NOT( OR( 
  ISPICKVAL( Existing_or_New_Disbursement_Destination__c, 'Leave Disbursement Destination Blank'), 
  ISPICKVAL( Existing_or_New_Disbursement_Destination__c, 'Use/Create a New Disbursement Destination'),
  Disbursement_Destination__c != Null
))
AND( 
  NOT( ISPICKVAL( Existing_or_New_Disbursement_Destination__c, 'Leave Disbursement Destination Blank')), 
  NOT( ISPICKVAL( Existing_or_New_Disbursement_Destination__c, 'Use/Create a New Disbursement Destination')),
  Disbursement_Destination__c == Null
)

 Generate an error if the pick val is not "leave blank" AND it is not "create new" AND the destination is Null.