Salesforce - 通过 Apex 控制器执行 Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10354284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Salesforce - Executing Javascript through Apex Controller
提问by user891859
Im new to Salesforce in general and Im trying to decide how to handle the invoking of a SOAP webservice. Currently the webservice is being executed via AJAX when a user clicks a button on the Opportunity page. I have been asked to move the webservice invocation from the button and place it into a custom controller page. So the webservice needs to be invoked seamlessly when certain conditions are met, opposed to having the user click a button.
我是 Salesforce 的新手,我正在尝试决定如何处理 SOAP 网络服务的调用。当前,当用户单击“商机”页面上的按钮时,Web 服务正在通过 AJAX 执行。我被要求从按钮移动 web 服务调用并将其放入自定义控制器页面。因此需要在满足某些条件时无缝调用 web 服务,而不是让用户单击按钮。
I'd like to just kick off the webservice using the same ajax statement because it will save me time. Although it would seem to make more sense to invoke the webservice via Apex, but am still researching that topic. So here is my question:
我想使用相同的 ajax 语句启动 web 服务,因为它可以节省我的时间。尽管通过 Apex 调用网络服务似乎更有意义,但我仍在研究该主题。所以这是我的问题:
Is it possible to execute the following javascript from within an Apex controller? If so how?
是否可以从 Apex 控制器中执行以下 javascript?如果是这样怎么办?
{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}
var xfolder = "MyNewFolder"
var parentid = "999999999999999"
var myvar = sforce.apex.execute("myWS","invokeExternalWs", {folderName:xfolder,ObjectID:parentid});
window.alert('Folder created: ' + myvar);
回答by Matt K
Read these reference documents thoroughly first: Action Functions, Apex in Ajax, or Visualforce JavaScript Remoting.
首先通读这些参考文档:Action Functions、Apex in Ajax或Visualforce JavaScript Remoting。
The easiest way to do what you're asking is to include the connection.js
and apex.js
files on the page using an <apex:includeScript>
tag (like below).
执行您要求的最简单方法是使用标记(如下所示)在页面上包含connection.js
和apex.js
文件<apex:includeScript>
。
<apex:includeScript value="/soap/ajax/24.0/connection.js"/>
<apex:includeScript value="/soap/ajax/24.0/apex.js"/>
Then, put your JavaScript in a function in a <script>
tag under the <page>
tag.
然后,将您的 JavaScript 放在<script>
标记下的<page>
标记中的函数中。
<script>
function invokeWebService()
{
var xfolder = "MyNewFolder"
var parentid = "999999999999999"
var myvar = sforce.apex.execute("myWS","invokeExternalWs", {folderName:xfolder,ObjectID:parentid});
window.alert('Folder created: ' + myvar);
}
</script>
Finally, call your function with the onclick
attribute on an input button.
最后,onclick
使用输入按钮上的属性调用您的函数。
<button type="button" onclick="invokeWebService();" style="btn">Invoke</button>
Note.. I haven't tested any of this, but it should work with minor tweaks (I've used a similar approach many times).
注意..我没有测试过任何这些,但它应该可以通过小的调整来工作(我已经多次使用类似的方法)。