0

I have ~25 checkboxes fields on my Opportunity object that I'm using in a trigger. If one of these fields is updated, I need to trigger an update on a corresponding field in a custom object that corresponds to the checkbox.

I've got the trigger working, but only by hardcoding in the relevant trigger values into the script. (I could add these values to an Apex class to make things a bit cleaner.)

Hardcoding this list works, but it requires a redeploy of my code every time I need to add a new checkbox. Ideally, I'll be able to tag these 25+ checkbox fields so I can dynamically select them going forward, without modifying my code at all.

How might I go about doing this?

1
  • It'll be hard to suggest improvements if you don't share the code you currently have.
    – Derek F
    Commented Apr 1, 2019 at 1:35

1 Answer 1

3

Field Sets

You can add the checkbox fields to a Field Set on the Opportunity object. By accessing Schema.FieldSet records via the Describe API, your code can access these lists, which can be updated without a code deploy.

Schema.FieldSet fs = Schema.SObjectType.Opportunity.fieldSets.MyTriggerFieldSet;

Then, iterate over the results of getFields() on that field set.

Custom Metadata Types

If your code requires more information about mapping Opportunity fields to custom object fields, consider using a Custom Metadata Type. These records can be deployed from org to org as metadata, but can be updated by an admin.

Your CMT could store field names in both objects. You query them in your trigger like any other sObject, but it won't cost against your limits.

Note that both approaches, due to their dynamism, can complicate your unit tests. In many cases you'll need to use dependency injection to make your code resilient against changes.

1
  • This is great - thank you!!
    – snark17
    Commented Apr 1, 2019 at 20:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .