第二篇:salesforce自定义link/button并执行js

来源:互联网 发布:南京行知基地 编辑:程序博客网 时间:2024/06/08 05:47

场景:当记录被锁定(因为审批操作而锁定)后,非超级管理员不能通connection.js的如下方式更新字段:

{!REQUIRESCRIPT("/soap/ajax/40.0/connection.js")}alert('确认后,该项目将发布至项目比选平台!');var tp = new sforce.SObject('Tender_Project__c');tp.id = "{!Tender_Project__c.Id}";tp.isPublished__c = true;tp.Compare_Stage__c = '比选应标';var result = sforce.connection.update([tp]);if(result[0].getBoolean("success")) {    alert('发布成功!');    window.location.reload();}else {    alert('网络异常,请尝试重新发送,仍然出现请联系系统管理员!');}
{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} //adds the proper code for inclusion of AJAX toolkitvar url = parent.location.href; //string for the URL of the current pagevar records = {!GETRECORDIDS($ObjectType.Lead)}; //grabs the Lead records that the user is requesting to updatevar updateRecords = []; //array for holding records that this code will ultimately updateif (records[0] == null) { //if the button was clicked but there was no record selectedalert("Please select at least one record to update."); //alert the user that they didn't make a selection } else { //otherwise, there was a record selectionfor (var a=0; a<records.length; a++) { //for all recordsvar update_Lead = new sforce.SObject("Lead"); //create a new sObject for storing updated record detailsupdate_Lead.Id = records[a]; //set the Id of the selected Lead recordupdate_Lead.Status = "Unqualified"; //set the value for Status to 'Unqualified'updateRecords.push(update_Lead); //add the updated record to our array}result = sforce.connection.update(updateRecords); //push the updated records back to Salesforceparent.location.href = url; //refresh the page}

现状:超级管理员的身份能点击按钮更新字段,没有那么高权限的专员点击按钮不能更新,并提示错误信息。

错误信息:{errors:{message:'此记录已锁定。如果您需要编辑,请联系您的管理员。', statusCode:'ENTITY_IS_LOCKED', }, id:null, success:'false', }

workaround:使用webservices,通过call apex来提高系统用户的权限。

步骤:
1、编写webservice类:

global class UpdateStateByCustomButtonFunction {    webservice static String updateState(ID recId){     String errMsg = '发布成功!';    Tender_Project__c tp = new Tender_Project__c();    tp.Id = recId;    tp.Is_Published__c = true;    try{    update tp;    }catch(DmlException e) {    System.debug('update error: '+e.getMessage());    errMsg = '网络异常,请尝试重新发送,仍然出现请联系系统管理员!';    }return errMsg;}}
2、添加该类到相关操作专员简档,允许专员访问该apex类

3、编写js逻辑:

{!REQUIRESCRIPT("/soap/ajax/40.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/40.0/apex.js")} alert('确认后,该项目将发布至项目比选平台!'); var result = sforce.apex.execute("UpdateStateByCustomButtonFunction","updateState",{recId:"{!Tender_Project__c.Id}"});//类名,方法名,参数 if(result = '发布成功!') { alert(result); location.reload(); }else { alert('网络异常,请尝试重新发送,仍然出现请联系系统管理员!') }

参考资源:

http://www.mstsolutions.com/blog/content/creating-custom-button-javascript-salesforce

http://blog.shivanathd.com/2014/07/call-apex-class-from-custom-button-salesforce.html

=====================================================

问题2:在记录锁定的情况下,通过自定义button,输入并更新记录的某一字段?

solution:在alert中输入值,并利用ajax更新字段。

preview:




资源链接:

https://www.ebsta.com/blog/custom-buttons-in-salesforce/

http://www.jitendrazaa.com/blog/salesforce/create-and-update-records-using-javascript-button-in-salesforce-ajax-toolkit/

原创粉丝点击