Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
Apex in AJAX
To invoke Apex through anonymous blocks or public webservice methods, include the following lines in your AJAX code:
1<script src="/soap/ajax/68.0/connection.js" type="text/javascript"></script>
2<script src="/soap/ajax/68.0/apex.js" type="text/javascript"></script>- 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:
1global class myClass { 2 webservice static Id makeContact(String lastName, Account a) { 3 Contact c = new Contact(LastName = lastName, AccountId = a.Id); 4 return c.id; 5 } 6}By using the following JavaScript code:
1var account = sforce.sObject("Account"); 2var id = sforce.apex.execute("myClass","makeContact", 3 {lastName:"Smith", 4 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:
1global class myClass{ 2 webservice static String getContextUserName() { 3 return UserInfo.getFirstName(); 4 } 5}Use the following JavaScript code:
1var contextUser = sforce.apex.execute("myClass", "getContextUserName", {});For more information on the return datatypes, see Data Types in AJAX Toolkit
Use the following line to display a window with debugging information:
1sforce.debug.trace=true;