Make validation rule easier to find

When writing apex tests many times I am getting validation errors especially when I am new on the project and don't know all rules yet.

If you get such an error you may spend some time to find which validation it is.

FIELD_CUSTOM_VALIDATION_EXCEPTION, You cannot change Contract Status when it's Released, Active or Cancelled. : [Contract Status] 

From this error message, you can understand that it is on object Contract but sometimes error messages don't tell too much.

Also, such errors may happen when a trigger, Flow or Process builder is updating related records then is even harder to understand which object it is.

 

To solve that name your validation rules with numbers like this:

VR007_Released_Status_cannot_be_changed

and in Error Message use code:

You cannot change Contract Status when it's Released, Active or Cancelled. [ServiceContract:VR007]

you will feel the difference on the first error and even better when you will get the first error screenshot from the end-user. Finding the rule will be much quicker.

Make validation rule easier to read

OK so you were able to find the failing validation rule quickly but why it fails. Let's check conditions:

(ISPICKVAL( PRIORVALUE(Contract_Status__c) , "Released")   && 
NOT(ISPICKVAL(Contract_Status__c, "Active")) && NOT(ISPICKVAL(Contract_Status__c, "Cancelled")) &&ISCHANGED(Contract_Status__c))
|| 
(ISPICKVAL(PRIORVALUE(Contract_Status__c), "Active")  &&  ISCHANGED(Contract_Status__c)  && NOT(ISPICKVAL(Contract_Status__c, "Overdue"))  && NOT(ISPICKVAL(Contract_Status__c, "Completed")) && NOT(ISPICKVAL(Contract_Status__c, "Cancelled")))
|| 
(ISPICKVAL(PRIORVALUE(Contract_Status__c), "Cancelled")  &&  ISCHANGED(Contract_Status__c)) &&  NOT($Permission.Admin_Lite_Access )

I hope you found all bracket pairs for ORs and ANDs to understand the logic in 10 seconds.

Let's try to format it. I know at least two ways to do that. First with Notepad++ (click here to read my article) second with ORGanizer plugin which is available for Firefox and Chrome. Its main purpose is to make it easier to log in to multiple SF orgs but also it contains a plugin for Formulas which will help us.

1. Just press [Enter] after opening bracket it will make an automatic indention.
2. Just after && press [Delete] so it will bring up the line below and press [Enter] again.
3. Repeat until you will find the closing bracket. Pressing [Enter] before bracket should bring it one line down and align to the opening bracket.

Check this animation.

Can you understand this in 10 seconds?

(
    ISPICKVAL(PRIORVALUE(Contract_Status__c), "Released") &&
    NOT(ISPICKVAL(Contract_Status__c, "Active")) &&
    NOT(ISPICKVAL(Contract_Status__c, "Cancelled")) &&
    ISCHANGED(Contract_Status__c)
) ||
(
    ISPICKVAL(PRIORVALUE(Contract_Status__c), "Active") &&
    ISCHANGED(Contract_Status__c) &&
    NOT(ISPICKVAL(Contract_Status__c, "Overdue")) &&
    NOT(ISPICKVAL(Contract_Status__c, "Completed")) &&
    NOT(ISPICKVAL(Contract_Status__c, "Cancelled"))
) ||
(
    ISPICKVAL(PRIORVALUE(Contract_Status__c), "Cancelled") &&
    ISCHANGED(Contract_Status__c)
) &&
NOT($Permission.Admin_Lite_Access)

I hope I made your life easier.

 

 

 

   
© Paweł Woźniak