添付ファイルと添付ファイルのメタデータを取得

  • リリースバージョン: Yokohama
  • 更新日 2025年01月30日
  • 所要時間:6分
  • NowAttachmentService API を使用すると、添付ファイルに対して CRUD 操作を実行し、ServiceNowインスタンスから添付ファイルのメタデータを取得できます。

    注:
    NowAttachment API は、ベース ServiceNow システムのゲストユーザーに対して機能します。
    この API を使用すると、次のことができます。
    • 添付ファイルを ServiceNow インスタンスにアップロードして、特定のレコードに関連付けます。
    • 1 つまたは複数の添付ファイルをダウンロードします。
    • 添付ファイルを削除します。
    • 添付ファイルの計算済みハッシュを予想されるハッシュと比較して、添付ファイルを検証します。
    • 添付ファイルのメタデータをダウンロードします。このメタデータは、添付ファイルがアップロードされるときに ServiceNow インスタンスによって生成されます。

    添付ファイルの操作の詳細については、「 添付ファイル API」を参照してください。

    NowAttachmentService をインスタンス化する方法を次に示します。
    /**
     * Helper class used to handle different Now service instances. 
     * It has an application scope or is a Singleton.
     */
    @Singleton
    class SdkManager @Inject constructor() {
    
    
        private var nowAttachmentService: NowAttachmentService? = null
    
        /**
         * Create the NowAttachmentService 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.
         * NowAttachmentService should be created after initializing the NowSDK
         */
        suspend fun getNowAttachmentService(): NowAttachmentService? {
            if (nowAttachmentService != null) return nowAttachmentService
    
            return NowDataSDK.makeAttachmentService(URL("https://instance-name.service-now.com"))
                .getOrThrow()
                .also { this.nowAttachmentService = it }
        }
    }

    特定の添付ファイルのメタデータをフェッチする Call オブジェクトを取得する方法を次に示します。指定された要求を実行し、 Response<NowAttachmentMetadata>を返すことができる実行可能オブジェクトを作成します。

    suspend fun fetchMetadata(sysId: String) {
        val response = runCatching {
            sdkManager.getNowAttachmentService()?.attachmentMetadata(sysId)?.execute()
        }
    
        if (response.isSuccess) {
            val nowAttachmentMetadata = response.getOrNull()?.body
        } else {
            Log.e("NowSDK", "result not successful")
        }
    }

    複数の添付ファイルのメタデータをフェッチする Call オブジェクトを取得する方法を次に示します。指定された要求を実行し、 Response< List<NowAttachmentMetadata>>を返すことができる実行可能オブジェクトを作成します。

    suspend fun fetchMultipleMetadata() {
        val filterQuery = "content_type=text/plain"
        val limit = 5
    
        val response = runCatching {
            sdkManager.getNowAttachmentService()?.attachmentMetadata(Filter(filterQuery), limit)?.execute()
        }
    
        if (response.isSuccess) {
            val attachmentMetadataList = response.getOrNull()?.body
        } else {
            Log.e("NowSDK", "result not successful")
        }
    }

    添付ファイルメタデータのページネーション

    NowAttachmentService メソッドを使用して、1 つ以上の添付ファイルの添付ファイルメタデータをダウンロードできます。複数の添付ファイルからメタデータをダウンロードする場合は、 NowAttachmentService - attachmentMetadataPaginator(フィルター: フィルター? = null, limit: Int? = null) メソッドを使用して、返される可能性のある大量のデータを簡単に反復処理できる Paginator オブジェクトを返すことができます。

    次に、Paginator メソッドを使用して返されたメタデータを反復処理し、 observe() コールバックで応答を受け取ることができます。
     suspend fun createAttachmentMetadataPaginator() {
      val filterQuery: String = "content_type=text/plain"
      val filter = filterQuery.let(::Filter)
      val limit = 10
    
      val paginator = sdkmanager.getNowAttachmentService()?.attachmentMetadataPaginator(filter, limit)
        ?.observe(object : PaginatorCallBack<NowAttachmentMetadata> {
          override fun onSuccess(response: Response<List<NowAttachmentMetadata>>) {
            // Handle response
          }
    
          override fun onFailure(e: NowDataError) {
            // Handle error
          }
    
        })
        ?: throw Exception("Response is null")
    
      // Use paginator operators to navigate. Example
      while (paginator.hasNext() && !paginator.isBusy()) {
        paginator.next()
      }
    }
    返された Paginator オブジェクトは、返されたレコードをページングできるようにする次のメソッドを提供します。最初に observe() 関数を呼び出して、Paginator のコールバックを設定する必要があります。
    • 最初の()
    • hasNext()
    • 前へ
    • ビジー状態
    • 最後 (Last()
    • 次へ()
    • 観察()
    • 前()
    • reset()
    注:
    一部の Paginator メソッドは、フェッチするページがなくなった場合など、例外をスローする場合があります。