This blog describes how to create a Custom web service in Salesforce using Apex.
Steps:
- Navigate to Setup>Develop>Apex Classes and create new class by clicking new button.
- Write a class with "global" access modifier with methods (to be exposed) with "webService" keyword
eg.
global class KartikService
{
webService static String hello(String Name)
{
return 'Hello '+Name+' ! :D';
}
}
- Then save this class. After saving the class you will view a button "Generate WSDL". Click on that to get WSDL for web service you just created. Save the WSDL file. Let say file name is "KartikService.wsdl"
- Now we will consider how we generate clients to call this service in Java and in .Net.
For Java:
- We will create new project in Eclipse with name "ApexCustomWS"
- You need Enterprise/Partner WSDL to access your Salesforce org. by getting a valid sessionId.
- Import Enterprise or Partner WSDL file into the project and generate the web service client for that.
- Import the "KartikService.wsdl" into project and generate web service client by right clicking on the WSDL file.
- Image below shows code generated for Partner WSDL in Blue box and code for Custom Web service in Red Box
package com.sforce.soap.schemas._class.KartikService;
import java.net.URL;
import org.apache.axis.AxisFault;
import com.sforce.soap.partner.LoginResult;
import com.sforce.soap.partner.SessionHeader;
import com.sforce.soap.partner.SforceServiceLocator;
import com.sforce.soap.partner.SoapBindingStub;
public class Test
{
public static void main(String[] args)
{
try
{
//Get a stub for calling partner WSDL's login method in order to get SessionID
SoapBindingStub bind= (SoapBindingStub)new SforceServiceLocator().getSoap();
LoginResult lr=bind.login("your_username_here", "your_Password+SecurityToken");
//Create a sessionHeader object and set its sessioId property to sessionId
//received in loginResult object
SessionHeader sh=new SessionHeader();
sh.setSessionId(lr.getSessionId());
//Create a service locator object for your custom web service
KartikServiceServiceLocator locator=new KartikServiceServiceLocator();
//Get URL for your custom web service
URL url=new URL(locator.getKartikServiceAddress());
//Create a stub for your custom web service with URL for your service and locator as parameters
KartikServiceBindingStub stub=new KartikServiceBindingStub(url, locator);
//Set the header property of stub with name "SessionHeader" and value as sh-sessionHeader
//object created above
stub.setHeader(locator.getKartikServiceAddress(), "SessionHeader", sh);
//now make call to custom service-in our case its "Hello(String name)"
System.out.println(stub.hello("KARTIK"));
}
catch (Exception e)
{e.printStackTrace(); }
}
}
Its output:
Hello KARTIK !
For VB.NET:
- Create a new Winows project or ASP.net project with name "ApexCustomWS"
- Add Web Service Reference using Partner.wsdl with namespace name as "sForceService"
- Add Web Service Reference using KartikService.wsdl with namespace name as "MyCustomApexWS"
- Now the project in solution explorer with these references will look like :
- Now we write a procedure "callCustomApexService" that first calls login service and get a sessionID and then make call to custom web service.
- Code:
Imports ApexCustomWS.sForceService Imports ApexCustomWS.MyCustomApexWS Public Class Form1 Public Sub callCustomApexService() Dim stub As New ApexCustomWS.sForceService.SforceService Dim lr As LoginResult Dim sh As New ApexCustomWS.sForceService.SessionHeader 'make call to login service to get sessionId lr = stub.login("your_username_here", "your_Password+SecurityToken") sh.sessionId = lr.sessionId stub.Url = lr.serverUrl stub.SessionHeaderValue = sh 'Create stub to call custom web service Dim myserviceStub As New MyCustomApexWS.KartikServiceService myserviceStub.SessionHeaderValue = New MyCustomApexWS.SessionHeader myserviceStub.SessionHeaderValue.sessionId = sh.sessionId MsgBox(myserviceStub.hello("KARTIK")) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load callCustomApexService() End Sub End Class