SF菜鸟笔记【JS里获取List View里的选中记录IDs】

// 网络来源 http://www.sundoginteractive.com/sunblog/posts/three-reasons-to-use-javascript-and-the-salesforce-api-behind-custom-button


THREE REASONS TO USE JAVASCRIPT AND THE SALESFORCE API BEHIND CUSTOM BUTTONS.

Let’s take a look at an example.  I need to be able to update all of the accounts that are selected in a standard list view.  I could create and call a Visualforce page, which would be passed to all of the rows that are selected.  This would require a Visualforce page, an Apex controller and the supporting unit tests.  Or I could have my custom button have the following settings which will allow it to call some javascript:
A) Display Type: List Button ( Check the ‘Display Checkboxes’ option )
B) Behavior: Execute Javascript
C) Content Source: Onclick JavaScript

Then, in the multi-line textbox where the Javascript can be written, something like this can be used:

{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
var url = parent.location.href;
var records = {!GETRECORDIDS($ObjectType.Account)};
var updateRecords = [];
 
if (records[0] == null) {
    alert("Please select at least one record to update.");
} else {
   for (var a=0; a < records.length; a++) {
     var update_Item = new sforce.SObject("Account");
     update_Item.Id = records[a];
     update_Item.MyCustomField__c = "My Special Value";
     updateRecords.push(update_Item);
   }
   //This line does the actual update
   result = sforce.connection.update(updateRecords);
       
   //Now check if there are any errors
   var hasErrors = false;
   var errorReport = "The following Accounts could not be updated:\n";
   for (var j = 0; j < result.length; j++) {
          if (!result[j].getBoolean("success")) {
            hasErrors = true;
            errorReport += ("AccountId: " + updateRecords[j].Id + ", error: " + result[j].errors.message + "\n");
            if (j == 10 && j < result.length) {
              errorReport += ("Maximum errors to display.  Will show first 10 out of " + "result.length\n");
              break;
            }
          }
        }
 
   if (hasErrors) {
     alert(errorReport);
   }
   parent.location.href = url;
}

Why do I like this solution?

1) It is easier to manage
I do not have to create a separate Visualforce page, Apex class and test cases.  Anytime we can reduce the number of objects in the code and have fewer lines of code we will probably come out ahead.  One of the important considerations of Agile programming is to do the most simple option first.  This is because we do not know what the future will hold, so do the most simple and effective option first.

2) Updates to code do not need to be deployed
If a Visualforce page and an Apex class were created we would have to deploy those items with Eclipse or a change set.  With the above solution once the custom button has been deployed any future changes can be as simple as copying and pasting the javascript code to the new environment.  This also means that all unit tests do not have to run which can make a deployment to production much easier and faster.

3) It is faster
Looping through these records and then calling updates with the API is very fast.  At the end of the code we still refresh the page by setting parent.location.href value.  But the user experience still seems to be better because it is faster and the user does not see as much screen flickering, which would happen if a separate Visualforce page was being called.

What other ways have you used the Salesforce API behind a custom button?


你可能感兴趣的:(salesforce,javascript,Salesforce学习笔记)