Performing calculations on data attributes
You can perform a calculation on a data attribute before it is displayed on the card.
- Example
- You might want to calculate the number of days after an expiration date. In that case, the
calculation takes today's date and subtracts the expiration date on the data attribute. This
calculation can be done with the transformation property.
- Example of a query
-
query InventoryExpired($tenantId: String, $cursorId: String, $now: String) { inventoryExpired:businessObjects(simpleFilter: {tenantId: $tenantId, type: InventoryLot}, advancedFilter: {AND: [ {LESS_EQUALS: [{SELECT: "expirationDate", type: STRING}, {type: STRING, VALUE: $now}]}, {EQUALS: [{SELECT: "class", type: STRING}, {type: STRING, VALUE: "NEW"}]}, {EQUALS: [{SELECT: "segment", type: STRING}, {type: STRING, VALUE: "UNSEGMENTED"}]}, {EQUALS: [{SELECT: "inventoryParentType", type: STRING}, {type: STRING, VALUE: "ONHAND"}]} ]}, cursorParams: {after: $cursorId, first: 10}, hint: {viewId: "inventorylotflow"}) { totalCount pageInfo { hasNextPage endCursor } edges { cursor object { ... on InventoryLot { id location { id locationName } product { id name } quantity quantityUnits expirationDate } } } } }
- Example of query variables
-
{ "tenantId": "{{ tenantId }}", "cursorId": "{{ cursorId }}", "now": "{{now}}" }
- Example of a transform
-
{ "errors": "{{ #? this.errors }}", "results": { "{{ #each this.data.inventoryExpired.edges }}": { "id": "{{ object.id }}", "class": "{{this.object.class}}", "expirationDate": "{{this.object.expirationDate}}", "location": "{{this.object.location}}", "product": "{{this.object.product}}", "quantity": "{{this.object.quantity}}", "quantityUnits": "{{ this.object.quantityUnits}}", "days": "{{Math.floor((new Date((Date.now() - new Date(this.object.expirationDate)))) /(60*60*1000 * 24)) }}" } }, "pageInfo": "{{ this.data.inventoryExpired.pageInfo }}", "totalCount": "{{ this.data.inventoryExpired.totalCount.toString() }}" }
Ensure that the alias of the query matches the transformation
this.data.inventoryExpired. The calculation for days is then the
value that is expected. This calculation might be anything, even a simple
this.object.expirationDate + 1. Now, the days column can be selected from the
available columns and can be displayed, as shown in the following example: