Skip to content

Instantly share code, notes, and snippets.

@joshbirk
Created February 9, 2012 20:11
Show Gist options
  • Select an option

  • Save joshbirk/1782756 to your computer and use it in GitHub Desktop.

Select an option

Save joshbirk/1782756 to your computer and use it in GitHub Desktop.
AJAX / JS Remoting Testing
global class AJAXRemoteController {
webService static String getContextUserName() {
return UserInfo.getFirstName();
}
webService static List<Contact> getAllMyContactsWS() {
List<Contact> contacts = [SELECT ID, FirstName, LastName, Phone, MobilePhone, AssistantPhone from Contact];
return contacts;
}
@RemoteAction
static public List<Contact> getAllMyContacts() {
List<Contact> contacts = [SELECT ID, FirstName, LastName, Phone, MobilePhone, AssistantPhone from Contact];
return contacts;
}
@RemoteAction
static public List<BlueCategory> getClassExample() {
List<BlueCategory> bc = new List<BlueCategory>();
BlueCategory b = new BlueCategory('Test');
bc.add(b);
return bc;
}
}
<apex:page showHeader="false" controller="AJAXRemoteController" >
<script type="text/javascript">
var __sfdcSessionId = '{!GETSESSIONID()}';
</script>
<script src="/soap/ajax/21.0/connection.js"></script>
<script src="/soap/ajax/21.0/apex.js"></script>
<script>
function getAJAX() {
var contacts = sforce.connection.query('SELECT ID, FirstName, LastName, Phone, MobilePhone, AssistantPhone from Contact');
console.log(contacts.records.length);
}
function getJSRemoting() {
AJAXRemoteController.getAllMyContacts(function (results, message) {
console.log(results.length);
});
}
function getAJAXWS() {
var contacts = sforce.apex.execute("AJAXRemoteController", "getAllMyContactsWS", {});
console.log(contacts.length);
}
function getJSRemotingClass() {
AJAXRemoteController.getClassExample(function (results, message) {
console.log(results[0]);
});
}
</script>
<div>
<button onclick="getAJAX()">Get Contacts (AJAX Toolkit)</button> |
<button onclick="getJSRemoting()">Get Contacts (JS Remoting)</button> |
<button onclick="getAJAXWS()">Get Contacts (AJAX w/ WS)</button> |
<button onclick="getJSRemotingClass()">Get Class (JS Remoting)</button>
</div>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment