We can create a web service in Salesforce using “web service” keyword. The web service is always of static type and can return values.
Here is the code to create a web service:
Syntax:
public class className { webservice static returnType webserviceName(type perameterName) { // Write the webservice code to perform an action //return statement } }
For Example:
global class pay_Invoice_Controller { webservice static String paymentMaid(Id invoiceId) { // Code to perform an action //return statement } }
The code to call a web service on button click:
Syntax:
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} var result=sforce.apex.execute("NameSpace.class Name","webservice name",{parameter name:"{!parameter value that we pass}"});
For Example:
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} var result=sforce.apex.execute("object.pay_Invoice_Controller","paymentMaid",{invoiceId:"{object__Invoice__c.Id}"});