---
schema_version: "1"
id: ecc26d967b7deea9622b8b93ff3804318e975733f855b2c160481db63af18d36
title: Apex in AJAX
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/apex_and_ajax.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_and_ajax.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: d23f3b6ae04ed6c31a2686dfb19c82e2fa10c337e97e49f9e02369d9a1a5ffd6
description: The AJAX toolkit includes built-in support for invoking Apex through anonymous blocks or public webservice methods.
---
# 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:

<script src="/soap/ajax/68.0/connection.js" type="text/javascript"></script>
<script src="/soap/ajax/68.0/apex.js" type="text/javascript"></script>

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/images/icon_note.png)

#### Note

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

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:
    
    global with sharing class myClass { 
        webservice static Id makeContact(String lastName, Account a) { 
            Contact c = new Contact(LastName = lastName, AccountId = a.Id); 
            return c.id; 
        }
    }
    
    By using the following JavaScript code:
    
    var account = sforce.sObject("Account");
    var id = sforce.apex.execute("myClass","makeContact",
                                 {lastName:"Smith",
                                  a:account});
    
    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:
    
    global with sharing class myClass{ 
       webservice static String getContextUserName() {
            return UserInfo.getFirstName();
       }
    }
    
    Use the following JavaScript code:
    
    var contextUser = sforce.apex.execute("myClass", "getContextUserName", {});
    
    ![Note](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/images/icon_note.png)
    
    #### Note
    
    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 myClass, the JavaScript code from above would be rewritten as follows:
    
    var contextUser = sforce.apex.execute("myNamespace.myClass", "getContextUserName", {});
    
    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’s listed under Developer Settings.
    
    For more information on the return datatypes, see [Data Types in AJAX Toolkit](https://developer.salesforce.com/docs/atlas.en-us.264.0.ajax.meta/ajax/sforce_api_ajax_datatypes.htm)

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

sforce.debug.trace=true;

- [← Previous](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_invoking_javascript_remoting.htm "JavaScript Remoting")