Retrieve attachments and attachment metadata
Summarize
Summary of Retrieve attachments and attachment metadata
The NowAttachmentService API in the Zurich release allows ServiceNow customers to manage attachments by performing CRUD operations and retrieving attachment metadata. This API supports uploading, downloading, deleting, and validating attachments, as well as fetching metadata associated with attachments stored in your ServiceNow instance. It is also accessible to guest users in the base ServiceNow system.
Show less
Key Features
- Upload Attachments: Attach files to specific records within your ServiceNow instance.
- Download Attachments: Retrieve one or multiple attachments as needed.
- Delete Attachments: Remove attachments from records.
- Validate Attachments: Compare the computed hash of an attachment against an expected value for integrity verification.
- Retrieve Attachment Metadata: Access metadata generated upon attachment upload, including support for filtering and pagination.
Using NowAttachmentService
You instantiate NowAttachmentService once per application lifecycle, typically inside a manager or application class, after initializing the NowSDK. The service can then be injected into other classes for reuse.
Fetching Attachment Metadata
- Single Attachment Metadata: Use
attachmentMetadata(sysId)to fetch metadata for a specific attachment identified by its sysid. - Multiple Attachments Metadata: Apply filters (e.g., content type) and limits to retrieve metadata lists via
attachmentMetadata(Filter, limit).
Metadata Pagination
For handling large metadata datasets, use the attachmentMetadataPaginator(filter, limit) method. This returns a Paginator object that facilitates iterating through metadata pages asynchronously via callbacks. Key paginator methods include:
first(),last(),next(),previous()– navigate pageshasNext(),hasPrevious()– check pagination statusobserve()– set callbacks for success and failure handlingreset()– restart pagination
Note that some paginator methods may throw exceptions if navigation is not possible (e.g., no more pages).
Practical Implications for ServiceNow Customers
This API enables streamlined management and retrieval of attachments and their metadata, essential for maintaining data integrity and efficient record handling. Its support for pagination and filtering ensures scalability when working with large volumes of attachments, making it suitable for enterprise environments. By integrating NowAttachmentService within your applications, you can provide robust attachment management capabilities aligned with ServiceNow best practices.
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()