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



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?


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值