0

I'm developing a Managed Package and would like to write a static class with a corresponding method that takes a collection of SObject records (can be of varying SObjectTypes) as the argument, and produces a list of records always of a specific SObjectType if conditions are met (e.x. will always be Custom_Object__c).

The objects that will invoke / call the static class method are going to be determined by configuration based records that admins add to the system.

For example they may specify that Leads, Opportunities, and Accounts make trigger the class method. However, this will always be dependent on the objects in the tenant org.

Without adding object specific triggers in the package, is there any recommended way to dynamically have these specified objects call my class method? I know packages like DLRS dynamically generate triggers in Production environments, but I'm wary of that approach and would like to see if there's a better path.

4
  • I don't really understand the question or problem you're trying to solve. It sounds like you want your managed package class automatically invoked in subscriber org on unknown objects without having to add any logic to that org.
    – Nick C
    Sep 9, 2022 at 1:42
  • I'll reword the problem - it's a bit difficult to convey. I'd want my managed package class automatically invoked in the subscriber org for objects that the client specifies in records. So basically, I have a configuration object that I'm exposing to admins of the app, they create records that specify the applicable sobject types that should call the class method. Then I need to figure out based on the configuration records (let's just say the admin has records for Lead, Opportunity, Contact) how I can have DML events for those objects call my class method.
    – Tom Kline
    Sep 9, 2022 at 1:48
  • 1
    I think I understand what you mean. Something in the subscriber org has to invoke your class, so you're going to need to add a trigger or flow in the subscriber org. Even with a config table, you still need an event to fire that your managed package code listens to, regardless of the config table.
    – Nick C
    Sep 9, 2022 at 1:59
  • Thanks @NickCook appreciate it. I was afraid that was likely the case.
    – Tom Kline
    Sep 9, 2022 at 11:40

1 Answer 1

2

There's no way to do this at present without altering the configuration of the subscriber org. Triggers must be associated with exactly one object, defined statically at deployment time.

You have a few options, but they're all relatively more complex than what you're hoping. For example, you might:

  • Dynamically deploy triggers like DLRS does (works great, but risky).
  • Provide a global Apex method and instruct your subscribers to build their own triggers to call it (lower risk, but far higher subscriber effort).
  • Provide a Flow template that invokes your managed Apex (requires manual subscriber intervention, you may need to build a complex UI to report on the current config status).
  • Write scheduled Apex instead of real-time, and execute dynamic queries based on your configuration table.
1
  • Thank you David, much appreciated. I'll likely end up using a combination of flow templates and scheduled apex based on your response. I appreciate the detailed response!
    – Tom Kline
    Sep 9, 2022 at 11:42

You must log in to answer this question.

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