Search code examples
triggerssalesforceapexapex-codeapex-sosl

How to create a picklist field value using trigger when new record is added?


Apex code to create a record picklist in an object when a new member is created in another object?


Solution

  • Picklist value sets cannot be mutated directly in Apex. You would have to make a callout to the Metadata or Tooling APIs in order to achieve this, which cannot be done in a trigger, so you'd have to use Asynchronous Apex.

    Making automated changes to metadata is a complex and somewhat risky implementation. Because the running user of the trigger in many cases will not have permission to call those APIs at all, you'll likely have to use a Named Credential-based approach to authorize as a specific System Administrator.

    This implementation does have security considerations and involves a lot of complexities and considerations around change management. It should be carefully considered by the org's architecture stakeholders.