从Salesforce Javascript Button调用多个函数

时间:2014-10-23 22:55:12

标签: javascript salesforce

我在javascript上相当新,所以这可能是一个愚蠢的问题。

我在Salesforce中构建了一个javascript按钮,点击后执行以下操作(http://i.stack.imgur.com/hqMhp.png

我现在要做的只是显示4个单独的警报(稍后我将添加实际功能)。

这是我正在使用的代码:

   {!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}


var leadObj = new sforce.SObject("Lead"); 
var countryVal = "{!Lead.Country }"; 
var leadID = "{!Lead.Id}"; 
var ownerID = "{!Lead.OwnerId }"; 

function insertScript(script){ 
    var targetNode = document.createElement('div'); //  construct div for script injection 
    document.body.appendChild(targetNode); 
    try {
        var el = document.createElement('script');
            el.type="text/javascript";
            el.innerHTML = script;
        targetNode.appendChild(el); 
    } catch (e){ 
        var el = document.createElement('span'); 
        targetNode.appendChild(el); 
        el.innerHTML = "<br /><scr"+"ipt type='text/javascript' defer='defer'>"+script+"</script" + ">"; 
    } 

var box = new SimpleDialog("hersh"+Math.random(), true); 
    parent.box = box; 
    box.setTitle("Lead Rerouter"); 
    box.createDialog(); 
    box.setWidth(350); 
    box.setContentInnerHTML("<p align='center'><img src='/img/icon/profile24.png' style='margin:0 5px;'/><img src='/img/sales/quotes/sync_overlay_arrow.png' style='margin:5px;'/><img src='/img/icon/custom51_100/globe24.png' style='margin:0 5px;'/></p><p align='center'>Which region should this lead be routed to?</p><p align='center'><br /><button class='btn' onclick='routeAPAC(); return false;'>APAC</button><button class='btn' onclick='routeEMEA(); return false;'>EMEA</button><button class='btn' onclick='routeNA(); return false;'>NA</button><button class='btn' onclick='routeLATAM(); return false;'>LATAM</button><br><button class='btn' onclick='window.parent.box.hide(); return false;'>Cancel</button></p>"); 
    box.setupDefaultButtons(); 
    box.show(); 
} 

script = "function routeAPAC(){alert (\"Lead routed to APAC!\")}"; 
script = "function routeEMEA(){alert (\"Lead routed to EMEA!\")}"; 
script = "function routeNA(){alert (\"Lead routed to NA!\")}"; 
script = "function routeLATAM(){alert (\"Lead routed to LATAM!\")}";   


insertScript(script);

我在这里做错了什么?

谢谢!

2 个答案:

答案 0 :(得分:1)

你过度复杂了。

document.querySelector(".generatorbutton").onclick = function() {
    //add the window
}

var actionlist = {
    ".apa": function(){
        //things to do onclick
    },
    ".emea": function(){
        //things to do onclick
    },
    ".na": function(){
        //things to do onclick
    },
    ".latam": function(){
        //things to do onclick
    },
};
for(selector in actionlist) {
    document.querySelector(selector).onclick = actionlist[selector];
}

请查看this以供将来参考。

希望这有帮助!

答案 1 :(得分:-1)

由于覆盖了script变量,在上面的代码中,您只能使用最后一个函数routeLATAM来定义insertScript函数

如果您连接如下,您将看到所有可用的函数定义

script = "function routeAPAC(){alert (\"Lead routed to APAC!\")}"; 
script += "function routeEMEA(){alert (\"Lead routed to EMEA!\")}"; 
script += "function routeNA(){alert (\"Lead routed to NA!\")}"; 
script += "function routeLATAM(){alert (\"Lead routed to LATAM!\")}";   

insertScript(script);