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
Sourav PSourav P 

Validation rule for the age gap field while editing or cloning

Dear All,
I have a below field, Age main, on a custom object " Quote", The " Main Driver" field is replicating from the account name ( personal account).
User-added image

The age & other details also get replicate from the account, i.e here the age 23 is maintained in the account " Chimni", But the user have the flexibility to edit the age on the same page or while cloning the page to get a new quote. But i need to set a validation rule where , the user can set only one year gap from the maintained one ( i.e he can edit it as either 23 or 25), it should show validation error if its 22 or 27 etc. If you can help me out in setting. Thnx

I tried to write the below VR but its showing syntax error, and failing.
 
(Age_MainD__c) <> OR(PRIORVALUE(Age_MainD__c)+1, PRIORVALUE(Age_MainD__c)-1)


Alternatives : 

Age_MainD__c <> PRIORVALUE(Age_MainD__c)+1
OR Age_MainD__c <> PRIORVALUE(Age_MainD__c)-1

 
Best Answer chosen by Sourav P
Shun KosakaShun Kosaka
Hi,
Use less than and greater than.
OR(Age_MainD__c < PRIORVALUE(Age_MainD__c)-1, Age_MainD__c > PRIORVALUE(Age_MainD__c)+1 )
Be careful if you use <> in OR condition because it means union in set theory.
For example, if prior value of the age is 20.
Age_MainD__c <> PRIORVALUE(Age_MainD__c)+1 is {...,18,19,20,22,23,...}  (all numbers except 21)
Age_MainD__c <> PRIORVALUE(Age_MainD__c)-1 is {...,18,20,21,22,23,...}  (all numbers except 19)
OR of above two set is {...,18,19,20,21,22,23,....} (all numbers)
So it didn't work well.

All Answers

Shun KosakaShun Kosaka
Hi,
Use less than and greater than.
OR(Age_MainD__c < PRIORVALUE(Age_MainD__c)-1, Age_MainD__c > PRIORVALUE(Age_MainD__c)+1 )
Be careful if you use <> in OR condition because it means union in set theory.
For example, if prior value of the age is 20.
Age_MainD__c <> PRIORVALUE(Age_MainD__c)+1 is {...,18,19,20,22,23,...}  (all numbers except 21)
Age_MainD__c <> PRIORVALUE(Age_MainD__c)-1 is {...,18,20,21,22,23,...}  (all numbers except 19)
OR of above two set is {...,18,19,20,21,22,23,....} (all numbers)
So it didn't work well.
This was selected as the best answer
Sourav PSourav P
Thanks Shun for the explanation.