function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Durai PandiDurai Pandi 

ApexREST and NextRecordsURL

We can fetch 200 to 2000 records in REST API using NextRecordsURL and 'Sforce-Query-Options: batchSize'  request header field.
I want to implement similar functionality in my ApexREST so that the resource returns more than 50K records and send batch by batch based on the header parameter value passed as in REST API.

Highly appreciate anyone shed light on this issue.
Varsha TomarVarsha Tomar
I recently implemented similar requirement successfully. You can add a response parameter in your API response in which you can pass a url. 
You can name it as NextRecordsURL.
Prepare a URL and pass required parameters (like start index, records size, etc.) in that URL which you need to send next set of records
This URL will then be used by other system to invoke next request.
When all records are send, pass null in NextRecordsURL.
When the other system receives null then they should stop invoking the request.

Hope it helps!!

Thanks
Varsha
Durai PandiDurai Pandi
@Varsha Tomar,

Thanks for your response. 
If we are manually building the URL, do we need to call the service again?

Can you please clarify?
Varsha TomarVarsha Tomar
Yes you need to call service again and again untill all records are sent.
Durai PandiDurai Pandi
ok.

I am curious about how the Query API works.
If the query returned more than 2K records, along with the response it sends the NextRecordsURL. 

/services/data/v37.0/query/01gp000000AJ51VAAT-800

In the above nextRecordsUrl, does the ID reflect the API or something else?
Varsha TomarVarsha Tomar
This is standard API URL. My explanation was for custom Apex Rest API.
That solution will work for custom Apex Rest API.
What you are looking for? Standard API URL or creating Custom RestResource API apex class?
Durai PandiDurai Pandi
I am looking for Apex REST.
But just wanted to understand how the standard one works.

Let me try the one you suggest.
Zakeer Mohammed 20Zakeer Mohammed 20
Hi Varsha,

Could you please share your code here?

-
Thanks,
Zakeer.