인스턴스에서 테이블 데이터와 ServiceNow 상호작용

  • 릴리스 버전: Washingtondc
  • 업데이트 날짜 2024년 02월 01일
  • 읽기6분
  • Mobile SDK 사용하면 인스턴스에 있는 테이블의 데이터와 상호작용할 수 있습니다 ServiceNow . REST 인터페이스를 직접 호출하는 NowTableService() API를 사용하여 REST 테이블 API를 통해 ServiceNow 또는 REST GraphQL API에 대해 지정된 GraphQL 쿼리를 실행하는 NowGraphQLService() API를 통해 이 데이터와 상호작용할 수 있습니다.

    단일 호출 내에서 여러 테이블의 데이터를 반환하려면 NowGraphQLService() API를 사용해야 합니다. 테이블 API 이외의 REST API와 ServiceNow 상호작용해야 하는 경우 을 참조하십시오인스턴스에서 공용 REST API와 ServiceNow 상호작용.

    NowGraphQLService를 사용하여 테이블과 ServiceNow 상호 작용

    NowGraphQLService 클래스는 로그인한 사용자에게 적절한 권한이 있는 경우 인스턴스 내의 지정된 테이블에 대해 GraphQL 쿼리를 생성하고 실행할 수 있는 ServiceNow 메서드를 제공합니다. GraphQL 쿼리 내에서 모든 CRUD 작업을 정의할 수 있습니다.

    다음 예제에서는 NowData 프레임워크를 가져온 다음 인스턴스의 테이블과 상호 작용하는 데 사용할 수 있는 NowGraphQLService 개체를 초기화하는 방법을 보여 줍니다 ServiceNow .
    /**
     * 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 }
        }
    }
    이 예제에서는 GraphQL 쿼리를 사용하여 게시된 KB 문서를 가져오는 방법을 보여줍니다.
    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 클래스는 인스턴스에 있는 테이블의 레코드에 대해 CRUD 작업을 수행하는 메서드를 제공합니다ServiceNow. 이 인터페이스를 통해 로그인한 사용자에게 권한이 부여된 테이블 ServiceNow 내의 기록에 직접 액세스할 수 있습니다. NowTableService 는 참조 필드에 대한 닷워킹을 지원합니다. 예를 들어 테이블에 사용자 테이블에 대한 참조가 포함되어 있는 경우 닷워킹 값은 사용자의 이름을 반환 user.name .

    요청된 데이터를 반환할 때 적용 가능한 모든 ACL(접근 제어 목록)이 데이터에 적용되어 예상보다 적은 결과가 발생하거나 인증된 사용자에게 지정된 테이블에 대한 액세스 권한이 없는 경우 권한 부여 오류가 발생할 수 있습니다.

    다음 예제에서는 NowData 프레임워크를 가져온 다음 인스턴스 테이블과 ServiceNow 상호 작용하는 데 사용할 수 있는 NowTableServce 개체를 초기화하는 방법을 보여 줍니다.
    /**
     * 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 참조하세요.