TransWikia.com

Validation Rule or Apex trigger To Allow Only One Checked Record based on the master record ID

Salesforce Asked by Avinash babu on December 26, 2021

We have two objects Assets and Employees with a Master-Detail Relationship (Where Asset is the Master and Employee is the Detail sObject). For Every asset that we have, at a given time only one employee will be assigned to an asset. In the same-way, over a period of time an asset may change hands and different employees might use the same asset.

On each child record(Employee record) we have a checkbox called “Provisioned” which if checked means that the asset is allocated to a particular employee. Once if a child record has this checkbox checked then the IT Admin shouldn’t be able to assign the same asset to other employee unless he un-check the check box and then assign it to new Employee. That means one Asset (Master-Object) record can have so many Employee (Detail) records but only one among those records should have the checkbox field “checked” (meaning the asset was provisioned to only one person at a time).

SO how can i address this issue? I don’t see a possibility to do it using a validation rule.I want to know how can i accomplish this requirement using an apex trigger. This is very critical to us.

Can anyone help me out with this issue.

Thanks.

2 Answers

You can add a roll-up summary field to your Asset object, counting the number of child Employee records that have their Provisioned checkbox set to TRUE.

A validation rule on Employee can then traverse the relationship to the selected Asset and evaluate the count of "Provisioned" child records before allowing the transaction to complete successfully.

The validation rule will be evaluated before the roll-up summary field is updated by the current transaction, so if the count is < 0 the validation rule can prevent the update. (Order of execution documentation)

Answered by Brian Piggins on December 26, 2021

I would recommend that instead of trying to have your Employees check all the other employees on update, add a Lookup to employee on your Asset called "Assigned To" (or something similar), then on the employee you can have a lookup checkbox which has the formula

Asset__r.Assigned_To__c = ID

This also means that you can easily find the employee which has been assigned the asset from the asset itself, rather than having to search the related employees. It implicitly enforces only allowing assets to be assigned to 1 employee or is unassigned, because lookup fields only lookup one object, or are null.

In the case that the above is not viable, you could write a trigger that ran before insert/update by retrieving all the employees from assets that are masters of an employee being updated/inserted, then if the employee is being provisioned check if any of the detail employees of the master asset are already provisioned.

Answered by IllusiveBrian on December 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP