{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":357,"path":"api-reference/developer-guides/mobsdk-ios-interact_data_instance.md","title":"Interact with table data in a ServiceNow instance","bundle":null,"doc_type":null,"release":"australia","body":"\n# Interact with table data in a ServiceNow instance\n\nThe Mobile SDK enables you to interact with data in tables that reside on your ServiceNow instance. You can interact with this data through the ServiceNow REST Table API using theNowTableService\\(\\) API that directly calls the REST interface, or through the NowGraphQLService\\(\\) API that executes a specified GraphQL query against the REST GraphQL API.\n\nIf you want to return data from multiple tables within a single call, you will need to use the NowGraphQLService\\(\\) API. If you need to interact with other ServiceNow REST APIs, see [Interact with the public REST API on a ServiceNow instance](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/developer-guides/mobsdk-ios-interact-rest_api.md).\n\n## Using NowGraphQLService to interact with ServiceNow tables\n\nThe [NowGraphQLService](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/cllent-mobile-api-reference/NowGraphQLServiceiOSAPI.md) class provides methods that enable you to construct and execute GraphQL queries on a specified table within your ServiceNow instance if the logged in user has the proper authorization. You can define all CRUD operations within your GraphQL query.\n\nNowGraphQLService supports fetching results for a query by returning a completion handler:\n\n```\nfunc execute(_ query: GraphQLQuery, completion: @escaping (Result<Data, NowDataError>) → Void)\n```\n\nThrough async/await:\n\n```\nfunc execute(_ query: GraphQLQuery) async throws -> Data\n```\n\nOr, a [Combine](https://developer.apple.com/documentation/combine) publisher \\(which is currently deprecated\\):\n\n```\nfunc publisher(for query: GraphQlQuery) → AnyPublisher<Data, NowDataError>\n\n```\n\nThe following example shows how to import the NowData framework and then initialize a NowGraphQLService object that you can then use to interact with tables in your ServiceNow instance.\n\n```\n// Import the NowData framework\nimport NowData\n\n// Initialize a NowGraphQLService\n// The makeGraphQLService completion handler returns a result that wraps \n// the NowGraphQLService upon success or error. \n\nprivate func initializeGraphQLService(instanceUrl: URL) {\n  makeGraphQLService(instanceUrl: instanceUrl) { [weak self] result in\n    switch result {\n    case .success(let service):\n      self?.graphQLService = service\n    case .failure(let error):\n      debugPrint(\"Creating GraphQL service failed with error: \\(error.localizedDescription)\")\n      self?.graphQLService = nil\n    }\n  }\n}\n```\n\nThis example shows how to create an async function that fetches and publishes the most recently published KB articles.\n\n```\nprivate func fetchArticles() async throws -> [Article] { \n    guard let graphQLService = graphQLService else { \n        throw ArticleListError.invalidGraphQLService \n    }         \n\n    do {\n        let data = try await graphQLService.execute(GraphQLQuery(query: recentlyPublishedQuery))\n        let articles = try dataToArticleList(data)\n    } catch let error as NowDataError {\n        throw ArticleListError.fetchError(error)\n    } catch {\n        throw error\n    }\n} \n  \nprivate let recentlyPublishedQuery = \n\"\"\" \n\n{\n  GlideRecord_Query {\n    kb_knowledge(queryConditions: \"active=true^ORDERBYpublishedDESC\" pagination: { limit: 10, offset: 0 }) {\n      _results { \n        sys_id {\n          value\n        },\n        number {\n          displayValue\n        },\n        short_description {\n          displayValue\n        },\n        author {\n          displayValue\n        }\n        published {\n          displayValue\n        }\n      }\n    }\n  }\n}\n\"\"\"\n```\n\n## Using NowTableService to interact with ServiceNow tables\n\nThe [NowTableService](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/cllent-mobile-api-reference/NowTableServiceiOSAPI.md) class provides methods to perform CRUD operations on the records in tables that reside on your ServiceNow instance. Through this interface you can directly access any of the records within any ServiceNow table for which the logged in user is authorized. NowTableService supports dot-walking for reference fields. For example, if a table contains a reference to the User table, the dot-walked value `user.name` returns the name of the user.\n\nWhen returning the requested data, all applicable Access Control Lists \\(ACLs\\) are applied to the data, which may result in fewer results than expected, or authorization errors if the authenticated user does not have access rights to the specified table.\n\nMost NowTableService methods provide three implementations for returning results data. One that uses async/await, one that calls a completion handler with a **Result** enumeration, and one that returns a [Combine](https://developer.apple.com/documentation/combine) publisher, which is currently deprecated.\n\nFor example, all createRecord\\(\\) methods insert a constructed record into a specified table, however, the\n\n`[NowTableService - createRecord\\(with fields: \\[FieldName: FieldValue\\], in tableName: String, writeOptions: FieldWriteOptions, configuration: FieldReadConfiguration, completion: @escaping \\(Result&lt;Data, NowDataError&gt;\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/cllent-mobile-api-reference/NowTableServiceiOSAPI.md)`\n\nmethod calls a completion handler with a **Result** enumeration \\(the `.success` case contains the results as its associated value\\).\n\nThe\n\n`[NowTableService - createRecord\\(with fields: \\[FieldName: FieldValue\\], in tableName: String, writeOptions: FieldWriteOptions? = nil, configuration: FieldReadConfiguration? = nil\\) async throws](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/cllent-mobile-api-reference/NowTableServiceiOSAPI.md)`\n\nmethod performs an async/await function. While the\n\n`[NowTableService - createRecord\\(with fields: \\[FieldName: FieldValue\\], in tableName: String, writeOptions: FieldWriteOptions? = nil, configuration: FieldReadConfiguration? = nil\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/cllent-mobile-api-reference/NowTableServiceiOSAPI.md)`\n\nmethod returns a Combine publisher, but is currently deprecated.\n\nIn addition, NowTableService provides two different implementations of CRUD methods, one that returns raw data, and one that returns Codable models. For example, both the update\\(\\) and updateRecord\\(\\) methods update a specified record in a specified table, but the update\\(\\) methods return a Codable model and the updateRecord\\(\\) methods return raw data. In some cases, Codable methods might be preferable, as a strongly typed Codable structure provides more flexibility than a collection of raw data fields.\n\nThe following example shows how to import the NowData framework and then initialize a NowTableService object that you can then use to interact with ServiceNow instance tables.\n\n```\n// Import the NowData framework\nimport NowData\n\n// Initialize a NowTableService\n// The makeTableService completion handler returns a result that wraps \n// the NowTableService upon success or error. \n\nmakeTableService(instanceUrl: instanceUrl) { [weak self] result in\n  guard let self = self else { return }\n  \n  switch result {\n  case .success(let tableService):\n    debugPrint(\"Successfully created a table service \\(tableService)\")\n    self.tableService = tableService\n  case .failure(let error):\n    debugPrint(\"Creating table service failed with error: \\(error.localizedDescription)\")\n  }\n}\n```\n\nFor additional code examples using the available NowTableService methods, refer to the `[NowTableService class - iOS](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/cllent-mobile-api-reference/NowTableServiceiOSAPI.md)`API documentation.\n\n"}}