Mobile SDK - Android › Mobile SDK API reference › API reference › API implementation and reference
NowAttachmentService interface- Android
The NowAttachmentService interface provides functions that enable the manipulation of file attachments and their associated metadata.
Parent Topic:Mobile SDK - Android
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.
| 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 |
| 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) }
)
}
Retrieves the metadata for the attachment associated with the specified sys_id.
| Name | Type | Description |
|---|
| sysId | String | Sys_id of the attachment whose metadata you want to retrieve. |
fun fetchMetadata(sysId: String) {
val call = attachmentService.attachmentMetadata(sysId)
call.enqueue(
{ response ->
val metadata: NowAttachmentMetadata? = response.body
},
{ nowDataError -> handleError(nowDataError) }
)
}
Retrieves the metadata for all the attachments that meet the specified criteria.
| 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. |
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) }
)
}
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.
| 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. |
| 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.
| Name | Type | Description |
|---|
| sysId | String | Sys_id of the attachment to delete. |
| 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.
| Name | Type | Description |
|---|
| data | ByteArray | Metadata to upload and associate with the attachment specified in the configuration object. |
| configuration | NowAttachmentUploadConfiguration | Upload configuration parameters. |
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) }
)
}