[SalesForce] How to compare two fields of two different objects in validation rule

I Have two custom objects(obj1 and obj2) and two fields(Obj1field and Obj2field) in these custom objects.Now I would like to write a validation rule in obj1, which compares the two fields.I tried the below one, but there is an error.

Enter_Required_Quanity__c >=    $ObjectType.StorageObject__c.Fields.Quantity_Left__c 

When I tried this the following error occurred.

Error: Incorrect parameter type for operator '>='. Expected Number, received Object

Best Answer

First of all, the Validation Rule executes upon a DML event on an object. For argument's sake, we'll assume that object is Obj1__c

So, as long as Obj2__c is a parent object (via master-detail or lookup) to Obj1__c, then the Validation Rule would look like

Enter_Required_Quantity__c > Obj2__r.Quantity_Left__c

Obj2__r is the relationship name from Obj1__c to Obj2__c

Now, if Obj2__c is not related to Obj1__c via a master or lookup relationship (parent/grandparent/great-grandparent or greatgreat-grandparent), then you can't do the Validation Rule.

Validation Rules can't look down to children nor look up and then down to siblings.

Related Topic