[SalesForce] Login to Salesforce from Salesforce using soap and xml request

I am new for integartion.Now i want to
Login into Salesforce from Salesforce using soap and xml request,so that if i give username and password of other developeredition and click login button,i can login into another developer edtion.

Thank you

Best Answer

First of all, create a Custom Label 'Login_Request' with value as-

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:partner.soap.sforce.com"><soapenv:Header><urn:organizationId>@@@@OrgId@@@@</urn:organizationId></soapenv:Header><soapenv:Body><urn:login><urn:username>@@@@Username@@@@</urn:username><urn:password>@@@@Password@@@@</urn:password></urn:login></soapenv:Body></soapenv:Envelope>

Next inside apex class Create the below method to login into system and get access token (Session ID)-

public string GetOrgAccessToken() {
        string sessionId;
        string orgId = **18DIGIT_ORG_ID**;
        string suborgId=orgId.substring(0,15);
        string orgbody=Label.Login_Request.replace('@@@@OrgId@@@@',suborgId);
        string userbody=orgbody.replace('@@@@Username@@@@',pHeader.**USERNAME**);
        string Pwdbody=userbody.replace('@@@@Password@@@@',pHeader.**PASSWORD**);
        string body = Pwdbody;
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://LOGIN_OR_TEST.salesforce.com/services/Soap/u/29.0');
        req.setMethod('POST');
        req.setHeader('Content-Type', 'text/xml;charset=UTF-8');
        req.setHeader('SOAPAction', '""');
        req.setTimeOut(30000);
        req.setBody(body);
        Http p = new Http();
        HttpResponse res = p.send(req);
        loginResponse = res.getbody();
        Xmlstreamreader reader = new Xmlstreamreader(res.getbody());
        while (reader.hasNext()) {
            if (reader.getEventType() == XmlTag.Characters) {
                if (string.valueof(reader.getText()).startswith(suborgId + '!')) {
                    sessionId = reader.getText();
                }
                if(string.valueof(reader.getText()).startswith('https://')){
                    getTicketsEndpoint = reader.getText();
                }
            }
            reader.next();
        }
        return sessionId;
    }

Pay special attention and replace the bold text. Important parameters are 18 Digit org ID, Username, Password, Endpoint. Ensure that endpoint base URL is login.salesforce.com for Production or DEV org and test.salesforce.com for Sandboxes.

Once you have the session ID, you can perform further action like example Query on Case object as below -

Create Custom Label Case_Request with value as -

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:partner.soap.sforce.com"><soapenv:Header><urn:DebuggingHeader><urn:debugLevel>none</urn:debugLevel></urn:DebuggingHeader><urn:AllowFieldTruncationHeader><urn:allowFieldTruncation>true</urn:allowFieldTruncation></urn:AllowFieldTruncationHeader><urn:SessionHeader><urn:sessionId>@@@@SessionID@@@@</urn:sessionId></urn:SessionHeader></soapenv:Header><soapenv:Body><urn:query><urn:queryString>@@@@QueryString@@@@</urn:queryString></urn:query></soapenv:Body></soapenv:Envelope>

Create a method in the same class and ensure that you set sessionID that you get in previous method.

    public string GetCaseDetails() {
            string Pwdbody= Label.Case_Request.replace('@@@@QueryString@@@@','Select Id,Name from Case Limit 3');
            string body = Pwdbody.replace('@@@@SessionID@@@@',**SESSIONID**);
            HttpRequest req = new HttpRequest();
            req.setEndpoint(getTicketsEndpoint);
            req.setMethod('POST');
            req.setHeader('Content-Type', 'text/xml;charset=UTF-8');
            req.setHeader('SOAPAction', '""');
            req.setTimeOut(30000);
            req.setBody(body); 
            sampleBody = body;
            Http p = new Http();
            HttpResponse res = p.send(req);
}

Note that sessionID and GetTicketsEndpoint are already set in our previous method. So we can use them. Final step is parse the response you get back from the other org into readable format of inner classes and parameters.