---
schema_version: "1"
id: 2889ee4f516c775267dcc61e065238e459f78de1527e4d308185ee0445c7b610
title: Associate Actions to Records with Apex
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/service-cloud/salesforce_guided_engagement/guides/guided-engagement-setup-process-apex.md
canonical_url: https://developer.salesforce.com/docs/service/salesforce-guided-engagement/guide/guided-engagement-setup-process-apex.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 5bceede9cabafb669f9e798690b375aa6cbb9cb62c331be83e1f5a33631e0c8a
products:
  - Lightning Flow for Service Developer Guide
  - Service Cloud Developer Content
---

# Associate Actions to Records with Apex

If you want to control how you trigger the creation of a RecordAction, you can use Apex to associate actions to records. The RecordAction object is exposed as a standard object in Apex. You can trigger it before a DML operation or on delete or undelete. You can also provide custom error handling.

**note:** RecordAction is available in API version 42.0 and later.

Here are some scenarios that Apex better accommodates.

-   Triggering before a DML operation rather than after
-   Triggering on delete and undelete DML operations
-   Validating data before the action is run
-   Custom error handling
-   Partial completion rather than complete failure


## Example
This example uses an Apex class and trigger pair to associate a flow to a newly created account that satisfies specific criteria. In the class, a method is defined that takes in a list of accounts and creates a RecordAction for each of them. It sets the new account as the RecordId and the ActionDefinition as an active flow. The trigger is called after the insert of an account record. When the criterion (type is Customer) is satisfied, the class method is executed, and it adds the defined flow to the new account.

**Apex Class**

```apex
public class RecordActionHandler {
    public static void addNewCustomerFlow(Account[] accts) {
        RecordAction[] recordActions = new List<RecordAction>();
        for (Account a : accts) {
            RecordAction ra = new RecordAction(RecordId=a.Id, ActionDefinition='New_Customer_Flow', Order=1, ActionType='Flow');
            recordActions.add(ra);
        }
        
        try {
            insert recordActions;
        } catch (DMLException e) {
            System.debug('An unexpected error has occurred: ' + e.getMessage());
        }
    }
}

```

**Apex Trigger**

```apex
trigger RecordActionTrigger on Account (after insert) {
    Account[] customerAccounts = new List<Account>();
    for (Account a : Trigger.new) {
        if (a.Type == 'Customer') {
            customerAccounts.add(a);
        }
    }
    RecordActionHandler.addNewCustomerFlow(customerAccounts);
}

```

**See Also**  


-   [*Apex Developer Guide*](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dev_guide.htm)
