Integrating Salesforce ORGs | Techila
1122
single,single-post,postid-1122,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive
 

Integrating Salesforce ORGs

salesforce-multi-org-pic-2-620x400

Integrating Salesforce ORGs

 

 

 

This post is centered around explaining the process of fetching data from another Salesforce.com Org using Rest WebServices.By Default Salesforce.com Does not allow to  Access any Domain , So the first step for integration is to allow authorized access to the URL’s which we would be using in the webservices.

 

Click Setup|Administer|Security Control | Remote Site Settings.

 

 and then add the below mentioned URL’s .

 

1)  https://www.salesforce.com

2)  https://ap2-api.salesforce.com

 

Replace the Yellow highlighted Text with the Subdomain of URL of the instance from where you want to fetch data.

 

Now Copy and the paste the below code to integrate two orgs and fetch the account data.

 

Class Code:

public with sharing class FetchAccount {

 

    String LOGIN_DOMAIN = ‘www’;

    public String pwd{get;set;}

    public String userName{get;set;}

    public List<Account> acc{get;set;}

    public String errMsg{get;set;}

    public String displayError{get;set;}

 

    public FetchAccount()

    {

        displayError = ‘none’;

    }

 

    public void fetch()

    {

        errMsg  = ‘Some error occurred, please try again’;

        try

        {

        HttpRequest request = new HttpRequest();

        request.setEndpoint(‘https://’ + LOGIN_DOMAIN + ‘.salesforce.com/services/Soap/u/22.0’);

        request.setMethod(‘POST’);

        request.setHeader(‘Content-Type’, ‘text/xml;charset=UTF-8’);

        request.setHeader(‘SOAPAction’, ‘””‘);

        request.setBody(‘<Envelope xmlns=”http://schemas.xmlsoap.org/soap/envelope/”><Header/><Body><login xmlns=”urn:partner.soap.sforce.com”><username>’ + userName+ ‘</username><password>’ + pwd+ ‘</password></login></Body></Envelope>’);

        Dom.XmlNode resultElmt = (new Http()).send(request).getBodyDocument().getRootElement()

          .getChildElement(‘Body’, ‘http://schemas.xmlsoap.org/soap/envelope/’)

          .getChildElement(‘loginResponse’, ‘urn:partner.soap.sforce.com’)

          .getChildElement(‘result’, ‘urn:partner.soap.sforce.com’);

 

        final String SERVER_URL = resultElmt.getChildElement(‘serverUrl’, ‘urn:partner.soap.sforce.com’) .getText().split(‘/services’)[0];

        final String SESSION_ID = resultElmt.getChildElement(‘sessionId’, ‘urn:partner.soap.sforce.com’) .getText();

 

       

        final PageReference theUrl = new PageReference(SERVER_URL + ‘/services/data/v22.0/query/’);

        theUrl.getParameters().put(‘q’,’Select a.Phone, a.Name, a.CreatedBy.FirstName, a.CreatedById From Account a limit 10′);

        request = new HttpRequest();

        request.setEndpoint(theUrl.getUrl());

        request.setMethod(‘GET’);

        request.setHeader(‘Authorization’, ‘OAuth ‘ + SESSION_ID);

 

        String body = (new Http()).send(request).getBody();

 

        JSONParser parser = JSON.createParser(body);

 

        do{

            parser.nextToken();

        }while(parser.hasCurrentToken() && !’records’.equals(parser.getCurrentName()));

 

        parser.nextToken();

 

        acc = (List<Account>) parser.readValueAs(List<Account>.class);

        }

        catch(Exception e)

        {

            displayError = ‘block’;

        }

 

    }

 

}

Courtesyhttp://www.worldofsalesforce.com/2016/05/IntegratingSalesforceOrgtoanotherSalesforceOrg.html

Author: techila

No Comments

Post A Comment