You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

Post Salesforce data from one org to Another org Using SOAP API


By: Venkata

SOAP API: 

SOAP API is used to integrate Force.com applications or other third party applications, and it lets the user to maintain passwords, perform searches and much more. You can use the SOAP API with any programming language that supports Web services.

post data to salesforce

In this article, I have documented the steps that we need to follow to post the data from one Salesforce instance to another instance using the SOAP API.

Note 1: 

First, log into the Salesforce (Destination system) that you want to receive the data from, and follow the steps 1 to 3.

  1. Create a web service class like below:
global class MyWebServiceHandler { 
    webService static Id createAccount(String firstName, String lastName) { 
        Contact c = new Contact(lastName = lastName, firstName = firstName); 
        insert c; 
        return c.id; 
    } 
}
  1. Generate a WSDL file for the above Web Service class and save the generated XML file (MyWebServiceHandler.xml) in your system:

post data to salesforce

  1. Generate partner WSDL File

Under Build section, navigate to Develop [Symbol] API and click Generate Partner WSDL link. It will generate a WSDL file and save the Partner.xml.

post data to salesforce

Note 2: 

Log into another Salesforce (Source system) that you are going to send the data and follow the below steps

  1. Under Build section, navigate to Develop [Symbol] Apex Classes, click Generate from WSDL button and select the Partner WSDL file (i.e. partner.xml) that you created in the Step 3. Now, click Parse WSDL button

post data to salesforce

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).

post data to salesforce

  1. Repeat the above step for the MyWebServiceHandler.xml file also

post data to salesforce

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.

  1. Create Remote site settings for the destination Org in the Source Org. For example, my destination org instance is Ap1, So I have created a remote site setting like below in the source org

post data to salesforce

  1. 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);

 

ContactHandler 

public class ContactHandler{ 

    @future(callout=true)     

    public static void createContact(String jsonString){ 

        List<Contact> contactList = (List<Contact>)json.deserialize(jsonString,List<Contact>.class); 

        if(!contactList.isEmpty() && contactList.size() < 99){ 

                 partnerSoapSforceCom.Soap myPartnerSoap = new partnerSoapSforceCom.Soap();  

            partnerSoapSforceCom.LoginResult partnerLoginResult = myPartnerSoap.login(xxxxx@xxx.com', 'xxxxx'); 

            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){  

                myWebservice.createContact(contactIns.firstname,contactIns.lastname); 

            } 

        } 

    } 

}

ContactTrigger: 

Trigger ContactTrigger on Contact (after Insert,after Update) { 

    List<Contact> contactList = NEW List<Contact>(); 

        if(Trigger.isInsert){ 

        For(Contact contactInstance:Trigger.New){ 

                contactList.add(contactInstance); 

        } 

        String jsonString = json.serialize(contactList); 

        ContactHandler.createContact(jsonString); 

    } 

} 

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.

  1. If you create a contact in the source instance, the same contact will be created in the destination instance also.

Source ORG: 

post data to salesforce

Destination ORG: 

post data to salesforce

Reference Link: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_quickstart_steps.htm