Start an Action from an Apex Class

DAP Action

Last published at: 2023-06-16 09:28:53 UTC
Delete

Starting an Action from an Apex Class is available for the Premium and PDM licenses.

Start any DAP action from an Apex Class. The actions are provided as macros, reducing the need for configuration. The class will invoke a flow that runs the DAP action on the selected records.
Delete

This article is about starting a DAP Action in an Apex Class. For running an Apex Class as a DAP Action, see Run An Apex Class action‍.

In an Apex Class, you can add methods to apply a DAP action to a single record, or to a collection of records. The DAP action is preconfigured in a Macro‍, to make sure it can be run straight away without any further configuration.

A DAP action applied to a collection of records will be executed as a DAP Job. Its progress can be tracked on the DAP Job Overview page, but you can also add a method to the class to retrieve the job status.

Prerequisites

To start a DAP Action from an Apex Class, you need:

- The DAP Action  you want to run, preconfigured in a Macro. 

  • The macro should be intended for the same object as the records called in the class.
    Some macros can be used for all objects (e.g. macros based on Mass Delete), but most are set up for one specific object.
  • Enable the macro for Start from an Apex Class for each applicable profile or user. In the Action Library‍, click Assignment to change this.
  • Note down the API name of the macro. You can find the macro API name in the Action Library‍. The macro API name always starts with "macro-".

Read more about creating Macros here: Macros‍.

Run DAP Action on a single record

Define the following parameters in the method:

input.flowInvokerReference = 'yourClassName' The name of your class will be shown in the DAP record audit log (if enabled) to indicate what invoked the flow that executed the action.

input.dapMacroApiName = 'macro-yourMacro' The API name of the macro, that you noted down earlier (see Prerequisites).

input.recordId = Id.valueOf('a0P17000005cSNgEAM') The ID of the record that will be passed on to your macro. The object type of this recordId should be the same object type as configured in the macro.

The method for running a DAP Action on a single record will then be:

plauti.dapApexApiV1.RunMacroOnRecordInput input = new plauti.dapApexApiV1.RunMacroOnRecordInput();
input.flowInvokerReference = 'yourClassName';
input.dapMacroApiName = 'macro-yourMacro';
input.recordId = Id.valueOf('a0P17000005cSNgEAM');

plauti.dapApexApiV1 dapApexApi = new plauti.dapApexApiV1();
try {
    plauti.dapApexApiV1.RunMacroOnRecordOutput output = dapApexApi.runMacroOnRecord(input);
} catch (plauti.dapApexApiV1.dapApiInternalException e) {
    System.debug('Internal DAP exception. Please contact plauti suport. ' + e.getMessage());
} catch (plauti.dapApexApiV1.dapApiExternalException e) {
    System.debug('External DAP exception. Please check the provided input. ' + e.getMessage());
} catch (Exception e) {
    System.debug('Exception. Please contact your SalesForce admin. ' + e.getMessage());
}

Run a DAP Job on a collection of records

Note that the action can process a maximum of 1560 record IDs.

Define the following parameters in the method:

input.flowInvokerReference = 'yourClassName' The name of your class will be shown in the DAP record audit log (if enabled) to indicate what invoked the flow that executed the action.

input.dapMacroApiName = 'macro-youMacro' The API name of the macro, that you noted down earlier (see Prerequisites).

input.recordIds = new List<Id>{
    Id.valueOf('a0P17000005cSNgEAM'),
    Id.valueOf('a0P17000005cSNgEAM')
};
IDs of the records that will be passed on to your macro.
The object type of these recordIds should be the same object type as configured in the macro.
The list should contain a maximum of 1560 record IDs.

The method for running a DAP Action on a collection of records will then be:

plauti.dapApexApiV1.RunMacroJobOnRecordsInput input = new plauti.dapApexApiV1.RunMacroJobOnRecordsInput();
input.flowInvokerReference = 'yourClassName';
input.dapMacroApiName = 'macro-youMacro';
input.recordIds = new List<Id>{
    Id.valueOf('a0P17000005cSNgEAM'), 
    Id.valueOf('a0P17000005cSNgEAM')
};

plauti.dapApexApiV1 dapApexApi = new plauti.dapApexApiV1();
try {
    plauti.dapApexApiV1.RunMacroJobOnRecordsOutput output = dapApexApi.runMacroJobOnRecords(input);
    Id dapFlowId = output.dapFlowId;
} catch (plauti.dapApexApiV1.dapApiInternalException e) {
    System.debug('Internal DAP exception. Please contact plauti suport. ' + e.getMessage());
} catch (plauti.dapApexApiV1.dapApiExternalException e) {
    System.debug('External DAP exception. Please check the provided input. ' + e.getMessage());
} catch (Exception e) {
    System.debug('Exception. Please contact your SalesForce admin. ' + e.getMessage());
}


The dapFlowId output, Id dapFlowId = output.dapFlowId , can then be used to fetch the status of the DAP Job.

Get the DAP Job status

When you started an action on a collection of records, it will be executed as a DAP Job. Its progress can be tracked on the DAP Job Overview page, but you can also add a method to the class to retrieve the job status. Use the dapFlowId output from the previous method for this.

Define the following parameter in the method:

input.dapFlowId = 'My Apex DC Job'; The ID of the DAP Flow, received from RunMacroJobOnRecordsOutput

The method for fetching the DAP Job status will then be:

plauti.dapApexApiV1.GetDapJobStatusInput input = new plauti.dapApexApiV1.GetDapJobStatusInput();
input.dapFlowId = 'My Apex DC Job';

plauti.dapApexApiV1 dapApexApi = new plauti.dapApexApiV1();
try {
    plauti.dapApexApiV1.GetDapJobStatusOutput output = dapApexApi.getDapJobStatus(input);
} catch (plauti.dapApexApiV1.dapApiInternalException e) {
    System.debug('Internal DAP exception. Please contact plauti suport. ' + e.getMessage());
} catch (plauti.dapApexApiV1.dapApiExternalException e) {
    System.debug('External DAP exception. Please check the provided input. ' + e.getMessage());
} catch (Exception e) {
    System.debug('Exception. Please contact your SalesForce admin. ' + e.getMessage());
}

Note that you can also use the dapFlowId from a DAP Job that was started from a flow, as outlined in Start an Action in a Flow‍.

Results

The Single Record method returns the record ID and a summary of what was done, and it will state whether running the macro succeeded or failed:

Parameter
Type
Content
recordId
ID
The ID of the record that was passed on to the macro.
summaryLine
String
A summary of what was done by the macro.
success
Boolean
An indication whether the macro execution succeeded or not.


The DAP Job method for a collection of records returns the ID of the flow that executed the macro.

Parameter
Type
Content
dapFlowId
ID
The ID of the flow that executed the macro. Use this ID in the 'Get DAP Job Status' method.


The Get DAP Job Status method returns data about the DAP Job status. Note that the DAP Job status can also be tracked on the Job Overview page in DAP.

Parameter
Type
Content
dapFlowId
ID
The ID of the flow that executed the macro you are fetching the Job status for.
startTime
Long
The starting time of the DAP Job (in Unix Epoch time)
endTime
Long
The ending time of the DAP Job (in Unix Epoch time)
status
String
The current status of the DAP Job, e.g. aborted, failed, running, etc.
isFinished
Boolean
A check whether the DAP Job is finished: is the status equal to 'finished' yes or no.


Furthermore, the Audit Log (if enabled) and the Job Information on the Job Overview page (if a DAP Job action was used) will show that the macro was started from your class via a flow, with the class name you entered.