Configure return results data
Summarize
Summary of Configure return results data
Within the ServiceNow Mobile SDK, you can customize the data returned from a ServiceNow instance via REST endpoints by using theFetchConfigurationstructure. This configuration controls which records, fields, and the number of records are returned, ensuring the data matches your app’s model and avoids JSON decoding errors.
Show less
Configuring Records to Return
The Filter structure within FetchConfiguration lets you specify and sort records retrieved through the REST API’s sysparmquery parameter. There are four main ways to filter records:
- Encoded Query Filtering: Pass an encoded query string (e.g.,
active=true^shortdescriptionLIKEbroken) to filter records by conditions directly. - Criteria Filtering: Build complex filters using conditions combined with OR/AND logic to match multiple criteria (e.g., active incidents assigned to specific users or with escalations).
- Keywords and Conditions Filtering: Combine keyword searches with multiple AND’d conditions and sorting options to filter records.
- Conditions Filtering: Filter records that meet all specified conditions combined with AND logic, such as filtering active records with SLA due dates today or later.
Each filtering style uses a specific initializer for the Filter structure suited to the filtering complexity you need.
Configuring Fields to Return
The FieldReadConfiguration structure lets you specify which fields to return for each record and how to format their content, including options for display values, actual values, or excluding reference links. This precision allows you to tailor the returned data to match your model’s Codable properties, which can reduce payload size and improve network efficiency.
Practical Considerations
- Use consistent
FetchConfigurationacross CRUD operations for a model to ensure required fields are always returned and JSON decoding succeeds. - Careful filtering and field configuration optimize performance by limiting data transfer and focusing on relevant data for your mobile app.
By leveraging these configurations, ServiceNow customers can efficiently control the volume and structure of data retrieved in their mobile applications, resulting in better performance, reliability, and user experience.
Within the Mobile SDK, when interacting with data from a ServiceNow instance through a REST endpoint, you can configure what data is passed back in the return results.
- The specific records to return from a table.
- The specific fields to return from the records.
- The number of records to return.
Configuring the specific records to return
The Filter structure within a FetchConfiguration call enables you to filter and sort the records that are fetched from a ServiceNow instance and passed back in the return results from a REST endpoint. This filter is passed in the sysparm_query parameter of the REST API endpoint. For additional information, see Table API.
- Filter by encoded queryencoded query. This type of
filter enables you to pass an encoded query that is applied to the records within the
specified table.For example, the following call only returns those records that are active and whose short description field contains the word "broken":
Filter(query: "active=true^short_descriptionLIKEbroken"To use this type of filtering, initialize the Filter structure using the Filter - init(query: String, queryCategory: String? = nil) function.
- Filter by criteria. This filter enables you to filter based on one or more filter
criteria that are OR'd together.For example, a manager has an employee Abel Tuter who is on PTO. They want to know if there are active incidents that are not assigned to anyone or assigned to Abel Tuter so they can assign/reassign them. They also want to see in that same list, all of the incidents that have high or overdue escalation so that they can also address those. The following shows how to create the necessary filters to obtain this data:
let activeIncidents = Condition.boolean(field: "active", .is(true)) let assignedToEmpty = Condition.string(field: "assigned_to", .isEmpty) let assignedToAbel = Condition.string(field: "assigned_to", .is("Abel Tuter")) let assignedToAbelOrEmpty = CompoundCondition.or([assignedToEmpty, assignedToAbel]) let needToReassign = Criteria(conditions: [activeIncidents, assignedToAbelOrEmpty]) let highEscelations = Condition.string(field: "escalation", .is("2")) let overdueEscelations = Condition.string(field: "escalation", .is("3")) let highORoverdueEscelations = CompoundCondition.or([highEscelations, overdueEscelations]) let needToHandleEscalation = Criteria(condition: highOrOverdueEscelations) let myPrioritiesForTodayFilter = Filter(criteria: [needToReassign, needToHandleEscalation])To use this type of filtering, initialize the Filter structure using the Filter - init(criteria criteriaList: [Criteria], sortBy: [Sort]? = nil, queryCategory: String? = nil) function.
- Filter by keywords and conditions. This filter enables you to filter records based on
specific keywords and conditions that are AND'd together.
For example, the following filters for all records that contain the keyword "iOS13 or iOS 14" and meet the specified condition sorted by testField.
testKeywordsAndConditionsAndSort() { let keywords = "iOS 13 | iOS 14" let conditionA = Condition.boolean(field: testField, .is(true)) let conditionB = Condition.boolean(field: testField2, .is(false)) let sortA = Sort.asc(testField) let filter = Filter(keywords: keywords, conditions: [conditionA, conditionB], sortBy: [sortA]) XCTAssertEqual(filter.query, "123TEXTQUERY321=\(keywords)^\(conditionA.query)^\(conditionB.query)^EQ^\(sortA.query)") }To use this type of filtering, initialize the Filter structure using the Filter - init(keywords: String? = nil, conditions: [Condition], sortBy: [Sort]? = nil) function.
- Filter by conditions. This filter enables you to filter records that meet all
specified conditions (AND'd together).
For example, the following filters for all records in which the Active field is "true" and the date value of the SLA due field is on "today" or any date after today.
// Where the record is active let condition1 = Condition.boolean(field: "active", .is(true)) // AND the date value of the SLA due field is on "today" or any date after today. let condition2 = Condition.dateTime(field: "sla_due", .atOrAfter(0, .daysAgoStart)) let filter = Filter(conditions: [condition1, condition2])To use this type of filtering, initialize the Filter structure using the Filter - init(conditions: [QueryProviding], sorts: [Sort]? = nil) function.
Configuring the specific fields to return
- display values
- actual values
- exclude reference links