Friday, May 5, 2017

How to call Web Services Method (SOAP) from one Salesforce ORG to another.

Scenario :
Suppose we have a SOAP web service in one Salesforce ORG (ORG 1), as mentioned in my below post. Now in this service there is a web method named as createEmployeeRecord(), containing 2 parameters,

  • Employee name in String format.
  • Employee roll number in Integer format.

This web method is responsible for Inserting employee records based on parameters passed.

Now, our aim is to call this web method from another Salesforce ORG (ORG 2).

Solution :
Below are the detail steps to solve the above scenario,

Activity in ORG 1.
1. Logged in to ORG 1.
2. Create a SOAP web service (MyWebService). To create this Web service, just check my below post and do the same.
3. Once you crate MyWebService, go to that class and click on "Generate WSDL" button. It will open a WSDL file. Just copy paste the contain and save it as "myWebService.xml"
4. Now, click on Setup and search "API" in left side search box. Then open Develop>API.
5. Click on API link will open "API WSDL" detail page, where you get a link "Generate Partner WSDL" under "Partner WSDL" section.
6. Click on this "Generate Partner WSDL" link and it will open a WSDL file. Just copy paste the contain and save it as "partner.xml".

Activity in ORG 2.
7. Logged in to ORG 2.
8. Security Controls > Remote Site Settings
9. Set up 2 new active remote sites for previously created 2 WSDL (Step 3, 5), by click on "New Remote Site" button.
10."Remote Site URL" will be found from generated xml files (Step 3, 5). Copy paste the "location" url under "soap:address" attributes of those xml. In xml it look like this,
<soap:address location="https://login.salesforce.com/services/Soap/u/39.0"/>
11.Once remote sites have been set up, go to Develop > Apex Class and click on "Generate from WSDL" button and chose the generate xml files (Step 3, 5).
12.After successful complete of step 11, system will generate some auto-generated classes for those 2 WSDL.
13.For this example, I will use the below auto-generated classes to call ORG1 web service method [createEmployeeRecord()].
14.Now create a new class as follows to call the web service method,

####################################################################

public class ConsumeWS{
    private static final String userName = 'ORG1 user name';
    private static final String password = 'ORG1 password';
    private static String sessionId = null;
   
    // Method to get the session id.
    private static String getSessionId(){      
        partnerSoapSforceCom.Soap prtnrSoapIns = new partnerSoapSforceCom.Soap();
        partnerSoapSforceCom.LoginResult rslt = prtnrSoapIns.login(userName, password);
       
        if(rslt != null)
            sessionId = rslt.sessionId;
         
        return sessionId;      
    }
   
    // Method responsible for web service method call.
    public static void callWSMethod(){
        soapSforceComSchemasClassMywebservi.MyWebService stub = new soapSforceComSchemasClassMywebservi.MyWebService();
        stub.SessionHeader = new soapSforceComSchemasClassMywebservi.SessionHeader_element();
       
        stub.SessionHeader.sessionId = getSessionId();
        System.debug('Response from web service :::::::: '+ stub.createEmployeeRecord('Mark',10));
    }
}
####################################################################
To test the scenario, open the anonymous window and call the callWSMethod() method of the above created class and it will execute result as follows,

  1. If there is no exception from service side, then it will crate a new employee record as per value passed as a parameters and in debug log it will shown "SUCCESS" message as response.
  2. If there is any exception from service side, then it will not crate any employee record and in debug log it will shown "FAILURE" message as response.

LWC to LWC Communication using Lightning Messaging Service (Part - 2)

In my previous post ( previous post link ) we have learn how to create a Lightning Messaging Service with 6 steps and today we will use the ...