Tuesday 23 February 2016

No service available for class when calling apex method from custom button in Salesforce

Error:

You may get a following error, when you call an apex method from custom button. 
{faultcode:'soapenv:Client', faultstring:'No service available for class ‘MetaDataSOQLController’ }

Class:

  1. global class MetaDataSOQLController{
  2.     webService static string getName(){
  3.         return ‘Hi….!;
  4.     }
  5. }

Button:

  1. {!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
  2. {!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")}
  3. var res = sforce.apex.execute("MetaDataSOQLController","getName",{ });
  4. alert(res);

Solution:

Referred class in a button is a Managed Package Class or your organization is enabled a namespace prefix.

To know to namespace prefix of the class. Go to Apex Classes, in the list view you can find the Namespace Prefix near by class name.

Copy the namespace prefix and change the sforce.apex.execute line as follows,
In my case, I have enabled a namespace prefix and consider my org namespace is “sfkings

Syntax:

sforce.apex.execute("NameSpacePrefix/ClassName","MethodName", “{Parameter 1, Parameter 2}”);

var res = sforce.apex.execute("sfkings/MetaDataSOQLController","getName",{ });

No comments:

Post a Comment

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...