Custom Trigger Setup
Prerequisites
To set up custom triggers, you need the following permissions:
-
Minimum permissions for the FieldFX modules you are using
-
System permissions:
-
Customize Application
-
View Setup and Configuration
-
Basics
FieldFX features a number of standard triggers that get installed with FieldFX packages.
| Refer to the FieldFX Managed Packages article for more information. |
You can add custom triggers to extend standard functionality.
TDTM (Table-Driven Trigger Management) for Custom Triggers
-
The global abstract class
TDTM_TriggerHandlerin the FX5 managed package provides a hook for custom triggers. -
If a custom trigger handler apex class extends
TDTM_TriggerHandler, some minimum setup allows custom codes to plug into theFX5 TDTMtrigger flows. -
Triggers execute in a specified order.
Adding a Custom Trigger
|
The examples are for illustration only, and illustrate adding a trigger to the |
Code a Global Trigger Handler Class
-
Make the Apex class global.
-
The name must end with
TriggerHandlerand must not exceed 40 characters (excluding theTriggerHandlersuffix).For example,
LFW_JobTriggerHandler.The class name is used to set the Label field of Custom Metadata
FX5__TDTMTrigger__mdt.For a class called
LFW_JobTriggerHandler, the Label for itsFX5__TDTMTrigger__mdtisLFW_Job.Do not use a lengthy Apex class name. The class name must be shorter than 40 characters +
TriggerHandler. -
Trigger handler Apex class must extend
FX5.TDTM_TriggerHandleror implementFX5.ITDTM_TriggerHandler. -
Apex classes authored by FieldFX by ServiceMax personnel should use a prefix, such as
LFW. This helps the support team identify if a trigger is authored by a FieldFX employee. -
Example:
global with sharing class LFW_JobTriggerHandler extends FX5.TDTM_TriggerHandler { global LFW_JobTriggerHandler() { this.setSobjectType('Job__c'); this.addTriggerOp(TriggerOperation.BEFORE_INSERT); //this.addTriggerOp(TriggerOperation.BEFORE_UPDATE); //this.addTriggerOp(TriggerOperation.BEFORE_DELETE); //this.addTriggerOp(TriggerOperation.AFTER_INSERT); //this.addTriggerOp(TriggerOperation.AFTER_UPDATE); //this.addTriggerOp(TriggerOperation.AFTER_DELETE); //this.addTriggerOp(TriggerOperation.AFTER_UNDELETE); } global override void onBeforeInsert(List<sObject> items) { LFW_JobService.initJobs((List<FX5__Job__c>)items); } //global override void onBeforeUpdate(List<sObject> objs, Map<Id, sObject> oldmap) {} //global override void onBeforeDelete(List<sObject> objs) {} //global override void onAfterInsert(List<sObject> objs) {} //global override void onAfterUpdate(List<sObject> objs, Map<Id, sObject> oldmap) {} //global override void onAfterDelete(List<sObject> objs) {} //global override void onAfterUndelete(List<sObject> objs) {} } //The service class doesn’t need to be global, public will work. public with sharing class LFW_JobService { public static void initJobs(List<FX5__Job__c> jobs) { System.debug(JSON.serializePretty(jobs)); } }
Enable Apex Setting
-
From Setup, go to Custom Code → Apex Settings.
-
Enable Deploy Metadata from Non-Certified Package Versions via Apex.
-
Click Save.
Run Script
-
Access the Developer Console.
-
Copy and run the following script.
System.debug(FX5.TDTMTriggerLauncher.RefreshTDTMTriggers(true)); -
This script creates a
FX5__TDTMTrigger__mdtentry for each trigger operation at the custom trigger handler Apex classes.
Modify the Trigger Execution Sequence
-
From Setup, go to Custom Code → Custom Metadata Types.
-
Click Manage Records next to TDTM Trigger.
-
Click Edit next to the TDTM Trigger to modify.
-
Change the Sequence Number to the desired value.
Smaller numbers execute earlier.
Unless required, leave the Managed Package Trigger Handler’s Sequence Number at 10. If you need a trigger to run before a managed trigger, give it a value less than 10. If you need it to run after a managed trigger, give it a value greater than 10.
-
Click Save.
Set Active, Allowlist, and Blocklist
-
ALLOWLIST: comma separated list of user login names
Trigger always runs even if the
TDTMTrigger__mdtis inactive -
BLOCKLIST: comma separated list of user login names
Trigger at
TDTMTrigger__mdtentry does not run for specified users -
ACTIVE:
falseTrigger does not run except for users in ALLOWLIST