If you have a requirement to limit the number of child records possible in a 1:N relationship, the Dynamics Rollup field is a handy way to achieve the restriction.
This solution has three parts:
- Two entities with 1:N relationship to each other.
- Rollup whole number field on parent entity configured to use the COUNT function which counts the child records.
- On create workflow on child entity with a condition to evaluate the value of the rollup field and stop record creation if it exceeds the allowed record count.
To Implement
(1) Create the rollup field on the parent entity.
Data Type = whole number
Field Type = rollup
Click the Edit button to configure the rollup.
You can also add a filter to limit the function to only count child records which meet a specific criteria; like, have a certain eye color.
(2) Create a workflow on the child entity. Configure it to run “on create” and as the very first step evaluate the value of the rollup field. If it exceeds the number of records allowed, “Stop” the workflow as CANCELLED and configure an error message to display to the user.
TIP: Add a step to call a forced calculation of the rollup at the end of the workflow. This ensures it has the correct value at all times. A plug-in to force a calculation can be found in Dynamics 365 Workflow Tools.