Thursday, August 18, 2011

Setting a child record to play a unique primary role to parent through config

There are many use cases where there is need to set one of the child record as primary and when another child record is updated as primary it should throw up an error saying that primary record already exists (similar to contact roles function). Many of  the developers would suggest an apex trigger solution But this can be achieved through configuration alone.

Its a simple 2 step process.

  1. Create a Roll up summary field on the parent object. It would roll up the count of the child records which have the primary checkbox set to true.
  2. Create a validation rule on this field if the number becomes more than 1 validation error will be thrown.
U are good to go now. When the child record is updated to primary rollup would happen. But when there is a primary record rollup will try to become 2 triggering the validation error thus preventing it from saving the record. But if the relationship between child and parent is lookup or u want user to be pointed to the primary record in the error message listen to developers :)