Query CRM Analytics Data with the Query API
Use the CRM Analytics REST API to directly query analytics data using SAQL or SQL queries.
You can use a REST API to query analytics datasets using either SAQL or SQL query statements. The query endpoint is wave/query and takes a POST request body using a SAQL Query Input to execute the query.
You can use the Lightning Web Component lightning/analyticsWaveApi module to bring this functionality into Salesforce, without proxying REST APIs directly. For more information, see the reference section for the executeQuery function.
- Example SAQL Query Request
For SAQL queries executed using REST API, you must have both the dataset ID and the dataset version ID to specify the dataset you want to query. Using the name of the dataset in the query returns an error. The only required attribute for the request is
query.- Example SAQL Query Response
The response results include
metadataandrecords. It also returns the original query and the response time. If there are warnings or errors,warningsis included. For the SAQL query executed in the previous example, here’s the response:- Example SQL Request Body
The default language for query execution is SAQL. To use SQL, include the attribute
"queryLanguage" : "SQL"in your request. For SQL queries, you can use the dataset name. Here’s the same query request in SQL.If you forget to specify
"queryLanguage" : "SQL", the request returns a syntax error.- Example SQL Query Response
The response records look the same as the SAQL response, but the
metadatais different. Thecolumnskey includes the name and type of projections in the query.- Example SQL Metadata: Group By Query With an Aggregation
The query projects the aggregation
avg(Sales)asAvgSales, which returns numerical data. In themetadata, the corresponding column type is returned as numeric.- Example SQL Query and Response Body: Extract Date Parts from a Date Field
This example returns the year, month, and day from the
CloseDatefield as numerical values. The request features thetimezoneattribute, which is attribute is optional and can only be used if timezone is enabled for the org.- Example SQL Query and Response Body: Project Date Field of Type
DateTime This query returns date information as timestamps.
- Query Metadata Details
Clients can parse the queries to figure out what dimensions and groups are used, but this can be expensive. So, in most cases, the query response contains a
metadatasection, which provides grouping and column information. Themetadatasection, if present, is found in theresultskey in the query response payload. Themetadatasection is structured withcolumnsandgroupskeys:The columns key includes the name and type of the projections of the query, and the groups key contains a list of groups used in the query.
- The metadata is added when the query is successful. If the query fails to run, if there’s a syntax error, or if the authorization callback fails then the metadata isn’t added to the results.
- The value set in a column name is the alias given to the projection and not the name of the dimension.
- A list of the groups used in the query is returned in the groups key, provided the query isn’t considered complex—where the group name returned is nondeterministic (the name of the group is used in multiple streams of the query). This is the case when the query uses cogroup or union. In such cases, the groups key is empty.
See Also