Salesforce | How to create and call a webservice in Salesforce

Author - Webner

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

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *