Salesforce | SOQL OFFSET clause is not supported in this organization

|
| By Webner

In Salesforce, we can limit the records returned by the soql query using LIMIT and OFFSET clauses. In our case where we tried to fetch salesforce Product records by using “Salesforce Rest API” using the following query:

$query = "Select unitprice,product2.id,product2.name,product2.description,product2.ProductCode from pricebookentry where product2.IsActive=true limit 20 offset 10";
$url = "$instance_url/services/data/v20.0/query?q=".urlencode($query);
$curl = curl_init($url);
$curl_setopt($curl, CURLOPT_HEADER, false);
$curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: OAuth $access_token"));
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$curl_close($curl);

The above code segment threw the error as:  “SOQL OFFSET clause is not supported in this organization”.

But when we tried to execute the same query in salesforce developer console, it worked fine with returning the required records.

This was because the SOQL OFFSET clause is only available in API version 24.0 and later. But in our code we’re using API version 20.0, that’s why it threw the error.

To resolve this we looked for the available Rest API Versions for our organization, which can be done by using the following rest URL:

curl https://yourInstance.salesforce.com/services/data/

It will list all the available Rest api versions for our organizations. And for using OFFSET clause we can use any version higher than 20.0. That is we can use it as:

 $url = "$instance_url/services/data/v37.0/query?q=".urlencode($query);
 $curl = curl_init($url);
 $curl_setopt($curl, CURLOPT_HEADER, false);
 $curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
 $curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: OAuth $access_token"));
 $json_response = curl_exec($curl);
 $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
 $curl_close($curl);

Leave a Reply

Your email address will not be published. Required fields are marked *