NowAttachmentService interface - Android

  • Rversion finale: Australia
  • Mis à jour 12 mars 2026
  • 3 minutes de lecture
  • The NowAttachmentService interface provides functions that enable the manipulation of file attachments and their associated metadata.

    Tableau 1. Properties
    Name Type Description
    configuration NowServiceConfiguration Configuration to associate with the service.

    NowAttachmentService - attachment(sysId: String, validateAttachment: Boolean = true)

    Retrieves the attachment with the specified sys_id and optionally validates the attachment by comparing the computed hash of the attachment to the expected hash.

    Tableau 2. Parameters
    Name Type Description
    sysId String Sys_id of the attachment to retrieve. This is the sys_id for the attachment on your ServiceNow instance.
    validateAttachment Boolean

    Flag that indicates whether to validate the attachment.

    Valid values:
    • true: Validate the attachment.
    • false: Do not validate the attachment.

    Default: true

    Tableau 3. Returns
    Type Description
    Call<NowAttachment> NowAttachment object that contains the requested attachment.

    fun getAttachment(sysId: String, isValidateAttachment: Boolean) { 
      val call = attachmentService.attachment(sysId, isValidateAttachment) 
      call.enqueue( 
        { response -> 
          val attachment: NowAttachment? = response.body 
        }, 
          { nowDataError -> handleError(nowDataError) } 
      ) 
    }

    NowAttachmentService - attachmentMetadata(sysId: String)

    Retrieves the metadata for the attachment associated with the specified sys_id.

    Tableau 4. Parameters
    Name Type Description
    sysId String Sys_id of the attachment whose metadata you want to retrieve.
    Tableau 5. Returns
    Type Description
    Call​<NowAttachmentMetadata> Object that contains the metadata for the specified attachment.
    fun fetchMetadata(sysId: String) { 
      val call = attachmentService.attachmentMetadata(sysId) 
      call.enqueue(
        { response -> 
          val metadata: NowAttachmentMetadata? = response.body 
        },
        { nowDataError -> handleError(nowDataError) } 
      )
    }

    NowAttachmentService - attachmentMetadata(filter: Filter? = null, limit: Int? = null)

    Retrieves the metadata for all the attachments that meet the specified criteria.

    Tableau 6. Parameters
    Name Type Description
    filter Filter Optional. Query string to use to filter the attachments whose metadata to return.

    Default: null - Returns metadata for all available attachments. Takes into consideration the limit parameter.

    limit Integer Optional. Maximum number of attachment file's metadata to return.

    Default: null - Returns all metadata that meets the filter parameter specifications.

    Tableau 7. Returns
    Type Description
    Call<List​<NowAttachmentMetadata>> List of objects that contain the metadata for the matching attachments.
    fun fetchMultipleMetadata(filterQuery: String, limit: Int) { 
      val call = attachmentService.attachmentMetadata(Filter(filterQuery), limit) 
      call.enqueue( 
        { response -> 
          val metadataList: List<NowAttachmentMetadata>? = response.body 
        },
        { nowDataError -> handleError(nowDataError) } 
      )
    }

    NowAttachmentService - attachmentMetadataPaginator(filter: Filter? = null, limit: Int? = null)

    Retrieves the metadata for all the attachments that meet the specified criteria and creates a paginator for iterating through the pages of the returned metadata.

    You can use this paginator to navigate through the returned metadata, performing navigation operations such as fetching the first, last, previous, or next page, or checking whether there is next or previous page.

    Tableau 8. Parameters
    Name Type Description
    filter Filter Optional. Query string to use to filter the attachments whose metadata to return.

    Default: null - Returns metadata for all available attachments. Takes into consideration the limit parameter.

    limit Integer Optional. Maximum number of attachment file's metadata to return.

    Default: null - Returns all metadata that meets the filter parameter specifications.

    Tableau 9. Returns
    Type Description
    Paginator​<NowAttachmentMetadata> Success: Paginator object along with the specified pages of metadata.

    Failure: NowDataError object.

    The following code example shows how to call this function.

    suspend fun createAttachmentMetadataPaginator() {
      val filterQuery: String = "content_type=text/plain"
      val filter = filterQuery.let(::Filter)
      val limit = 10
      val paginator = nowServiceManager.getNowAttachmentService()?.attachmentMetadataPaginator(filter, limit)
        ?.observe(object : PaginatorCallBack<NowAttachmentMetadata> {
          override fun onFailure(e: NowDataError) {
            handleError(e)
          }
    
          override fun onSuccess(response: Response<List<NowAttachmentMetadata>>) {
            handleResponse(response)
          }
    
        })
        ?: throw Exception("Response is null")
    }

    NowAttachmentService - delete(sysId: String)

    Deletes the attachment with the specified sys_id.

    Tableau 10. Parameters
    Name Type Description
    sysId String Sys_id of the attachment to delete.
    Tableau 11. Returns
    Type Description
    Call<ByteArray> Success: Nothing is returned.

    Failure: NowDataError returned.

    fun deleteAttachment(sysId: String) { 
      val call = attachmentService.delete(sysId) 
      call.enqueue( 
        { response -> handleResponse(response) }, 
        { nowDataError -> handleError(nowDataError) } 
      )
    } 

    NowAttachmentService - upload(data: ByteArray, configuration: NowAttachmentUploadConfiguration)

    Retrieves the metadata for all the attachments that meet the specified criteria and creates a paginator for iterating through pages of the returned metadata.

    Tableau 12. Parameters
    Name Type Description
    data ByteArray Metadata to upload and associate with the attachment specified in the configuration object.
    configuration NowAttachmentUploadConfiguration Upload configuration parameters.
    Tableau 13. Returns
    Type Description
    NowAttachmentMetadata Uploaded metadata.
    fun uploadAttachment(tableName: String, recordSysId: String, fileName: String) { 
      val bitmap = BitmapFactory.decodeResource(resources, R.drawable.test_image) 
      val data = bitmap.compress(ImageType.JPEG) 
      val contentType = "image/jpg" 
      val config = NowAttachmentUploadConfiguration(tableName, recordSysId, fileName, contentType) 
      val call = attachmentService.upload(data, config) 
     
      call.enqueue( 
        { response -> 
          val uploadedAttachmentMetadata: NowAttachmentMetadata? = response.body 
        },
        { nowDataError -> handleError(nowDataError) } 
      )
    }