0

How can we upsert records to glocal picklist values from trigger?

I wanna get existing records from a custom object and upsert the records to glocal picklist value by trigger. What type of trigger should I use? (before update, after update or something else?)

1

You can't do that from a trigger, strictly speaking. You cannot modify most metadata in your org without making a callout to either the Tooling API or the Metadata API, and you cannot make a callout from a trigger.

Your trigger would need to enqueue some asynchronous functionality to even be able to make this callout, such as a Queueable or Future method. However, I would generally advise against trying to do this in a triggered way. For one thing, most of your ordinary users won't have the right permissions to call the Tooling or Metadata API at all (you'd have to use a Named Credential authenticated as a privileged user to authorize the callout). For another, you run the risk of confusing and potentially incorrect behavior should multiple records be modified in parallel. Your code would have to be very carefully structured to ensure that each change is applied in order and that you don't have race conditions that could overwrite parallel changes.

I would suggest instead either (a) building a Scheduled Apex class to run on a nightly schedule, scheduled as a privileged user, to perform this synchronization activity, or (b) building a custom UI to allow privileged users in your org to initiate it at the time of need.

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.