Quantcast
Viewing latest article 10
Browse Latest Browse All 20

Filtered Lookups, Validation Rules, and Order of Execution

Reading the cheatsheet for Filtered Lookup (beta), I noticed an interesting line:

Lookup filters function similarly to validation rules when you save a record. That is, actions that cause related records to save, such as changes to a roll-up summary fields, also trigger the lookup filters on the related record and block the save.

The implications for this are massive. Let's explore two examples:

Example 1: Filter as Validation Rule from Parent Record

  • We create a lookup on a Child object to Parent.
  • We filter the lookup to EXCLUDE Parent.Status = 'Closed' (Parent.Status is only Open or Closed.)
  • We can edit the Child records as long as the Parent Status is not Closed.
  • When Parent.Status is changed to Closed, existing related Child records are not affected...
  • BUT if we attempt to edit a Child when the Parent is Closed, Force.com will throw an error (which we can customize) beause that the Lookup is invalid.
  • (and clearly we cannot add new Child records either)

Conclusion: Thus, Filtered Lookups act much like Validation Rules. A quick experiment shows that Filtered Lookup errors actually fire before Validation Rules.

Example 2: Filter as Validation Rule on Roll-Up Summary (from Child Record) - what the line above was referencing

  • Use the above example, but change the lookup to a master-detail relationship
  • Create a Roll-Up Summary field to count all child records
  • Prevent saving more than 10 child records for one parent record

Here, we have triggered a filter error without touching a parent record, yet we throw an error based on a value on the parent record.

This second example is significant because we could already prevent more than 10 child records from saving, but doing so required a Roll-Up Summary field on the parent object AND a Validation Rule on the child object. Now we can replace the Validation Rule with the Lookup Filter, though we still need the Roll-Up Summary field. Whether or not this simplifies things is definitely up for debate...

Conclusion

This is a very powerful feature! Thanks to salesforce.com for rolling it out, even in beta form.

Real world example: The above example would be great for Time Sheet Entry and Time Sheet Header objects, as they would create, in effect, a validation rule on the Header record preventing editing of any child records. Awesome!

For further reading, check Salesforce Help's Lookup Filters examples.


Viewing latest article 10
Browse Latest Browse All 20

Trending Articles