2

Return value from Apex method to Javascript in Salesforce using a custom button.For that i have controller :

global class web
{
    Webservice static String fetch(Id localId)
    {
        return localId;
    }
}

And Custom Button Javascript:

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

var r = confirm("Are you sure want to fetch?");

if(r == true)
{
    var a = sforce.apex.execute("web","fetch",{ID:"{!Account.Id}"});
    alert(a);
}
else
{
    alert('Operation aborted');
}

When i clicked on custom Button one alert box appeared "Are you sure want to fetch?" that's ok after that second alert box give me the Error.Error is

A problem with the OnClick JavaScript for this button or link was encountered:

{faultcode:'soapenv:Client', faultstring:'No operation available for request {http://soap.sforce.com/schemas/package/web}fetch, please check the WSDL for the service.', }

So what can i do to solved the above problem...Please help me....

Snapshot of the Problem....

image

0

3 Answers 3

2

Do you have a namespace for your org you are working in? Say, if the namespace of the org is "abc", try the following code:

var a = sforce.apex.execute('abc.web', 'fetch', {localId:'{!Account.Id}'});

1
  • Thnaks Sir....this is very helpful...thanks again....
    – Vivek113
    Commented Feb 21, 2014 at 5:42
1

I think that you have to change the parameter name to localId.

Change this:

var a = sforce.apex.execute("web","fetch",{ID:"{!Account.Id}"});

by this:

var a = sforce.apex.execute("web","fetch",{localId:"{!Account.Id}"});
6
  • thanks for replay Sir,I just tried as a localId but the problem was not solved...please give me other solutions...
    – Vivek113
    Commented Feb 19, 2014 at 8:01
  • try by changing the parameter type to string. This also, could generate a problem: fetch(Id localId) should be fetch(String localId) Commented Feb 19, 2014 at 8:10
  • I tried Sir but not solved but here i think, the problem in WSDL for the service..I works on developer account so is it not a problem?
    – Vivek113
    Commented Feb 19, 2014 at 8:17
  • 2
    Your class definition and javascript call are ok. The WSDL is not the problem, because you don't have to create it when you are referring sforce lib. I've created your same sample applied the mentioned fixes and it worked fine. Commented Feb 19, 2014 at 8:23
  • Sir Here i add the snapshot of the alert box of the above defined problem...sir please help me to solve the problem...
    – Vivek113
    Commented Feb 19, 2014 at 8:29
0

You have used String return type for your method and returning an Id.

Try this:

Webservice static Id fetch(Id localId)
{
        return localId;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .