views:

80

answers:

2

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 list 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...)

A: 

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", {});
Paddyslacker
no namespaces. Plus, I'd be somewhat (though not deeply.. this is salesforce) surprised that it would tell me "apex" was undef if the problem was the namespace of an apex webservice method.
Ben
A: 

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.

Adam
Switched it to no effect. Moreover, in the other button which works, or if I move this code to the other button, it works in the posted order,
Ben
I get a javascript error when I try it with apex.js loaded first. However, this might depend on the page where the custom button is located - which SF page works and which one doesn't?
Adam
I updated the question details with this info--
Ben
Another interesting thought, but sadly, no, I updated to whichever the latest is and it had no effect. Also, given that the other button works with either API version, it's probably not related to that. I appreciate these suggestions, though.
Ben