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 

1sforce.opencti.runApex({
2    apexClass:STRING, 
3    methodName:STRING,
4    methodParams:STRING,
5    callback:FUNCTION //Optional
6})

Arguments 

Pass a single args object with the following properties.

NameTypeDescription
apexClassstringSpecifies the Apex class of the method to execute.
methodNamestringSpecifies the method to execute.
methodParamsstringSpecifies 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, {}.
callbackfunctionJavaScript method called upon completion of the method. Optional.

Example methodParams value:

1name=acme&phone=(212) 555-5555

Sample Code–HTML and JavaScript 

  1. In Setup, create an Apex class and Apex method.

    1global class AccountRetrieval{
    2
    3webService static String getAccount(String name) {
    4   List<Account> accounts = new List<Account>();
    5   for (Account account : Database.query('Select Id, Name, phone from Account where Name like \'' + name + '%\'')){
    6       accounts.add(account);
    7   }
    8       String JSONString = JSON.serialize(accounts);
    9       return JSONString;
    10   }
    11}
  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:

    1<html>
    2<head>
    3   <script type="text/javascript" src="http://domain:port/support/api//lightning/opencti_min.js"></script>
    4   <script type="text/javascript">
    5       var callback = function(response) {
    6         if (response.success) {
    7            console.log('API method call executed successfully! returnValue:', response.returnValue);
    8         } else { 
    9            console.error('Something went wrong! Errors:', response.errors);
    10         }
    11      };
    12       function runApex() {
    13          var param = {apexClass: 'AccountRetrieval', methodName: 'getAccount', methodParams: 'name=acme'};
    14          param.callback = callback;
    15          //Invokes API method
    16          sforce.opencti.runApex(param);
    17       }
    18   </script>
    19</head>
    20<body>
    21       <button onclick="runApex();">runApex</button>
    22</body>
    23</html>
  4. Output is returned. In this example, one account named Acme was found:

    1{
    2    "success": true,
    3    "returnValue": {
    4        "runApex": "[{\"attributes\":{\"type\":\"Account\",\"url\":\"/services/data/v38.0/sobjects/Account
    5/001xx000003DGawAAG\"},\"Id\":\"001xx000003DGawAAG\",\"Name\":\"Acme\",\"Phone\":\"(212) 555-5555\"}]"
    6    },
    7    "errors": null
    8}

Response 

NameTypeDescription
successbooleanReturns true if the API method call was invoked successfully, false otherwise.
returnValueobjectA 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.
errorsarrayIf the API call was successful, this variable is null. If the API call failed, this variable returns an array of error messages.

Example returnValue:

1{"runApex":"[{\"attributes\":{\"type\":\"Account\",
2\"url\":\"/services/data/v/sobjects/Account/
3001xx000003DGawAAG\"},\"Id\":\"001xx000003DGawAAG\",
4\"Name\":\"Acme\",\"Phone\":\"(212)555-5555\"}]"}