Tuesday 23 February 2016

How to call an apex method from custom button in salesforce

Here is the simple example to execute apex method and passing parameters from a custom button in salesforce.

Here is the simple example to execute apex method and passing parameters from a custom button in salesforce.

Apex Class:

  1. global class NameMergeController{
  2.     webService static string getName(String fName, String lName){
  3.         String fullName = 'Hi '+ fName +' '+ lName;
  4.         return fullName;
  5.     }
  6. }

Button Code:

Passing Static Parameters:

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

Passing Dynamic Parameters:

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

Where,
NameMergeController > Class name
getName > Method name.

Please check this link, if you receive "No service available for class" exception.

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",{ });

Monday 22 February 2016

UNSUPPORTED_CLIENT Callout Error in Salesforce

Error:

[{"message":"HTTPS Required","errorCode":"UNSUPPORTED_CLIENT"}]

Solution:

Unsupported_Client error occurs when making callout from salesforce using http web server instead of https. There are two possible solutions to fix this issue,

Option 1:

You can change the security setting in your Salesforce account to allow API communication over HTTP as follows:
1. Log into Salesforce.com.
2. Click Setup, click Security Controls, and then click Session Settings.
3. Uncheck the option labeled Require secure connections (HTTPS).



Note: To change to HTTP, you need to contact salesforce.com

Option 2:


  1. HttpRequest req = new HttpRequest();
  2. req.setHeader('Authorization''Bearer ' + UserInfo.getSessionID());
  3. req.setHeader('Content-Type''application/json');
  4. req.setEndpoint('https://na15.salesforce.com/services/data/v36.0/tooling/sobjects/');
  5. req.setMethod('GET');

End point address should be https.

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