I'm having a hard time understanding what is going on here. I am trying to code up some onClick javascript for a button on a Force.com li开发者_如何学Cst view for a custom object. Here's the JS.
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")}
{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}
var myURL = sforce.apex.execute("MyWebServices", "myUrl", {});
and here's the webservice I'm attempting to call.
global class MyWebServices {
webservice static String myUrl(){
return 'www.foo.com';
}
}
When I click the button, I get the little alert pop-up saying:
"A problem with the OnClick JavaScript for this button or link was encountered: Cannot call method 'execute' of undefined" When I iterate the members of "sforce", there is no "apex".
I am using literally the exact same syntax in another button in this same org, the only difference being the methods I am calling. That button works. In fact, if I copy the code from the problem script and put it at the head of the working script on the other button it works. What am I missing here?
Here is further detail on the two buttons element of this issue: One button (that works) is a list view button on Account, the other (broken) one is on a custom object list button for Foo__c.
I use this code for both:
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}
alert( sforce.apex.execute("HammockWebServices", "crossCoverageUrl", {}));
on Account it works perfectly, on Foo__c it does not. I've tried switching the order of the !REQUIRE... statements to no observable effect.
Besides the label and name of the buttons they seem to be defined identically: List Button Display Checkboxes Execute JavaScript OnClickJavaScript
Are there object-level permissions, profiles, or sharing rules that affect which javascript packages can be accessed? (Note, that even were this the case, I'm trying this as the sysadmin, and getting nowhere...)
Does your organization use namespaces? If so, then the syntax of calling the function changes slightly from:
var myURL = sforce.apex.execute("MyWebServices", "myUrl", {});
to:
var myURL = sforce.apex.execute("MyNamespace.MyWebServices", "myUrl", {});
I think connection.js needs to load before apex.js, try changing the order.
Edit: from apex.js
if (!sforce) {
throw "unable to find sforce. Make sure that connection.js is loaded before apex.js script";
}
But since changing the order of the scripts didn't seem to have any effect, maybe the problem is with the API versions? I see you're using version 10, which is quite old, is your Apex class also using the same version? Try changing the versions to see what works.
"connection" has to be referenced before "apex". It sets up the core sforce object.
精彩评论