1

My custom object and User object, both are having a custom field named as State, I want to share custom object records with only those users who match with the State of the custom object's record. I believe it is not possible with Criteria-based sharing, I know it is to be done with the help of Apex Managed Sharing, by Writing a trigger. Can anyone pls tell me how would I compare the users to share the records with my apex code?

0

I would recommend you do this using the following strategy:

  • Use a trigger that first writes the record Ids you want to share to a custom object.

  • A 2nd trigger then fires when the records are written to the custom object.

  • After checking available limits, the 2nd trigger calls either a queuable or a schedulable to process the records in the custom object (both have different limits with the schedulable being called if the flex queue is >=99 or if there are too many being queued >4 & <100 scheduled jobs, otherwise abort).

  • Now, use the queueable or the schedulable to run a query on the records that have been sent to the custom object to compare them with your partner user records. If you find matches, create the share records.

  • Once you've created the records successfully, as part of your finish method, delete the records in your custom object. If you weren't successful, write an error message to the custom object to retry them again once unless it was a fatal error that you know isn't related to concurrence, etc.

  • Send an email to the admin with your results, esp regarding errors they may need to intervene manually, including the error messages that you'll have saved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.