---
schema_version: "1"
id: c881f5ccaf4bff6a223fd4037297f9366c0752daf0e05334e0e3c181bbd23ccc
title: runApex() for Lightning Experience
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/service-cloud/api-cti/guides/sforce-api-cti-runapex-lex.md
canonical_url: https://developer.salesforce.com/docs/service/api-cti/guide/sforce-api-cti-runapex-lex.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 643480137382edaa507a77bb2158512105c5ff11f2bd24760765c72d0472b389
products:
  - Open CTI Developer Guide
  - Service Cloud Developer Content
---

# runApex() for Lightning Experience

## Usage

Executes an Apex method from an Apex class that's exposed in Salesforce. This method is available in API version 38.0 or later.

## Syntax

```javascript
sforce.opencti.runApex({
    apexClass:STRING, 
    methodName:STRING,
    methodParams:STRING,
    callback:FUNCTION //Optional
})
```

## Arguments

Pass a single `args` object with the following properties.

|Name|Type|Description|
|------|------|-------------|
|`apexClass`|string|Specifies the Apex class of the method to execute.|
|`methodName`|string|Specifies the method to execute.|
|`methodParams`|string|Specifies the method parameters to pass. The string must include field value pairs formatted as a valid query string. If the Apex method doesn't take any parameters, specify `methodParams` as `none` or an empty object, `{}`.|
|`callback`|function|JavaScript method called upon completion of the method. Optional.|


Example `methodParams` value:

```javascript
name=acme&phone=(212) 555-5555
```

## Sample Code–HTML and JavaScript


1.  In Setup, create an Apex class and Apex method.

    <!-- javascript set_clipboard -->
    ```javascript
    global class AccountRetrieval{
    
    webService static String getAccount(String name) {
       List<Account> accounts = new List<Account>();
       for (Account account : Database.query('Select Id, Name, phone from Account where Name like \'' + name + '%\'')){
           accounts.add(account);
       }
           String JSONString = JSON.serialize(accounts);
           return JSONString;
       }
    }
    ```

2.  In Setup, click **Generate from WSDL** to expose the method and class so that a third-party softphone can call it.
3.  Add your code to the softphone:

    <!-- html set_clipboard -->
    ```html
    <html>
    <head>
       <script type="text/javascript" src="http://domain:port/support/api//lightning/opencti_min.js"></script>
       <script type="text/javascript">
           var callback = function(response) {
             if (response.success) {
                console.log('API method call executed successfully! returnValue:', response.returnValue);
             } else { 
                console.error('Something went wrong! Errors:', response.errors);
             }
          };
           function runApex() {
              var param = {apexClass: 'AccountRetrieval', methodName: 'getAccount', methodParams: 'name=acme'};
              param.callback = callback;
              //Invokes API method
              sforce.opencti.runApex(param);
           }
       </script>
    </head>
    <body>
           <button onclick="runApex();">runApex</button>
    </body>
    </html>
    ```

4.  Output is returned. In this example, one account named Acme was found:

    ```javascript
    {
        "success": true,
        "returnValue": {
            "runApex": "[{\"attributes\":{\"type\":\"Account\",\"url\":\"/services/data/v38.0/sobjects/Account
    /001xx000003DGawAAG\"},\"Id\":\"001xx000003DGawAAG\",\"Name\":\"Acme\",\"Phone\":\"(212) 555-5555\"}]"
        },
        "errors": null
    }
    ```


## Response

|Name|Type|Description|
|------|------|-------------|
|`success`|boolean|Returns `true` if the API method call was invoked successfully, `false` otherwise.|
|`returnValue`|object|A JavaScript object containing the result from executing the method from the specified Apex class. No specific format is returned. The format is determined by the value from the method that executed.|
|`errors`|array|If the API call was successful, this variable is `null`. If the API call failed, this variable returns an array of [error messages](https://developer.salesforce.com/docs/service/api-cti/guide/sforce-api-cti-error-messages-lightning.md).|


Example `returnValue`:

```javascript
{"runApex":"[{\"attributes\":{\"type\":\"Account\",
\"url\":\"/services/data/v/sobjects/Account/
001xx000003DGawAAG\"},\"Id\":\"001xx000003DGawAAG\",
\"Name\":\"Acme\",\"Phone\":\"(212)555-5555\"}]"}
```

