Adding queries to cards

To render the data for your card, you must add a valid query, query variables, and transform.

Procedure

  1. On the Data tab, in the Queries group, click Add to add a query.
  2. Optional: Edit the query.
    1. On the Data tab, in the Queries group, select the query that you want to edit.
    2. Click the overflow menu and select Edit.
  3. Optional: On the Define query page, build criteria for querying data.
    You can also build your query using the code editor. To use the code editor, see step 4.
    Note: The query builder supports only a single business object field selection. For multiple business object selections, use Code editor.
    1. Select a primary business object, for example ActionDefinition.
    2. On the Criteria tab, click Add criteria.
    3. Select a column, operand, and value to build your criteria.
      You can add multiple criteria and groups to refine your output.
    4. On the Variables tab, click Add variable to use the context variables in your query.
      If the context variables are already configured, then a list of the variables appears. Select the existing variable. If the context variables are not configured, you can configure a local variable to use in the query.
    5. Click Create variable and add the name, type, value, and default value fields.
      The scope of the local variable is limited to only the current query.
    6. Click Done
  4. Click Code editor to create a query.
    1. In the query section, create a query that is similar to the following example.

      The following query returns the data for a line chart.

      query orders($tenantId: String) {
        orderLineChart: businessObjects(
          simpleFilter: {tenantId: $tenantId, type: Order}
          hint: {viewId: "orderflow"}
          cursorParams: {first: 10, sort: {fieldPath: "plannedShipDate", order: ASC}}
          aggregationParams: {dimension: {fieldPath: "plannedShipDate", type: STRING}, reduce: {expression: {fieldPath: "quantity", operator: SUM, type: INT}, output: {resultAlias: "buyers", type: INT}}}
        ) {
          totalCount
          pageInfo {
            endCursor
            hasNextPage
          }
          edges {
            object {
              id
              ... on Order {
                buyer {
                  id
                  name
                }
                quantity
                plannedShipDate
              }
            }
          }
          aggregationResultsTotalCount
          aggregationResults {
            dimensionValues {
              name
              value
            }
            values {
              name
              value
            }
          }
        }
      }
    2. In the Query variables field, add variables as shown to the following example.
      {
        "tenantId": "{{ tenantId }}"
      }
      
    3. Click the Execute Query icon to run the query.
      The query results are displayed in the results section of the GraphiQL editor.
  5. Click Next.
  6. On the Transform page, in the Transform section, add a valid transform that is similar to the following example.
    
    
    {
    	"errors": "{{ #? this.errors }}",
    	"totalCount": "{{ this.data.orderLineChart.aggregationResultsTotalCount }}",
    	"pageInfo": "{}",
    	"results": {
    		"{{ #each this.data.orderLineChart.aggregationResults }}": {
    			"group": "orderLineChart",
    			"x-axis-key": "{{new Date(this.dimensionValues[0].value).toDateString()}}",
    			"y-axis-key": "{{this.values[0].value}}",
    			"tooltipsConfig": {
    				"name": "{{this.dimensionValues[0].name}}"
    			}
    		}
    	}
    }
    You can also continue using the default transformation that is applied to your card based on the card type selection. The default transform type is Select transform, but if your query needs a complex transformation, then you can select Javascript. After you change the transform type, make sure that your code still works correctly.
  7. Click Done
  8. To apply the changes, click Save.
The following table shows the card messages for invalid query elements and action to fix the query.
Table 1. Messages for invalid query elements
Card message Description Action
No transform When the Transform section of a query is empty for a card. Add a transform to the query.
Invalid transform When the transform is not valid for a card. Add a valid transform.
Invalid query When the query, query variables, or a transform is not valid for a card. Add a valid query, query variables, or a transform.
No queries When the Queries group is empty for a card. Add at least one query to render your card data.