Introducing SOAP API
SOAP is a remote procedure call protocol that works over HTTP. SOAP requests are HTTP POST requests made to the web service endpoint URL. The client and server exchange data in the XML format in the body of HTTP requests and responses
Salesforce provides programmatic access to your org’s information using simple, powerful, and secure application programming interfaces. Use SOAP API to create, retrieve, update or delete records, such as accounts, leads, and custom objects. With more than 20 different calls, SOAP API also allows you to maintain passwords, perform searches, and much more. Use SOAP API in any language that supports Web services.
Any functionality described in this guide is available if your organization has the API feature enabled. This feature is enabled by default for Performance, Unlimited, Enterprise, and Developer Editions. Some Professional Edition organizations may also have the API enabled.
SOAP API (Simple Object Access Protocol API) can be used to create, retrieve, delete and update records. We can use SOAP API in any language that supports web services.We can create, update, delete, retrieve records in force.com from any external system that supports SOAP based web services by using force.com SOAP API (like Java, .net etc.…) with more than 20 different calls.SOAP-API also allows you to maintain passwords, perform searches and retrieve metadata.
Salesforce provides two different SOAP API WSDLs (WSDL: Web service description language).
1. Enterprise WSDL
2. Partner WSDL
What is WSDL?
=>WSDL (Web service description language) is an XML document which contains a standardized description on how to communicate using a web service.
What is Enterprise WSDL?
=>Enterprise WSDL is a strongly typed WSDL for customers who want to build an integration with their Salesforce organization. It is intended primarily for customers.
What is partner WSDL?
=>Partner WSDL is a loosely typed WSDL for customers and partners who are building client applications for multiple originations. This WSDL can be used to access data within any organization. It I intended primarily for partners.
The Salesforce prebuilt applications provide powerful CRM functionality. In addition, Salesforce provides the ability to customize the prebuilt applications to fit your organization. However, your organization may have complex business processes that are unsupported by the existing functionality. In this case, Lightning Platform provides various ways for advanced administrators and developers to build custom functionality. These include the SOAP API, Apex, and Visualforce.
=>To integrate one Salesforce org to another using SOAP API follow below steps:
Step 1: First, log into the Salesforce (Destination Org) that you want to receive the data.
global class MyWebServiceHandler {
webService static Id createContact(String firstname, string lastname){
Contact c = new Contact();
c.FirstName = firstname;
c.LastName = lastname;
insert c;
return c.Id;
}
}
Step 2: Generate partner WSDL File
2.1 Go to setup and search API and click Generate Partner WSDL link. It will generate a WSDL file and save the Partner.xml.
*Log into another Salesforce (Source system) that you are going to send the data and follow the below steps:
Step 3: Go to setup and search for apex classes. Click to Generate from WSDL and select the Partner WSDL file (i.e. partner.xml) that you created in the Step 2. Now, click Parse WSDL button.
*It will navigate to another page that will show the Parse successful message. Now, click Generate Apex code button. (if you get any error during the apex generation, please replace all the occurrences of “anyType” to “string” in the Partner.xml file).
Step 4: Repeat the above step for the MyWebServiceHandler.xml file also.
*Two files will be generated from their corresponding xml files. The first file is used for the synchronous call, and the next one is used for the asynchronous call.
Step 5: Create Remote site settings for the destination Org in the Source Org.
Step 6: Apex class and Trigger for Contact Creation: Create the below class (ContactHandler) and trigger (ContactTrigger) into your Source org.
Replace the “xxx” with your Destination org’s user name and password + security token, like below.
partnerSoapSforceCom.LoginResult partnerLoginResult = myPartnerSoap.login(‘xxxxx@xxx.com’, ‘xxxxx’);
to
partnerSoapSforceCom.LoginResult partnerLoginResult = myPartnerSoap.login(Username, Password+securityToken);
****Apex class****
public class ContactHandler{
@future(callout=true)
public static void createContact(List<Id> IdList){
List<Contact> contactList = [SELECT Id,Firstname, Lastname FROM Contact WHERE ID=:IdList];
if(!contactList.isEmpty() && contactList.size() < 99){
partnerSoapSforceCom.Soap myPartnerSoap = new partnerSoapSforceCom.Soap();
partnerSoapSforceCom.LoginResult partnerLoginResult = myPartnerSoap.login(XXXX@XXX.com', ‘XXXX'+'XXXXXX');
soapSforceComSchemasClassMywebservi.SessionHeader_element webserviceSessionHeader = new soapSforceComSchemasClassMywebservi.SessionHeader_element();
webserviceSessionHeader.sessionId = partnerLoginResult.sessionId;
soapSforceComSchemasClassMywebservi.MyWebServiceHandler myWebservice = new soapSforceComSchemasClassMywebservi.MyWebServiceHandler();
myWebservice.SessionHeader = webserviceSessionHeader;
for(Contact contactIns:contactList){
System.debug('contactIns-->'+contactIns);
myWebservice.createContact(contactIns.firstname,contactIns.lastname);
}
}
}
}
****Trigger****
Trigger ContactTrigger on Contact (after Insert) {
List<Id> contactList = NEW List<Id>();
if(Trigger.isInsert){
For(Contact contactInstance:Trigger.New){
contactList.add(contactInstance.Id);
}
// String jsonString = json.serialize(contactList);
ContactHandler.createContact(contactList);
}
}
*Note: The above class and trigger handle only 99 records because standard Salesforce callout limit is 100 per transaction, if you want to process more than 100 records, you should go for a batch class.
Step 6: If you create a contact in the source instance, the same contact will be created in the destination instance also.