I have a requirement that user can't add child records once the parent records is submitted for approval. In the approval process, parent record gets locked but I also want to prevent child records to be created.
This mechanism will work for both master detail and lookup without resorting to code:
- Create a new checkbox field on the parent record of 'Locked' or similar with a default value of unchecked.
- Add an initial submission action on your approval process that sets
the value of 'locked' to true
- Add a validation rule to the child record that checks isnew() and the value of the parent's locked field and blocks the the save if it is true.
The advantage to handling this via a validation rule is that you can give the user a friendly error message and attach it to the lookup/master detail field if you want.
Preventive Solution :
Add a picklist field such as Approval Status on the parent. When the parent is submitted for approval, set this field to 'Submitted' or such as an initial approval action. (Reset to 'Approved' as final approval action)
Add a validation rule on child which checks the status of this field on the parent when child ISNEW() and the parent Approval Status is 'Submitted' to return appropriate error message.
Reactive Solution :
If it is a Master Detail relationship, add a simple cross object workflow on the child to update a field on the master when a child record is created (ISNEW())
If the Master is locked, this cross object workflow field update will return an error and prevent the child from being created. (I have a hunch creating a rollup summary formula also inhibit child creation, but I will need to validate this)
If however a lookup, a trigger to update the parent on create of child. Again this should trigger an error, which you can catch, format and invoke addError on the child record to return a more user friendly error message.
A submitted and locked master record prevents child objects being created if:
- The child records triggers a field update on the master OR
- The master has rollup fields related to the child
The first option is best but it should be an unconditional (onAllChanges) workflow or an unconditional
before insert before update trigger on the child.