066_VFPage中Js Button与controller交互方式(一)

这种方式经常被用来,在button中处理一些逻辑,做法是在detail 页面中加一个button,对应是jS执行调用invoke controller

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:
sforce.apex.execute(类,方法名,参数);
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:

  1. Display Type: Detail Page Button
  2. Behavior: Execute JavaScript
  3. Content Source: OnClick JavaScript
{!REQUIRESCRIPT("/soap/ajax/42.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/42.0/apex.js")}


var idsToInsert= {!GETRECORDIDS( $ObjectType.Account )};
var noteTitle = prompt("Please enter the title of the note");
var noteBody = prompt("Please enter the body of the note");

if (idsToInsert.length) {

   // Now make a synchronous call to the Apex Web service
   // method
   var result = sforce.apex.execute(
    "MassNoteInsert",       // class
    "insertNotes",          // method
    {iTitle : noteTitle,    // method arguments
     iBody: noteBody,
     iParentIds: idsToInsert });

   alert(result[0] + " notes inserted!"); //response
} else if (idsToInsert.length == 0) {
   alert("Please select the accounts to which" +
         " you would like to add notes.");
}

此刻,静下心来学习
原文地址:https://www.cnblogs.com/bandariFang/p/9682149.html