---
schema_version: "1"
id: a2767481be0598f5f3383377c0a32562832d502ab74a4b41d239b4630f01d10d
title: runApex() for Salesforce Classic
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.md
canonical_url: https://developer.salesforce.com/docs/service/api-cti/guide/sforce-api-cti-runapex.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 0b422bea6d8d1682994e6c99615c09ce56addcf343fa4848dc9d80fc73a6dfdf
products:
  - Open CTI Developer Guide
  - Service Cloud Developer Content
---

# runApex() for Salesforce Classic

## Usage

Executes an Apex method from an Apex class that’s exposed in Salesforce.

## Syntax

```javascript
sforce.interaction.runApex(apexClass:string, methodName:string, methodParams:string, (optional) callback:function)
```

## Arguments

|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 and be formatted as a valid query string. For example:`name=acme&phone=(212) 555-5555`.|
|`callback`|function|JavaScript method executed when the API method call is completed.|


## Sample Code–JavaScript


1.  An administrator creates 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 the location where you’ve created the Apex class and method in Salesforce, click **Generate WSDL** to expose the method and class so that a third-party softphone can call it.
3.  Add your code to the softphone:

    <!-- javascript set_clipboard -->
    ```javascript
    <html>
    <head>
       <script type="text/javascript" src="http://domain:port/support/api/25.0/interaction.js"></script>
       <script type="text/javascript">
           var callback = function (response) {
               if (response.result) {
                      alert(response.result);
                } else {
                       alert(response.error);
               }
            };
           function runApex() {
                    //Invokes API method
                    sforce.interaction.runApex('AccountRetrieval', 'getAccount', 'name=acme', callback);
            }
    </script>
    </head>
    <body>
           <button onclick="runApex();">runApex</button>
    </body>
    </html>
    ```

4.  Output is returned. In this example, one account named, Acme, was found:

    ```javascript
    [{"attributes":{"type":"Account",
    "url":"/services/data/v25.0/sobjects/Account/001x0000003DGQRAA4"},
    "Id":"001x0000003DGQRAA4","Name":"Acme","Phone":"(212) 555-5555"}]
    ```


## Response

|Name|Type|Description|
|------|------|-------------|
|`result`|string|Returns 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 was executed.|
|`error`|string|If the API call was successful, this variable is undefined. If the API call failed, this variable returns an error message.|


