Sunday 2 December 2012

VLOOKUP Function in Salesforce


It returns a value by comparing the field on the related object. It can be easily understand by following example.

Syntax:
VLOOKUP(field_to_return_from_lookup_object, field_to_compare_on_lookup_object, value_to_compare)

Suppose there is a situation in which you have to configure a validation rule to compare/verify the field value against/with the value store on the related object, then this can be achieved by VLOOKUP easily.

For ex:

Country(1st object):

  • Fields:


    1. Name(Text)
    2. Capital(Text)
    3. Population(Numeric)


Customer(2nd object):

  • Fields:


    1. Name
    2. Country(Text)
    3. Capital(Text)
    4. Country(Lookup/related object)

Now you can set a validation rule for "Capital" in the Customer object like below:

VLOOKUP($ObjectType.Country__c.Fields.Capital__c,$ObjectType.Country__c.Fields.Name,Country__c) != Capital__c

Then display a message "Capital is not correct, please enter a valid one."

Now when you will save the 'Customer' record the validation rule would be fired. It will take the value from 'Country__c' and compare with the record in the 'Country' object until it gets one. Then it will return the related 'Capital__c' value to the function from Country object record. If the returned value is not equal to the value entered in the 'Capital__c' field in 'Customer' object record, then it will show the message and record will not be saved.

1 comment: