Retrieve attachments and attachment metadata
Summarize
Summary of Retrieve Attachments and Attachment Metadata
The NowAttachmentService API facilitates CRUD operations on attachments and allows retrieval of attachment metadata from your ServiceNow instance. This API is accessible for guest users in the base ServiceNow system, enabling critical functions such as uploading, downloading, and deleting attachments, as well as validating their integrity and fetching associated metadata.
Show less
Key Features
- Upload Attachments: Attachments can be uploaded and linked to specific records within ServiceNow.
- Download Attachments: Users can download one or multiple attachments as needed.
- Delete Attachments: Unwanted attachments can be deleted to manage storage effectively.
- Validate Attachments: You can ensure attachment integrity by comparing computed hashes.
- Retrieve Metadata: Metadata related to attachments can be downloaded, providing insights into each attachment's details.
- Pagination for Metadata: The Paginator feature allows for efficient navigation through extensive lists of attachment metadata.
Key Outcomes
By utilizing the NowAttachmentService API, ServiceNow customers can streamline their attachment management processes, ensuring data integrity and providing easy access to essential attachment information. This functionality enhances operational efficiency and supports better data governance within ServiceNow environments.
The NowAttachmentService API enables you to perform CRUD operations on attachments and retrieve attachment metadata from your ServiceNow instance.
- Upload attachments to your ServiceNow instance and associated them to a specific record.
- Download one or more attachments.
- Delete attachments.
- Validate an attachment by comparing the computed hash of the attachment to the expected hash.
- Download attachment metadata. This metadata is generated by your ServiceNow instance when an attachment is uploaded.
For additional information on working with attachments, see Attachment API.
/**
* 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 }
}
}The following shows how to obtain a Call object that fetches metadata for a particular attachments. It creates an
executable object that is able to perform the specified request and return
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")
}
}The following shows how to obtain a Call object that fetches metadata for multiple attachments. It creates an
executable object that is able to perform the specified request and return
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")
}
}Attachment metadata pagination
You can use the NowAttachmentService methods to download attachment metadata for one or more attachments. When downloading metadata from multiple attachments, you may want to use the NowAttachmentService - attachmentMetadataPaginator(filter: Filter? = null, limit: Int? = null) method, which returns a Paginator object that enables you to easily iterate over the potentially large amount of data that is returned.
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()
}
}- first()
- hasNext()
- hasPrevious
- isBusy
- last()
- next()
- observe()
- previous()
- reset()