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
Ryan McLaughlinRyan McLaughlin 

How to use sortBy in Analytics api

I am trying to run a report through the analytics api and use the sortBy field as specified here https://developer.salesforce.com/docs/atlas.en-us.api_analytics.meta/api_analytics/sforce_analytics_rest_api_getbasic_reportmetadata.htm

The documentation does not mention how the sortBy field should be formatted, so I have been guessing. Like it says in the Docs "API name of the field on which the report is sorted and the direction of the sort (asc or desc)." I have put in the name of the field and asc seperated by a space. it looks something like this "sortBy":"LEAD_ID asc"

However whenever I put in a value for sortby I get an error back saying "The request body is either invalid or incomplete."

If I remove the sortBy it works fine.

Any help would be appreciated
Best Answer chosen by Ryan McLaughlin
Ryan McLaughlinRyan McLaughlin
So i figured it out myself. Apparantly the API doc is wrong. The SortBy field is not a string, it is an array, but it is only allowed to have one element. The Array has to look like this "sortBy":[{"sortColumn":"LEAD_ID","sortOrder":"Asc"}], 

All Answers

Jayson Faderanga 14Jayson Faderanga 14
it should be like

ORDER BY Name ASC

hope that helps :D
Ryan McLaughlinRyan McLaughlin
Unfortuantly, that does not work either. I get the same error.

I also find it interesting that in the above link sortBy is labeled as a string, but in some code examples https://resources.docs.salesforce.com/sfdc/pdf/salesforce_analytics_rest_api.pdf (https://resources.docs.salesforce.com/sfdc/pdf/salesforce_analytics_rest_api.pdf" target="_blank) sortBy is shown as an array
"reportBooleanFilter" : null,
"reportFilters" : [ ],
"reportFormat" : "TABULAR",
"reportType" : {
"label" : "Accounts",
"type" : "AccountList" },
"scope" : "user",
"sortBy" : [ ],
"standardDateFilter" : {
"column" : "CREATED_DATE",
"durationValue" : "CUSTOM",
"endDate" : null,
"startDate" : null },
"standardFilters" : null },

 
Ryan McLaughlinRyan McLaughlin
So i figured it out myself. Apparantly the API doc is wrong. The SortBy field is not a string, it is an array, but it is only allowed to have one element. The Array has to look like this "sortBy":[{"sortColumn":"LEAD_ID","sortOrder":"Asc"}], 
This was selected as the best answer