> For the complete documentation index, see [llms.txt](https://docs.work-relay.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.work-relay.com/automations/global-actions-and-action-groups/service-backup-of-types/execute-apex.md).

# Execute Apex

Use this type of Action whenever you want to call a method from Work Relay API, or even your custom Apex class.

### Calling Work-Relay API

With **Work-Relay API** checkbox checked, you are offered to choose a class and a method from the API, and the list of parameters you should enter to run the method:

![](/files/3a7253c3865ca29399c56b88b723d7b354353d17)

#### Some examples of using Work-Relay API

**Execute Action Group**

* Select `GlobalActionService` in API Class Name (1), select `ExecuteActionGroup` method (2):

![](/files/d1303a72ea7edc2db552e36a70a5f62b1eba603d)

* Populate `groupId` with Global Action Group Id (3):

![](/files/e2f19e307d39354374caf7c02d5832ea05551835)

To get the Global Action Group Id go to the Actions -> Action Groups, open the Action group you need and grab Id (4):

![](/files/7614fdf2202e6006816eb8c3a925306cb6d97733)

* (Optional) populate Source (5):

In source you can pass a JSON array which contains sources for each action from your Action Group. There are 2 ways you can pass sources:

1. Pass Action IDs with record ID for each action:\
   `"{Action1_ID}":"{record1_Id}", "{Action2_ID}":"{record2_Id}",...`
2. Pass object name and record ID. All Actions which use specified object as a source will use specified record ID:\
   `"{Object1_API_Name}":"record1_Id", "{Object2_API_Name}":"record2_Id",...`

For example:\
if there are 4 actions in the group;\
3 of them use Account as source object;\
1 of them uses Contact;\
and you pass: `"Account":"{Account.Id}", "Contact":"{Contact.Id}"`in this case 3 actions will get the same `Account.Id` as source and 1 will get `Contact.Id`

![](/files/e0820c7d525d44998e4bd55c86b163c254d0d110)

(Optional) populate variables (6):

![](/files/7a0c5174b0e638adb5ce748a984bad19e3edacee)

See [API methods](/automations/api/api-methods.md) to know more.

### Calling your custom Apex

To call your custom Apex, put those calls inside of a class that will implement one of the Work Relay interfaces, `WR_BPM.WorkflowCallApexActionInterface`. Here's an example of such class:

{% code overflow="wrap" lineNumbers="true" %}

```apex
global with sharing class WorkRelayApexCalls implements WR_BPM.WorkflowCallApexActionInterface
{  
    public void run(sObject context, WR_BPM__Flow_Instance_Cursor__c flowInstanceCursor, String operationType, String parameter)
    {
        Map <String, String> params;
        if(parameter != null) params = (Map<String, String>)JSON.deserialize(parameter, Map<String, String>.class);
       
        if(params != null && params.get('callType') == 'doSomething')
        {
             String paramToDoSomething = params.get('paramToDoSomething');
             //your code for doing something    
        }
        else if (params != null && params.get('callType') == 'doSomethingElse')
        {  
             //your code for doing something else
        }
    }
}
```

{% endcode %}

This example implies that you are unifying usage of Work Relay calls of your custom code inside one class, `WorkRelayApexCalls`, but whether you want to have it this way or create multiple classes for logical separation, is up to you.

Parameters received by the `run` method are:

* `sObject context`: process instance's context record
* `WR_BPM__Flow_Instance_Cursor__c flowInstanceCursor`: cursor that "owns" the step in the instance
* `String operationType`: Proceed/Approve/Reject
* `String parameter`: whatever parameters passed from the action as JSON.

The screenshot below illustrates how this class's call is made (7) from the Process builder:

![](/files/bddbe6ff5e476beed2d26c5e00ba0c1c84cc8b8e)

### Miscellaneous

Check **Execute in background** checkbox (8) whenever you need to perform this call in asynchronous manner.
