0

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

  • What exactly do you mean by "move to that developer edition"? What's the goal here? – martin Jul 17 '15 at 13:26
  • Hi Martin, Here my requirement is i need to create one visualforce page with username and password fields and login button aslo .if i click loginbutton with username and password deatils of any other developer organizations ,i can login into that developer id. – user17646 Jul 17 '15 at 13:36
  • Okay, what then? – martin Jul 17 '15 at 13:44
  • like exact target ,just to know how integration to salesforce to salesforce – user17646 Jul 17 '15 at 13:49
  • I see. Best of luck. – martin Jul 17 '15 at 13:58
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy