I am following this blog Post Shivasoft where you can get Records From One Org to Another Using REST API. I have Followed the Same Thing As mentioned tin Blog Still I am Not getting any results

This is Vf Page

<apex:page controller="FetchAccount" standardStylesheets="true">
<style type="text/css">
    font-size:0.8 em;
<apex:pageBlock >
<apex:form >
<apex:outputLabel value="UserName : " for="userName"/>
<apex:inputText required="true" id="userName" value="{!userName}" />
<br />
<apex:outputLabel value="Password : " for="pwd"/>
<apex:inputsecret id="pwd" value="{!pwd}"/>
<br />
<apex:commandButton id="getRecords" value="Get Records" action="{!fetch}" rerender="wrapper" status="waitStatus" />
<apex:actionStatus startText="Requesting..." stopText="" id="waitStatus"/>
<hr />
<apex:outputPanel id="wrapper">
<div class="errorMsg" style="display:{!displayError}"> {!errMsg} </div>
<apex:pageBlockTable value="{!acc}" var="account" id="accTable" rowClasses="odd,even" styleClass="tableClass">

    <apex:column >
        <apex:facet name="header">Account Name</apex:facet>
         <apex:outputText value="{!account.name}"/>

    <apex:column >
        <apex:facet name="header">Created By</apex:facet>
         <apex:outputText value="{!account.CreatedBy.FirstName}"/>

    <apex:column >
        <apex:facet name="header">Phone</apex:facet>
         <apex:outputText value="{!account.Phone}"/>


This is Controller

public with sharing class FetchAccount {

    //Login Domain May be test, prerellogin.pre
   // String LOGIN_DOMAIN = 'www.login';
    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';
        // Login via SOAP/XML web service api
        HttpRequest request = new HttpRequest();
        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 10 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.CreatedBy.FirstName, a.CreatedById From Account a limit 10');
        request = new HttpRequest();
        request.setHeader('Authorization', 'OAuth ' + SESSION_ID);

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

        JSONParser parser = JSON.createParser(body);

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


        acc = (List<Account>) parser.readValueAs(List<Account>.class);
        catch(Exception e)
            displayError = 'block';


Only changes I have done in 4th line of Controller Where I have Comment And Hard coded the "login" in 25th line i.e-"request.setEndpoint"

But I am getting the Same Error Message errMsg = 'Some error occurred, please try again';

I have created the Remote site Settings also..Am I doing something wrong here OR Not ?

  • Can you put a System.debug(e) in catch block and see in debug logs what exactly the error you are getting. – tandonprateek Jul 9 '14 at 15:55
  • @tandonprateek DEBUG|##############System.CalloutException: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = login.salesforce.com/services/Soap/u/22.0 – user40138 Jul 9 '14 at 15:59
  • ok ... as you already mentioned that you have added remote settings for end point but still you are getting this error. – tandonprateek Jul 9 '14 at 16:02
  • ya...If i Add whole URL(above Comment) in Remote Site..This will be the Error DEBUG|##############System.CalloutException: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = ap1.salesforce.com/services/data/v22.0/query/… – user40138 Jul 9 '14 at 16:06
  • 1
    you might also take a look at - cropredysfdc.com/2013/12/06/… – cropredy Jul 9 '14 at 18:34

Your Answer

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

Browse other questions tagged or ask your own question.