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
Katerina ParkinsKaterina Parkins 

Validation rule - two conditions at the same time

Hello, I have created a validation rule which stop users linking one company's contacts to another company's account licence. However one record type 'IPAD Contact Licence' is excluded from this rule, all works fine and the formula currently looks like this:

AND( 
Contact__r.AccountId <> Company_Licence__r.Company__r.Id,
RecordType.Name <> "IPAD Contact Licence" 
)

Now I have a challenge to include this record type in this restriction but not fully - the Contact Account ID doesn't have to be equal to the Company Licence Company ID, but needs to be within the company hierarchy so our users can attach a contact to a licence sitting under a different branch.

I'm not sure how to amend the formula to ensure all the other record types follow the existing validation rule and add the exception above.

Hopefully this makes sense. 

Thanks.

Katerina
Terence_ChiuTerence_Chiu
Do you already have a field on the account object that would indicate if two account records are part of the same hierarchy ?

 
Katerina ParkinsKaterina Parkins
We have a parent account field present on the account object. 
Terence_ChiuTerence_Chiu
The parent account field will only indicate the immediate parent of an account, however, to identify whether or not the account is part of the same hiearchy you will have to check the ultimate parent account. You can do so by creating a formula field that goes up the hierarchy to find the top level parent. One caveat is that this formula is restricted by character limitations so you can only go so far up the hierarchy. But if you have relatively small hierarchies this may work.

The below handles pulling the ultimate parent 5 levels deep into the hierarchy.
IF(ParentId == NULL, Id, 

IF( Parent.ParentId == NULL, Parent.Id, 


IF(Parent.Parent.ParentId == NULL, Parent.Parent.Id, 

IF(Parent.Parent.Parent.ParentId == NULL, Parent.Parent.Parent.Id, 

IF(Parent.Parent.Parent.Parent.ParentId == NULL, Parent.Parent.Parent.Id, 

IF(Parent.Parent.Parent.Parent.Parent.ParentId == NULL, Parent.Parent.Parent.Parent.Id, Id) 

) 

) 


) 

) 

)

So if the above formula field is called Ultimate_Parent__c then you can modify your validation rule to look similiar to the below example.
 
OR(

AND( 
Contact__r.AccountId <> Company_Licence__r.Company__r.Id,
RecordType.Name <> "IPAD Contact Licence" 
),

AND(
Contact__r.Ultimate_Parent__c <> Company_Licence__r.Company__r.Ultimate_Parent__c,
RecordType.Name == "IPAD Contact Licence" 
)

)