Newer Version Available

This content describes an older version of this product. View Latest

Apex in AJAX

The AJAX toolkit includes built-in support for invoking Apex through anonymous blocks or public webservice methods.

To invoke Apex through anonymous blocks or public webservice methods, include the following lines in your AJAX code:

For AJAX buttons, use the alternate forms of these includes.

Note

To invoke Apex, use one of the following two methods:
  • Execute anonymously via sforce.apex.executeAnonymous (script). This method returns a result similar to the API's result type, but as a JavaScript structure.
  • Use a class WSDL. For example, you can call the following Apex class:

    By using the following JavaScript code:

    The execute method takes primitive data types, sObjects, and lists of primitives or sObjects.

    To call a webservice method with no parameters, use {} as the third parameter for sforce.apex.execute. For example, to call the following Apex class:

    Use the following JavaScript code:

    If a namespace has been defined for your organization, you must include it in the JavaScript code when you invoke the class. For example, to call the above class, the JavaScript code from above would be rewritten as follows:

    To verify whether your organization has a namespace, log in to your Salesforce organization and from Setup, enter Packages in the Quick Find box, then select Packages. If a namespace is defined, it is listed under Developer Settings.

    Note

    Both examples result in native JavaScript values that represent the return type of the methods.

Use the following line to display a popup window with debugging information: