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