Accessing SalesForce data using SOAP Service - SoapUi

Recently i was trying to figure out how to access SalesForce data from outside. I had two options either to use REST or use SOAP API for accessing the data. I tried both and these are my notes for accessing SalesForce Soap API using Soap client such as SoapUi.
  1. First thing that you would want to do is generate security token for salesforce. Follow these steps for generating security token
  2. Once you have security token, login into the SalesForce console to download the WSDL, Click on Develop -< API and you will see page where you can download all the WSDL's, Download the Enterprise WSDL
  3. Now create new SOAP Ui project using the Enterprise WSDL you just downloaded
    You will notice that it generates bunch of SOAP operations
  4. In order to use the SOAP API you will have to first send a login request to SalesForce and get sessionid once you have it you can use that for making subsequent calls. First call the Login service like this
    
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:urn="urn:enterprise.soap.sforce.com">
       <soapenv:Header>
       </soapenv:Header>
       <soapenv:Body>
          <urn:login>
             <urn:username>replacerwitheuserid</urn:username>
             <urn:password>replacewithpasswordreplacewithsecuritytoken</urn:password>
          </urn:login>
       </soapenv:Body>
    </soapenv:Envelope>
    
    You should replace replacerwitheuserid with your userId (email id that you use for login) and also replace replacewithpassword with your password and use the security token that you got in email to replace replacewithsecuritytoken. Make a request and you will get a response which looks like this
    
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns="urn:enterprise.soap.sforce.com" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <soapenv:Body>
          <loginResponse>
             <result>
                <metadataServerUrl>https://na15.salesforce.com/services/Soap/m/29.0/00Di0000000iKns</metadataServerUrl>
                <passwordExpired>false</passwordExpired>
                <sandbox>false</sandbox>
                <serverUrl>https://na15.salesforce.com/services/Soap/c/29.0/00Di0000000iKns</serverUrl>
                <sessionId>sessionId</sessionId> 
                <userId>userId</userId>
                <userInfo>
                   <accessibilityMode>false</accessibilityMode>
                   <currencySymbol>$</currencySymbol>
                   <orgAttachmentFileSizeLimit>5242880</orgAttachmentFileSizeLimit>
                   <orgDefaultCurrencyIsoCode>USD</orgDefaultCurrencyIsoCode>
                   <orgDisallowHtmlAttachments>false</orgDisallowHtmlAttachments>
                   <orgHasPersonAccounts>false</orgHasPersonAccounts>
                   <organizationId>xxxx</organizationId>
                   <organizationMultiCurrency>false</organizationMultiCurrency>
                   <organizationName>Self</organizationName>
                   <profileId>xxxx</profileId>
                   <roleId xsi:nil="true"/>
                   <sessionSecondsValid>7200</sessionSecondsValid>
                   <userDefaultCurrencyIsoCode xsi:nil="true"/>
                   <userEmail>xxxxx</userEmail>
                   <userFullName>Sunil Patil</userFullName>
                   <userId>xxxxx</userId>
                   <userLanguage>en_US</userLanguage>
                   <userLocale>en_US</userLocale>
                   <userName>sxxxxxx</userName>
                   <userTimeZone>America/Los_Angeles</userTimeZone>
                   <userType>Standard</userType>
                   <userUiSkin>Theme3</userUiSkin>
                </userInfo>
             </result>
          </loginResponse>
       </soapenv:Body>
    </soapenv:Envelope>
    
    You will need the value of sessionId element and value of serverUrl in subsequent requests. so note it down
  5. Next assume that you want to execute query request that will give you list of all the contacts using SELECT Id, FirstName, LastName From Contact query, so use XML that looks like this for the soap request. Replace the replacewithyoursessionid with the sessionId that you got from the login request and also change the URL where SOAP request is made to value of serverUrl
    
    <soapenv:Envelope 
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:urn="urn:enterprise.soap.sforce.com">
    
       <soapenv:Header>
          <urn:SessionHeader>
    <urn:sessionId>replacewithyoursessionid</urn:sessionId>
          </urn:SessionHeader>
       </soapenv:Header>
       <soapenv:Body>
          <urn:query>
             <urn:queryString>
    SELECT Id, FirstName, LastName  From Contact<
    /urn:queryString>
          </urn:query>
       </soapenv:Body>
    </soapenv:Envelo
    
    You will get response with list of contacts

3 comments:

Ram Ramky said...

I have never read more interesting articles than yours before. You make me so easy to understand and I will continue to share this site. Thank you very much and more power!
Salesforce Administrator 211 Training in Chennai
Salesforce Developer 401 Training in Chennai

jefrin said...

glad to see the post very useful

software testing training institute in chennai

lakshmibhucynix said...

I really enjoy the article.Thanks Again. Fantastic.
dot net online training
dotnet institute