In order to call a business policy immediately when a record is created or updated, you need to:

  • Set up an APEX trigger to call the Decisions on Demand API for the appropriate object type and event
  • Activate the trigger from the policy detail page

A business policy will only be invoked if the Decisions on Demand API is called for the relevant trigger event and that event is enabled on the policy detail page. The latter step allows you to control the activation of a business policy with a few clicks at any time without making APEX code changes.


Only one APEX trigger is needed per object type, but multiple policies can be active for the same object. 


Setting up an APEX trigger

API calls

The following methods are used to apply the Decisions on Demand business policies from a trigger:

  • On insert
    String triggerName = 'My trigger';
    Map<Id, SObject> scope = trigger.newMap;
    Map<String, String> results = 
    DecsOnD.TriggerExecutionHandler.executePolicySObjectInsert(scope, triggerName);
  • On update
    String triggerName = 'My trigger';
    Map<Id, SObject> scopeOld= trigger.oldMap;
    Map<Id, SObject> scopeNew = trigger.newMap;
    Map<String, String> results = DecsOnD.TriggerExecutionHandler.executePolicySObjectUpdate(scopeNew, scopeOld, triggerName);
    
    
  • On delete
    String triggerName = 'My trigger';
    Map<Id, SObject> scope= trigger.oldMap;
    Map<String, String> results =  DecsOnD.TriggerExecutionHandler.executePolicySObjectDelete(scope, triggerName);
    
    


Notes:

  • These calls can be integrated into an existing trigger, or you can create a standalone trigger for Decisions on Demand
  • The policy will be invoked asynchronously -- any actions will only be applied after the trigger completes
  • In all cases, the API should called in the after event -- API calls from before triggers are ignored, as are calls made from unit tests or non-trigger contexts
  • You can filter the scope parameters passed in to these API methods to exclude records that do not need to be processed by Decisions on Demand

  • The triggerEvent parameter is used to distinguish the trigger in logs -- it is optional
  • The return value will be null on successful invocation.
    If there are errors, it will contain a map of record IDs to the error messages returned for those records
    For a global failure, the message will be stored under the GLOBAL key


Trigger events

You only need to call the API for the events that actually need to trigger your policy. But we recommend including the code for all three events (insert, update and delete), to avoid having to make APEX changes if you later decide to enable those events. The overhead of calling the API if no policies are active for that event is low.


Related objects

If your policy needs to be triggered on insert, update or delete of related objects, you should create a trigger for those objects as well. For instance, if you have an Account Assignment policy, and you want to trigger a reassignment when a new opportunity is created for an account, you would need to activate a trigger for the insert event on the Opportunity object. Decisions on Demand will automatically determine the relevant Account from the Opportunity.


See the Administrator Guide for more details, including a complete code example for a standalone trigger.


Activating triggers for your business policy

Setting up the trigger in the step above will call the Decisions on Demand API whenever relevant insert, update or delete events occur. This can result in the invocation of zero or more business policies.

To activate and configure trigger-based invocation for a specific policy, go to the Triggers section on the policy detail page. There, you can select the objects and events that should trigger a re-evaluation of the rules. You can also indicate in which cases you would like an Execution record to be created, and whether to send you an email on trigger failure.


For instance, to invoke a policy only on creation of a new Lead, configure the triggers as follows:


Or to invoke a policy only on creation or update of an account, as well as any associated Opportunities, use the following settings: