Fetching data from another salesforce instance using API

Fetching data from another salesforce instance using API

Visualforce page:

<apex:page controller=”SearchAcctFromSf” standardStylesheets=”true”>
<apex:form >


<apex:pageBlock >
    <apex:pageblockSection columns=”2″ >
        <apex:outputLabel value=”UserName”/>
        <apex:inputText required=”true” id=”userName” value=”{!userName}” />
        <apex:outputLabel value=”Password”/>
        <apex:inputsecret id=”pwd” value=”{!pwd}”/>      
    </apex:pageblockSection>
    <apex:pageblockButtons >
        <apex:commandButton id=”getRecords” value=”Fetch” action=”{!fetch}” rerender=”acctDetails” status=”waitStatus” />
    </apex:pageblockButtons>
</apex:pageBlock>


<apex:actionStatus startText=”Fetching…” id=”waitStatus”/>


<div style=”display:{!displayError}”> {!errMsg} </div>


<apex:pageBlock >
    <apex:pageblockSection id=”acctDetails”>
        <apex:pageBlockTable value=”{!acc}” var=”account” id=”accTable”>
  
            <apex:column >
                <apex:facet name=”header”>Account Name</apex:facet>
                <apex:outputText value=”{!account.name}”/>
            </apex:column>
  
            <apex:column >
                <apex:facet name=”header”>Created By</apex:facet>
                <apex:outputText value=”{!account.CreatedBy.FirstName}”/>
            </apex:column>
  
            <apex:column >
                <apex:facet name=”header”>Phone</apex:facet>
                <apex:outputText value=”{!account.Phone}”/>
            </apex:column>

        </apex:pageBlockTable>

    </apex:pageblockSection>
</apex:pageBlock>


</apex:form>
</apex:page>

Apex Controller:


public with sharing class SearchAcctFromSf {  
    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 SearchAcctFromSf() 
{
        displayError = ‘none’;
    }

    public void fetch() 
{
        errMsg  = ‘Some error occurred, please try again’;
        try {
        /*———————————–
         Login via SOAP/XML web service api
        ———————————–*/
        HttpRequest request = new HttpRequest();
        request.setEndpoint(‘https://www.salesforce.com/services/Soap/u/22.0’);
        request.setMethod(‘POST’);
        request.setHeader(‘Content-Type’, ‘text/xml;charset=UTF-8’);
        request.setHeader(‘SOAPAction’, ‘””‘);
        /*not escaping username and password because we’re setting those variables above
        in other words, this line “trusts” the lines above
        if username and password were sourced elsewhere, they’d need to be escaped below*/
        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’);

        /*——————————-

         Grab session id and server url
        ——————————–*/
        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();

        /*———————————-

         Load first 20 accounts via REST API
        ———————————*/
        final PageReference theUrl = new PageReference(SERVER_URL + ‘/services/data/v22.0/query/’);
        theUrl.getParameters().put(‘q’,’Select a.Phone, a.Name, a.Industry From Account a limit 20′);
        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’;
        }

    }

}

Remote Site settings:

Dont forget to add “http://www.salesforce.com” under Setup –> Administration Setup –> Security Controls –> Remote Site Settings.

Output:

Leave a Reply