/force_platform_cookbook

Page 72

Chapter 3: Customizing the User Interface

Creating a Button with Apex Problem You want to create a new button that executes logic written in Apex. Solution Define a webService method in Apex and then call it using the AJAX Toolkit in a button. For example, suppose you want to create a Mass Add Notes button on accounts: 1. Define the Web service method in Apex by clicking Setup ➤ Develop ➤ Apex Classes, clicking New, and adding the following code into the body of your new class: global class MassNoteInsert{ WebService static Integer insertNotes(String iTitle, String iBody, Id[] iParentIds) { Note[] notes = new Note[0]; iBody = String.escapeSingleQuotes(iBody); for (Id iParentId : iParentIds) { notes.add(new Note(parentId = iParentId, title = iTitle, body = iBody)); } insert notes; //Bulk Insert return notes.size(); } }

2. Then, click Setup ➤ Customize ➤ Accounts ➤ Buttons and Links, and click New in the Custom Buttons and Links related list. 3. Name the button and assign it the following attributes: • • •

Display Type: List Button Behavior: Execute JavaScript Content Source: OnClick JavaScript

4. In the code for the button, enter: {!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")} var idsToInsert= {!GETRECORDIDS( $ObjectType.Account )}; var noteTitle = prompt("Please enter the title of the note");

66


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.