Calling Apex method from a custom button using Ajax

There are times when we need to call methods in Apex class on click of a custom button.
Lets take an example of a scenario where we need to add a common note in more than one contact.
In that scenario we can create a custom button namely “Mass Insert Note” in the contact list page
and make a call to the webservice method in Apex to add notes to the selected contacts.

Here is how to do that.

1. First we need to define an Apex class by clicking Setup -> Develop-> Apex Classes and then click New button.
Lets call this class AddNoteToContacts.
2. Then define a webservice method in that class namely addNote().

global class AddNoteToContacts{

 //method to add notes to multiple contact
 WebService static Integer addNote(String noteTitle,
 String noteBody,
 Id[] contactIds) {
 Note[] notes = new Note[0];
 for (Id contactId : contactIds) {
 notes.add(new Note(parentId = contactId,
 title = noteTitle, body = noteBody));
 insert notes; //Bulk Insert

 //return the no. of records in which notes added successfully
 return notes.size();

3. Create a custom button namely “Mass Insert Note” in Contact by clicking SetUp -> Customize -> Contact -> Buttons, Links and Actions
4. In the Buttons, Links and Actions page click “New Button or Link” to create a new custom button.
5. In new Button or Link page:
– Use “Mass Insert Note” as the Label for the custom button.
– Give a description for the custom button.
– Select List Button radio button as the Display type
– Select Execute JavaScript from the Behavior dropdown.
– Content Source dropdown will be populated with the option OnClick Javascript.
6. In the code for button copy the following line of codes:

 //Include the following AJAX toolkit libraries

 var contactIds = {!GETRECORDIDS( $ObjectType.Contact )};
 var noteTitle = prompt("Please enter a title for the note");
 var noteBody = prompt("Please enter the body of the note");

 //check if the user has selected any contact record
 if (contactIds.length) {

// call apex webservice method
 var result = sforce.apex.execute(
 "AddNoteToContacts", // class
 "addNote", // method
 {title : noteTitle, // method arguments
 body: noteBody,
 parentIds: contactIds });

alert("Notes added successfully in "+ result[0] +" records !");
} else if (contactIds.length == 0) {
 alert("Please select at least one contact to add notes to.");

7. Now you can add the custom button in the search list by clicking Setup -> Customize -> Contact -> Search Layout
8. In Contact Layouts page click the link Edit for Contact List View layout and add the button “Mass Insert Note”
9. Click Save.
10. Now to test this go to the Contact List Page and click GO next to View dropdown.
11. Select contacts for which common note need to be added and then click Mass Insert Note.
12. You will be prompted to provide the title and body of the notes.
13. Click OK after providing title and body for the note. If the note get added to the selected contacts successfully you will
see the message that Notes added successfully in [the no. of selected contacts] records.