> 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/types/execute-apex.md).

# Execute Apex

### Generic Global Action properties

This action has generic properties, as other global actions:

* **Name** - give Action a name in order to search for it easy, otherwise system will use ID of this Action.
* **Type** (Rule or Action) - this is mandatory. Select "Action".
* **ID** - given by system after saving a new Action first time.
* **Status** - you can activate or deactivate an action temporary (e.g., for debug).
* **Context Object Type** - a data source which fields can be used in Action [conditions](/automations/queries-and-conditions/working-with-conditions/conditions-overview.md) and properties.
* **Action Type** - main property which determines what Action will do and it's configuration. [Explore all action types here](/automations/global-actions-and-action-groups/types.md).
* **Execute** - specify if Action will be executed always or when some conditions are matched.&#x20;

<figure><img src="/files/Wyr0epEhpQbqgP9w7cC9" alt=""><figcaption></figcaption></figure>

### Overview

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:

<figure><img src="/files/bRGsAgiOPLesRDlwbFgw" alt=""><figcaption></figcaption></figure>

#### Examples of using Work-Relay API

**Execute Action Group**

* Select **GlobalActionService** in API Class Name, select **ExecuteActionGroup** method:

<figure><img src="/files/y0hafAXuxa3yW35aKw9n" alt=""><figcaption></figcaption></figure>

* Populate **groupId** with Global Action Group Id:

<figure><img src="/files/Q7OXD1UcUqPR0nznVutK" alt=""><figcaption></figcaption></figure>

To get the Global Action Group Id go to the Actions -> Action Groups, open the action group you need and copy Id:

<img src="/files/vU8HMTFg7VnkME8I3GvD" alt="" class="gitbook-drawing">

* (Optional) populate **Source.**

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`:

<figure><img src="/files/G7C7L8VTSJAN9IvljU5E" alt=""><figcaption></figcaption></figure>

* (Optional) populate **variables.**

<figure><img src="/files/eN9Bd5Aqqzj8yCCBWyyz" alt=""><figcaption></figcaption></figure>

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 from the Process builder:

<figure><img src="/files/6phLM5DoHYbydBojwMIT" alt=""><figcaption></figcaption></figure>

#### **Configure background execution**

* Check **Execute in background** if you need to execute this action in other context than current user operation. See more info at [How to use "Execute in Background" feature](https://app.gitbook.com/o/0zHyyR0TFOv6bVLRE1y2/sites/site_4yKrA/s/kkIfnKKNSULyGZ085PIG/~/edit/~/changes/89/global-actions-and-action-groups/using-actions/how-to-use-execute-in-background-feature). If check this option, the new fields will appear:
  * **Wait for the result**: if check, system will wait for execution result before doing other things. In this mode no gaps (see below) available.
  * **With gap**: by default it is 0, but can be set as an integer number of minutes. This field specifies interval before action execution (for repeated actions - before each iteration).

<figure><img src="/files/Rpvws6d8hpOndSUwvvWf" alt=""><figcaption></figcaption></figure>
