インスタンス内の ServiceNow テーブルデータを操作する
を使用すると Mobile SDK 、インスタンスに存在する ServiceNow テーブルのデータを操作できます。このデータは、ServiceNowREST インターフェイスを直接呼び出す NowTableService() API を使用して REST テーブル API を介して、または REST GraphQL API に対して指定された GraphQL クエリを実行する NowGraphQLService() API を介して操作できます。
1 回の呼び出しで複数のテーブルからデータを返す場合は、 NowGraphQLService() API を使用する必要があります。テーブル API 以外の REST API とServiceNowやり取りする必要がある場合は、を参照してくださいインスタンスで ServiceNow パブリック REST API を操作する。
NowGraphQLService を使用したテーブルの操作ServiceNow
NowGraphQLService クラスは、ログインしたユーザーが適切な権限を持っている場合に、インスタンス内のServiceNow指定されたテーブルで GraphQL クエリを構築して実行できるようにするメソッドを提供します。GraphQL クエリ内のすべての CRUD 操作を定義できます。
/**
* Helper class used to handle different Now service instances.
*/
@Singleton
class SdkManager @Inject constructor() {
private var nowGraphQLService: NowGraphQLService? = null
/**
* Create the NowGraphQLService once in the lifetime of the application inside the Application class or another manager class
* that will be injected into other classes via dagger/hilt.
* NowGraphQLService should be created after initializing the NowSDK
*/
suspend fun getNowGraphQLService(): NowGraphQLService? {
if (nowGraphQLService != null) return nowGraphQLService
return NowDataSDK.makeGraphQLService(URL("https://instance-name.service-now.com")).getOrThrow()
.also { this.nowGraphQLService = it }
}
}suspend fun loadList() = withContext(Dispatchers.IO) {
val kbArticlesQuery = """
{
GlideRecord_Query {
kb_knowledge(queryConditions: "active=true^ORDERBYpublishedDESC"
pagination:{ limit: 10, offset: 0 }) {
_results {
sys_id { value },
number { displayValue },
short_description { displayValue },
author { displayValue }
published { displayValue }
}
}
}
}
"""
val graphQLService = sdkManager.getNowGraphQLService()
//graphQLService?.graphQLRequest(kbArticlesQuery)?.execute() can also be used
graphQLService?.graphQLRequest(kbArticlesQuery)?.enqueue(
{ response ->
response.body?.let {
val resultString = String(it)
}
}, { nowDataError ->
//handle error
})
}NowTableService を使用したテーブルの操作ServiceNow
NowTableService クラスは、インスタンス上ServiceNowに存在するテーブルのレコードに対して CRUD 操作を実行するためのメソッドを提供します。このインターフェイスを介して、ログインしているユーザーが許可されているテーブル ServiceNow 内の任意のレコードに直接アクセスできます。NowTableService は、参照フィールドのドット連結をサポートしています。たとえば、テーブルにユーザーテーブルへの参照が含まれている場合、ドット連結の値 user.name ユーザーの名前を返します。
要求されたデータを返すと、該当するすべてのアクセス制御リスト (ACL) がデータに適用されます。その結果、予想よりも結果が少なくなったり、認証されたユーザーが指定されたテーブルへのアクセス権を持っていない場合は認証エラーが発生する可能性があります。
/**
* Helper class used to handle different Now service instances. It has an application scope or is Singleton
*/
@Singleton
class SdkManager @Inject constructor() {
private var nowTableService: NowTableService? = null
/**
* Create the NowTableService once in the lifetime of the application inside the Application class or another manager class
* that will be injected into other classes via dagger/hilt.
* NowTableService should be created after initializing the NowSDK
*/
suspend fun getNowTableService(): NowTableService? {
if (nowTableService != null) return nowTableService
return NowDataSDK.makeTableService(URL("https://instance-name.service-now.com")).getOrThrow()
.also { this.nowTableService = it }
}
}
suspend fun loadCases() = withContext(Dispatchers.IO) {
val tableService = sdkManager.getNowTableService()
//configure what data to pass back
val fetchConfiguration = FetchConfiguration(Filter("active=true"), 10)
val response = runCatching {
tableService?.records(
"sn_customerservice_case",
fetchConfiguration
)?.execute()
}
if (response.isSuccess) {
val resultString = response.getOrNull()?.body?.let { String(it) }
} else {
//handle unsuccessful result
}
}利用可能な NowTableService メソッドを使用するその他のコード例については、NowTableService API のNowTableService インターフェイス - Androidドキュメントを参照してください。