This blog post will describe that how to create an Apex web service and integrate it with .Net App using Force.com SOAP API.
Below is an example of how to integrate Apex webservice with Force.com SOAP API and .Net Apps using Visual Studio.
To get started, we'll run through the following steps:
1) Create an Apex webService class
Apex Code:
/********************************************************************************/
global class SuperClass{
global class RequestClass{
webService String accountName;
}
global class ResponseClass{
webService String responseResultID;
webService String responseResultName;
webService String responseResultRecordType;
}
webService static ResponseClass behaviourOfWebService(RequestClass reqClass){
Account acct = new Account();
acct.Name = reqClass.accountName;
insert acct;
ResponseClass resClass = new ResponseClass();
resClass.responseResultID = acct.Id;
resClass.responseResultName = acct.Name;
resClass.responseResultRecordType = acct.RecordTypeId;
return resClass;
}
}
/********************************************************************************/
2) Download the "Enterprise WSDL" or "Partner WSDL" from your salesforce.com organization.
Log in into salesforce.com organization -> User Name -> Setup -> App Setup section -> Develop -> API -> click on Generate Enterprise or Partner WSDL -> Save file with extension ".wsdl" as "EnterpriseWSDL.wsdl" or "PartnerWSDL.wsdl".
3) Download the Apex class "SuperClass" wsdl.
Log in into salesforce.com organization -> User Name -> Setup -> App Setup section -> Develop -> Apex Classes-> click on SuperClass -> click Generate WSDL -> Save file with extension ".wsdl" as "WebServiceWSDL.wsdl".
4) Create a .Net App in Visual Studio
Open Visual Studio -> File -> New -> Project -> Visual C# -> Windows -> Console Application -> Name it "CustomWebService" -> OK
5) Copy the path where your both WSDL's are present.
For Example:
C:\Users\Administrator\Desktop\EnterpriseWSDL.wsdl
C:\Users\Administrator\Desktop\WebServiceWSDL.wsdl
6) Add these WSDL's as Web References in Visual Studio.
In Solution Explorer -> right click on project -> Add Web Reference -> add the path in URL for Enterprise WSDL -> Web reference name = EnterpriseWSDL -> click Add Reference.
For Example:
C:\Users\Administrator\Desktop\EnterpriseWSDL.wsdl
In Solution Explorer -> right click on project -> Add Web Reference -> add the path in URL for Apex Class WSDL -> Web reference name = WebServiceWSDL -> click Add Reference.
For Example:
C:\Users\Administrator\Desktop\WebServiceWSDL.wsdl
Your Project in Visual Studio will look like below screenshot:
7) Write C# Code to integrate with Force.com Apex Web Service using Force.com SOAP API
C# Code:
/********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
namespace CustomWebService
{
class Program
{
static void Main(string[] args)
{
/*
When I add WebReferences I named Apex Class WSDL as "WebServiceWSDL"
When I add WebReferences I named Enterprise WSDL as "EnterpriseWSDL"
*/
//Salesforce.com Credentials
string userName = "Your User Name";
string userPassword = "Your Password + Your Security Token";
//Apex Main Class WSDL Object
WebServiceWSDL.SessionHeader webServiceWSDLSession = new WebServiceWSDL.SessionHeader();
//Enterprise WSDL Object
EnterpriseWSDL.LoginResult enterpriseWSDLLoginResult = new EnterpriseWSDL.LoginResult();
EnterpriseWSDL.SforceService enterpriseWSDLSforceService = new EnterpriseWSDL.SforceService();
//Make Sessions and Login successfully
enterpriseWSDLLoginResult = enterpriseWSDLSforceService.login(userName, userPassword);
enterpriseWSDLSforceService.Url = enterpriseWSDLLoginResult.serverUrl;
webServiceWSDLSession.sessionId = enterpriseWSDLLoginResult.sessionId;
//Number of records to process
int numOfRecords = 1;
//Apex Inner Class RequestClass object to fill the variables of RequestClass and send it to Salesforce.com
WebServiceWSDL.RequestClass reqClass = new WebServiceWSDL.RequestClass();
WebServiceWSDL.RequestClass[] reqClassList = new WebServiceWSDL.RequestClass[numOfRecords];
//Traverse each record
for (int i = 0; i < numOfRecords; i++)
{
reqClass = new WebServiceWSDL.RequestClass();
reqClass.accountName = "Account Name from Dot Net";
reqClassList[i] = reqClass;
}
//Apex Main Class Object
WebServiceWSDL.SuperClassService mainWSDLClassObject = new WebServiceWSDL.SuperClassService();
mainWSDLClassObject.SessionHeaderValue = new WebServiceWSDL.SessionHeader();
mainWSDLClassObject.SessionHeaderValue.sessionId = webServiceWSDLSession.sessionId;
//mainWSDLClassObject.behaviourOfWebService(reqClassList[0]);
//Retrieve the Response of Apex class
WebServiceWSDL.ResponseClass responseClassObject = new WebServiceWSDL.ResponseClass();
responseClassObject = mainWSDLClassObject.behaviourOfWebService(reqClassList[0]);
//Response Result
Console.WriteLine("ID" + responseClassObject.responseResultID);
Console.WriteLine("NAME" + responseClassObject.responseResultName);
Console.WriteLine("RECORD TYPE" + responseClassObject.responseResultRecordType);
Console.ReadLine();
}
}
}
/********************************************************************************/
Below is an example of how to integrate Apex webservice with Force.com SOAP API and .Net Apps using Visual Studio.
To get started, we'll run through the following steps:
1) Create an Apex webService class
Apex Code:
/********************************************************************************/
global class SuperClass{
global class RequestClass{
webService String accountName;
}
global class ResponseClass{
webService String responseResultID;
webService String responseResultName;
webService String responseResultRecordType;
}
webService static ResponseClass behaviourOfWebService(RequestClass reqClass){
Account acct = new Account();
acct.Name = reqClass.accountName;
insert acct;
ResponseClass resClass = new ResponseClass();
resClass.responseResultID = acct.Id;
resClass.responseResultName = acct.Name;
resClass.responseResultRecordType = acct.RecordTypeId;
return resClass;
}
}
/********************************************************************************/
2) Download the "Enterprise WSDL" or "Partner WSDL" from your salesforce.com organization.
Log in into salesforce.com organization -> User Name -> Setup -> App Setup section -> Develop -> API -> click on Generate Enterprise or Partner WSDL -> Save file with extension ".wsdl" as "EnterpriseWSDL.wsdl" or "PartnerWSDL.wsdl".
3) Download the Apex class "SuperClass" wsdl.
Log in into salesforce.com organization -> User Name -> Setup -> App Setup section -> Develop -> Apex Classes-> click on SuperClass -> click Generate WSDL -> Save file with extension ".wsdl" as "WebServiceWSDL.wsdl".
4) Create a .Net App in Visual Studio
Open Visual Studio -> File -> New -> Project -> Visual C# -> Windows -> Console Application -> Name it "CustomWebService" -> OK
5) Copy the path where your both WSDL's are present.
For Example:
C:\Users\Administrator\Desktop\EnterpriseWSDL.wsdl
C:\Users\Administrator\Desktop\WebServiceWSDL.wsdl
6) Add these WSDL's as Web References in Visual Studio.
In Solution Explorer -> right click on project -> Add Web Reference -> add the path in URL for Enterprise WSDL -> Web reference name = EnterpriseWSDL -> click Add Reference.
For Example:
C:\Users\Administrator\Desktop\EnterpriseWSDL.wsdl
In Solution Explorer -> right click on project -> Add Web Reference -> add the path in URL for Apex Class WSDL -> Web reference name = WebServiceWSDL -> click Add Reference.
For Example:
C:\Users\Administrator\Desktop\WebServiceWSDL.wsdl
Your Project in Visual Studio will look like below screenshot:
7) Write C# Code to integrate with Force.com Apex Web Service using Force.com SOAP API
C# Code:
/********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
namespace CustomWebService
{
class Program
{
static void Main(string[] args)
{
/*
When I add WebReferences I named Apex Class WSDL as "WebServiceWSDL"
When I add WebReferences I named Enterprise WSDL as "EnterpriseWSDL"
*/
//Salesforce.com Credentials
string userName = "Your User Name";
string userPassword = "Your Password + Your Security Token";
//Apex Main Class WSDL Object
WebServiceWSDL.SessionHeader webServiceWSDLSession = new WebServiceWSDL.SessionHeader();
//Enterprise WSDL Object
EnterpriseWSDL.LoginResult enterpriseWSDLLoginResult = new EnterpriseWSDL.LoginResult();
EnterpriseWSDL.SforceService enterpriseWSDLSforceService = new EnterpriseWSDL.SforceService();
//Make Sessions and Login successfully
enterpriseWSDLLoginResult = enterpriseWSDLSforceService.login(userName, userPassword);
enterpriseWSDLSforceService.Url = enterpriseWSDLLoginResult.serverUrl;
webServiceWSDLSession.sessionId = enterpriseWSDLLoginResult.sessionId;
//Number of records to process
int numOfRecords = 1;
//Apex Inner Class RequestClass object to fill the variables of RequestClass and send it to Salesforce.com
WebServiceWSDL.RequestClass reqClass = new WebServiceWSDL.RequestClass();
WebServiceWSDL.RequestClass[] reqClassList = new WebServiceWSDL.RequestClass[numOfRecords];
//Traverse each record
for (int i = 0; i < numOfRecords; i++)
{
reqClass = new WebServiceWSDL.RequestClass();
reqClass.accountName = "Account Name from Dot Net";
reqClassList[i] = reqClass;
}
//Apex Main Class Object
WebServiceWSDL.SuperClassService mainWSDLClassObject = new WebServiceWSDL.SuperClassService();
mainWSDLClassObject.SessionHeaderValue = new WebServiceWSDL.SessionHeader();
mainWSDLClassObject.SessionHeaderValue.sessionId = webServiceWSDLSession.sessionId;
//mainWSDLClassObject.behaviourOfWebService(reqClassList[0]);
//Retrieve the Response of Apex class
WebServiceWSDL.ResponseClass responseClassObject = new WebServiceWSDL.ResponseClass();
responseClassObject = mainWSDLClassObject.behaviourOfWebService(reqClassList[0]);
//Response Result
Console.WriteLine("ID" + responseClassObject.responseResultID);
Console.WriteLine("NAME" + responseClassObject.responseResultName);
Console.WriteLine("RECORD TYPE" + responseClassObject.responseResultRecordType);
Console.ReadLine();
}
}
}
/********************************************************************************/