Authored by Zishan Razzaq

Tuesday, December 23, 2008

How to Create a Validation Rule in Salesforce.com(Force.com)

What Are Validation Rules in Salesforce.com:
From Salesforce.com Premier Support Rep:
"Validation rules verify that the data a user enters in a record meets the standards you specify before the user can save the record. A validation rule can contain a formula or expression that evaluates the data in one or more fields and returns a value of "True" or "False." Validation rules also include an error message to display to the user when the rule returns a value of "True" due to an invalid value."

In simplest terms, some rule that you would like to do checks on your system, record.

So, Validation Rules are Available in: Group, Professional, Enterprise, Unlimited, and Developer Editions.

To create validation rules:
  • Go to the Object you would like to create the validation rule on. To get to that object click Setup and then Customize. If it is a standard object like Account, Contact, Opportunity, Product, etc.
  • If it is a Customize Object then click on Setup==> Create==>Objects and then click on the object you like.
  • Now click on Validation Rule and click New.
  • Give your Validation a Name
  • Give it a Description (not needed but helps in the long run and good practice)
  • Check the Active Box to make it Active
  • Error Condition Formula: This is where you place in your expression to validate the field. For example: Let say you want to validate that US States Field to have 2 abbreviations in it. The rule would be something like:
  • AND (
    OR(BillingCountry = "US", BillingCountry="USA", LEN(BillingCountry)=0),
    OR(
    LEN(BillingState) < 2, NOT( CONTAINS("AL:AK:AZ:AR:CA:CO:CT:DE:DC:FL:GA:HI:ID:" & "IL:IN:IA:KS:KY:LA:ME:MD:MA:MI:MN:MS:MO:MT:NE:NV:NH:" & "NJ:NM:NY:NC:ND:OH:OK:OR:PA:RI:SC:SD:TN:TX:UT:VT:VA:" & "WA:WV:WI:WY:PR:MP", BillingState) )))
  • Now in the Error Message Place in the Error you would like the User to see or get.
  • Like : A valid two-letter state code is required.
  • Error Location: Do you want to display the error on top of the page or next to the field itself. If you want it next to the field then click field and choose your field from the picklist.
  • Click Save to finish or Save & New to create additional validation rules.
Done...
Thanks
View my Other Blogs:
VisualForce Made Easy
Salesforce Data Migration Made Easy