Hi folks, In this tutorial I am going to show you how to fetch records from sobjects using REST API in apex class salesforce. This is also an approach to overcome salesforce “Too Many Query Rows 50001” limit exception.

How to fetch records from sobjects using REST API in apex class salesforce:

Before starting add your salesforce base url to remote setting in my case i have added https://devlighting-dev-ed–c.na46.visual.force.com to remote setting to whitelist my salesforce URL.

Create Apex Controller:

Step 1- Goto Setup and open Developer Console.

Step 2- Create an apex class called RESTAccountController. Replace the following code in the class.

public class RESTAccountController {
 public List<Account> AccountList { get
 {
 
 HttpRequest req = new HttpRequest();
 req.setEndpoint('https://'+URL.getSalesforceBaseUrl().getHost()+'/services/data/v39.0/query/?q=SELECT+Id,Name,AccountNumber,Phone+FROM+Account');
 req.setMethod('GET');
 
 //Get SessionId
 string autho = 'Bearer '+userInfo.getSessionId();
 req.setHeader('Authorization', autho);
 
 //Get Response
 Http http = new Http();
 HTTPresponse res= http.send(req);
 string response = res.getBody();
 
 //Deserialize obtained json response
 string sData = '['+ response.substring(response.indexOf('records":[')+10,response.indexof(']}')) +']';
 List<Account> lstAccount = (list<Account>)JSON.deserialize(sData,list<Account>.class);
 return lstAccount;
 
 }
 set;
 }
}

Where,

URL.getSalesforceBaseUrl().getHost(): is used to get host name from salesforce base url

string autho = ‘Bearer ‘+userInfo.getSessionId();
req.setHeader(‘Authorization’, autho);

This line of code is used to authorize your request.

Create Visualforce Page:

Create a Visualforce Page called DisplayAccount. Write the following code.

<apex:page controller="RESTAccountController">
 
 <apex:sectionHeader title="Accounts" subtitle="List View"/>
 <apex:pageBlock>
 
 <apex:pageBlockTable value="{!AccountList}" var="key">
 
 <apex:column>
 <apex:facet name="header">Account Name</apex:facet>
 <apex:outputLink value="/{!key.Id}">{!key.Name}</apex:outputLink>
 </apex:column>
 <apex:column value="{!key.AccountNumber}"/>
 <apex:column value="{!key.Phone}"/> 
 
 </apex:pageBlockTable>
 
 </apex:pageBlock>
 
</apex:page>

Output:

REST API

View More:

Conclusion:

I hope you would love this post. Please don’t hesitate to comment for any technical help. I would appreciate your feedback and suggestions.

Thank You.

6 Comments

  • There are definitely a great deal of information like that to think about. That is an excellent point to bring up. I supply the thoughts above as basic motivation however clearly there are questions like the one you raise where one of the most crucial thing will certainly be operating in sincere good faith. I don?t know if ideal methods have actually arised around things like that, but I make certain that your work is clearly determined as a level playing field. Both boys and women really feel the effect of just a moment?s satisfaction, for the remainder of their lives.

  • That is really interesting, You are an excessively skilled blogger. I have joined your feed and sit up for seeking more of your great post. Additionally, I have shared your website in my social networks!

  • Definitely believe that which you stated. Your favorite reason appeared to be on the net the simplest thing to be aware of. I say to you, I definitely get irked while people think about worries that they just don’t know about. You managed to hit the nail upon the top and defined out the whole thing without having side-effects , people can take a signal. Will probably be back to get more. Thanks

  • I just like the helpful information you supply for your articles. I’ll bookmark your blog and test once more right here regularly. I’m somewhat sure I will be told lots of new stuff right right here! Good luck for the following!|