NowAttachmentService class - iOS
The NowAttachmentService class provides functions that enable you to upload and query attachments that are associated with records within a table on a ServiceNow instance.
You can upload or retrieve a single file with each request. NowAttachmentService respects any system limitations on uploaded files, such as maximum file size and allowed attachment types. You can control these settings using the instance properties com.glide.attachment.max_size, 1024MB by default, and glide.attachment.extensions.
| Name | Type | Description |
|---|---|---|
| configuration | NowServiceConfiguration | Configuration settings provided when the service was initialized. |
NowAttachmentService - attachment(for sysId: String, validateAttachment: Boolean) async throws
Retrieves the attachment with the specified sys_id and validates the attachment by comparing the computed hash of the attachment to the expected SHA256 checksum.
false. You can validate the attachment at a
later stage if needed by calling the Attachment.validate() function.| Name | Type | Description |
|---|---|---|
| for sysId | String | Sys_id of the attachment to retrieve. |
| validateAttachment | Boolean | Flag that indicates whether to validate the attachment. Valid values:
Default: true |
| Type | Description |
|---|---|
| NowAttachment | Returned when the method is successful. Specified attachment. |
| NowDataError | Thrown when the method fails.
|
The following code examples shows how to call this method.
func downloadAttachment(sysId: String) async throws -> NowAttachment {
do {
let nowAttachment = try await attachmentService.attachment(for: sysId, validateAttachment: false)
return nowAttachment
} catch {
// Handle NowDataError
throw error
}
}
NowAttachmentService - attachment(for sysId: String, validateAttachment: Boolean, completion: @escaping (Result<NowAttachment, NowDataError>))
Retrieves the attachment with the specified sys_id and validates the attachment by comparing the computed hash of the attachment to the expected SHA256 checksum. Once finished, it executes the passed completion handler.
false. You can validate the attachment at a later stage
if needed by calling the Attachment.validate() function.| Name | Type | Description |
|---|---|---|
| for sysId | String | Sys_id of the attachment to retrieve. |
| validateAttachment | Boolean | Flag that indicates whether to validate the attachment. Valid values:
Default: true |
| completion | @escaping (Result<NowAttachment, NowDataError>) | Completion handler to execute after the attachment is retrieved. Return
values for the completion handler:
|
| Type | Description |
|---|---|
| None |
The following code example shows how to call this function.
let sysId = UUID().uuidString // UUID for the attachment
private func downloadAttachment(sysId: String) {
attachmentService.attachment(for: sysId, validateAttachment: false) { (result) in
switch result {
case .success(let attachment):
// Attachment retrieved successful, return attachment
case .failure(let error):
// Attachment retrieved failed, return NowDataError
}
}
}
NowAttachmentService - attachment(for sysId: String, validateAttachment: Boolean)
Retrieves the attachment with the specified sys_id and validates the attachment by comparing the computed hash of the attachment to the expected hash.
false. If needed, you can validate the attachment at a
later stage by calling the Attachment.validate() function.| Name | Type | Description |
|---|---|---|
| for sysId | String | Sys_id of the attachment to retrieve. |
| validateAttachment | Boolean | Flag that indicates whether to validate the attachment. Valid values:
Default: true |
| Type | Description |
|---|---|
| AnyPublisher<NowAttachment, NowDataError> | Success: NowAttachment - Contains the specified attachment. Failure: NowDataError
|
The following code example shows how to call this function.
let sysId = UUID().uuidString // UUID for the attachment
private func downloadAttachment(sysId: String) {
let publisher = self.attachmentService.attachment(for: sysId, validateAttachment: false)
publisher
.receive(on: DispatchQueue.main)
.sink { [weak self] completion in
if case let .failure(error) = completion {
// Attachment retrieved failed, return NowDataError
}
} receiveValue: { [weak self] (attachment) in
// Attachment retrieved successful, return attachment
}
.store(in: &subscriptions)
}
NowAttachmentService - attachmentMetadata(filter: Filter, limit: Int) async throws
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: nil - 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: nil - Returns all metadata that meets the filter parameter specifications. |
| Type | Description |
|---|---|
| [NowAttachmentMetadata] | Returned when the method is successful. Array that contains the metadata for the matching attachments. |
| NowDataError | Thrown when the method fails.
|
The following code examples shows how to call this method.
let filter = Filter(criteria: [], sortBy: [.desc("created")],
queryCategory: nil)
do {
let metadata = try await attachmentService.attachmentMetadata(filter: filter, limit: 10)
// Metadata retrieval successful
} catch {
// Metadata retrieval failed, NowDataError thrown
}
NowAttachmentService - attachmentMetadata(filter: Filter, limit: Int, completion: @escaping (Result<[NowAttachmentMetadata], 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: nil - 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: nil - Returns all metadata that meets the filter parameter specifications. |
| completion | @escaping (Result<[NowAttachmentMetadata], NowDataError>) | Completion handler to execute after the metadata is retrieved. Return values
for the completion handler:
|
| Type | Description |
|---|---|
| None |
The following code example shows how to call this function.
let query = "active=true^short_descriptionLIKEbroken"
let filter = Filter(query: query)
attachmentService.attachmentMetadata(filter: filter, limit: limit) { (result) in
DispatchQueue.main.async { [weak self] in
switch result {
case .success(let response):
do {
let data = try JSONEncoder().encode(response)
// Retrieve metadata successfully, return data
} catch {
// Retrieve metadata failed, return error
}
case .failure(let error):
// Retrieve metadata failed, return NowDataError
}
}
}
NowAttachmentService - attachmentMetadata(filter: Filter, limit: Int)
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: nil - 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: nil - Returns all metadata that meets the filter parameter specifications. |
| Type | Description |
|---|---|
| AnyPublisher<[NowAttachmentMetadata], NowDataError> | Success: NowAttachmentMetadata - Objects that contain the metadata for the
matching attachments. Failure: NowDataError
|
The following code example shows how to call this function.
let query = "active=true^short_descriptionLIKEbroken"
let filter = Filter(query: query)
let publisher = self.attachmentService.attachmentMetadata(filter: filter)
publisher
.receive(on: DispatchQueue.main)
.encode(encoder: JSONEncoder())
.sink { [weak self] completion in
if case let .failure(error) = completion {
// Retrieve metadata failed, return NowDataError
} receiveValue: { [weak self] (data) in
// Retrieve metadata successfully, return data
}
.store(in: &subscriptions
NowAttachmentService - attachmentMetadata(for sysId: String) async throws
Retrieves the metadata for the attachment associated with the specified sys_id.
| Name | Type | Description |
|---|---|---|
| for sysId | String | Sys_id of the attachment whose metadata to retrieve. |
| Type | Description |
|---|---|
| NowAttachmentMetadata | Returned when the method is successful. Metadata for the requested attachment. |
| NowDataError | Thrown when the method fails.
|
The following code examples shows how to call this method.
do {
let metadata = try await attachmentService.attachmentMetadata(for: sysId)
// Metadata retrieval successful
} catch {
// Metadata retrieval failed, NowDataError thrown
}
NowAttachmentService - attachmentMetadata(for sysId: String, completion: @escaping (Result<NowAttachmentMetadata, NowDataError>))
Retrieves the metadata for the attachment associated with the specified sys_id and then executes the completion handler after the metadata is retrieved.
| Name | Type | Description |
|---|---|---|
| for sysId | String | Sys_id of the attachment whose metadata to retrieve. |
| completion | @escaping (Result<NowAttachmentMetadata, NowDataError>) | Completion handler to execute after the metadata is retrieved. Return values
for the completion handler:
|
| Type | Description |
|---|---|
| None |
The following code example shows how to call this function.
let sysId = UUID().uuidString // UUID for the attachment
attachmentService.attachmentMetadata(for: sysId) { (result) in
DispatchQueue.main.async { [weak self] in
switch result {
case .success(let response):
do {
let data = try JSONEncoder().encode(response)
// Retrieve metadata successfully, return data
} catch {
// Retrieve metadata failed, return error
}
case .failure(let error):
// Retrieve metadata failed, return NowDataError
}
}
}
NowAttachmentService - attachmentMetadata(for sysId: String)
Retrieves the metadata for the attachment associated with the specified sys_id.
| Name | Type | Description |
|---|---|---|
| for sysId | String | Sys_id of the attachment whose metadata to retrieve. |
| Type | Description |
|---|---|
| AnyPublisher<[NowAttachmentMetadata], NowDataError> | Success: NowAttachmentMetadata - Object that contains the metadata for the
requested attachment. Failure: NowDataError
|
The following code example shows how to call this function.
let sysId = UUID().uuidString // UUID for the attachment
let publisher = self.attachmentService.attachmentMetadata(for: sysId)
publisher
.receive(on: DispatchQueue.main)
.encode(encoder: JSONEncoder())
.sink { [weak self] completion in
if case let .failure(error) = completion {
// Retrieve metadata failed, return NowDataError
}
} receiveValue: { [weak self] (data) in
// Retrieve metadata successfully, return data
}
.store(in: &subscriptions)
NowAttachmentService - attachmentMetadataPaginator(filter: Filter, limit: Int)
Retrieves the metadata for all the attachments that meet the specified criteria and returns a Paginator object for iterating through the pages of the returned metadata.
| Name | Type | Description |
|---|---|---|
| filter | Filter | Optional. Query string to use to filter the attachments whose metadata to
return. Default: nil - 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: nil - Returns all metadata that meets the filter parameter specifications. |
| Type | Description |
|---|---|
| Paginator<[NowAttachmentMetadata]> | Success: Array of NowAttachmentMetadata objects. Failure: NowDataError object.
|
The following code example shows how to call this function.
let paginator: Paginator<T> = makePaginator(query: query, fetchLimit: 1)
func makePaginator(query: String, fetchLimit: Int?) -> Paginator<[NowAttachmentMetadata]> {
attachmentsService.attachmentMetadataPaginator(filter: Filter(query: query), limit: fetchLimit)
}
NowAttachmentService - delete(sysId: String) async throws
Deletes the attachment with the specified sys_id.
| Name | Type | Description |
|---|---|---|
| sysId | String | Sys_id of the attachment to delete. |
| Type | Description |
|---|---|
| None | Nothing is returned if the method was successful. |
| NowDataError | Thrown when the method fails.
|
The following code examples shows how to call this method.
do {
try await attachmentService.delete(sysId: sysId)
// Deletion successful
} catch {
// Deletion failed, NowDataError thrown
}
NowAttachmentService - delete(sysId: String, completion: @escaping (Result<Void, NowDataError>))
Deletes the attachment with the specified sys_id and then executes the passed completion handler after the attachment is deleted.
| Name | Type | Description |
|---|---|---|
| sysId | String | Sys_id of the attachment to delete. |
| completion | @escaping (Result<Void, NowDataError>) | Completion handler to execute after the attachment is deleted. Return values
for the completion handler:
|
| Type | Description |
|---|---|
| None |
The following code example shows how to call this function.
let sysId = UUID().uuidString // UUID for the attachment
attachmentService.delete(sysId: sysId) { (result) in
DispatchQueue.main.async { [weak self] in
switch result {
case .success:
// Attachment with sysId is deleted
case .failure(let error):
// Failed to delete, with NowDataError
}
}
}
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 |
|---|---|
| AnyPublisher<Void, NowDataError> | Success: Nothing returned Failure: NowDataError
|
The following code example shows how to call this function.
let publisher = attachmentService.delete(sysId: sysId)
publisher
.receive(on: DispatchQueue.main)
.sink { [weak self] completion in
switch completion {
case .finished:
// Attachment with sysId is deleted
case .failure(let error):
// Failed to delete, with NowDataError
}
} receiveValue: {
}
.store(in: &subscriptions)
NowAttachmentService - init(configuration: NowServiceConfiguration, coreServiceProvider: NowCoreServiceProviding)
Initializes a new NowAttachmentService instance.
| Name | Type | Description |
|---|---|---|
| configuration | NowServiceConfiguration | Configuration information needed to properly initialize the service. |
| coreServiceProvider | NowCoreServiceProviding | Optional. Object implementing the NowCoreServiceProviding protocol. Default: nil |
The following code example shows how to call this function.
guard let coreService = NowSDK.core() else {
// Error with NowServiceError.sdkNotConfigured
return
}
guard
let instanceUrl = URL(string: "http://sample.service-now.com"),
let serviceConfig = NowSDK.makeServiceConfiguration(for: instanceUrl) else {
// Could not create service –
// NowServiceError.serviceConfigurationInvalid
return
}
let attachmentService = NowAttachmentService(configuration: serviceConfig, coreServiceProvider: coreService)
NowAttachmentService - upload(data: Data, configuration: NowAttachmentUploadConfiguration, progressUpdate: @escaping ProgressUpdate) async throws
Uploads the attachment data and attaches it to a record in a table as specified in the upload configuration.
The ProgressUpdate callback provides upload progress information that you can then use to provide UI feedback.
| Name | Type | Description |
|---|---|---|
| data | Data | Attachment to upload. |
| configuration | NowAttachmentUpload Configuration | Configuration information that defines the upload parameters for the attachment, such as:
|
| progressUpdate | @escaping ProgressUpdate | Completion handler to execute after the attachment is retrieved. This is called multiple times until the upload process is complete. Return values for the completion handler: ProgressUpdate - Returns the current progress of the upload.
|
| Type | Description |
|---|---|
| NowAttachmentMetadata | Returned when the method is successful. Object that contains the uploaded metadata. |
| NowDataError | Thrown when the method fails.
|
The following code examples shows how to call this method.
func uploadFile(for tableName: String, sysId: String, mimeType: String, encryptionContext: String?, data: Data, filename: String) async throws -> NowAttachmentMetadata {
let mimeType = MediaUtilities.mimeType(forFileName: filename)
let uploadConfig = NowAttachmentUploadConfiguration(tableName: tableName,
recordSysId: sysId,
fileName: filename,
contentType: mimeType,
encryptionContext: encryptionContext)
do {
let attachmentMetadata = try await attachmentService.upload(data: data, configuration: uploadConfig) { (_, _, _) in }
return attachmentMetadata
} catch {
// Handle NowDataError
throw error
}
}
NowAttachmentService - upload(data: Data, configuration: NowAttachmentUploadConfiguration, progressUpdate: @escaping ProgressUpdate, completion: @escaping (Result<NowAttachmentMetadata, NowDataError>))
Uploads the attachment data and attaches it to a record in a table as specified in the upload configuration.
The ProgressUpdate callback provides upload progress information that you can then use to provide UI feedback.
| Name | Type | Description |
|---|---|---|
| data | Data | Attachment to upload. |
| configuration | NowAttachmentUpload Configuration | Configuration information that defines the upload parameters for the attachment, such as:
|
| progressUpdate | @escaping ProgressUpdate | Completion handler to execute after the attachment is retrieved. This is called multiple times until the upload process is complete. Return values for the completion handler: ProgressUpdate - Returns the current progress of the upload.
|
| completion | @escaping (Result<NowAttachmentMetadata, NowDataError>) | Completion handler to execute after the metadata is uploaded. Return values
for the completion handler:
|
| Type | Description |
|---|---|
| None |
The following code example shows how to call this function.
typealias ProgressUpdate = (_ bytesWritten: Int64, _ totalBytesWritten: Int64, _ totalBytesExpectedToWrite: Int64) -> Void
let image = UIImage(named: "photo.png")
let data = image.pngData()
let recordSysId = "" // Sys_id of the record on the SN instance
let config = NowAttachmentUploadConfiguration(
tableName: "tableName",
recordSysId: recordSysId,
fileName: "photo.png",
contentType: "image/png",
encryptionContext: nil)
private func uploadFile(
data: Data,
config: NowAttachmentUploadConfiguration,
progress: @escaping ProgressUpdate) {
attachmentService.upload(data: data, configuration: config, progressUpdate: progress) { (result) in
DispatchQueue.main.async { [weak self] in
switch result {
case .success(let response)
do {
let data = try JSONEncoder().encode(response)
// Upload file successfully, return data
} catch
// Upload file decode failed, return error
}
case .failure(let error):
// Upload file failed, return NowDataError
}
}
}
NowAttachmentService - upload(data: Data, configuration: NowAttachmentUploadConfiguration, progressUpdate: @escaping ProgressUpdate)
Uploads the attachment data and attaches it to a record in a table as specified in the upload configuration.
The ProgressUpdate callback provides upload progress information that you can then use to provide UI feedback.
| Name | Type | Description |
|---|---|---|
| data | Data | Attachment to upload. The data size and type of attachment that you can upload are controlled by your ServiceNow instance. |
| configuration | NowAttachmentUploadConfiguration | Configuration information that defines the upload parameters for the attachment, such as:
|
| progressUpdate | @escaping ProgressUpdate | Completion handler to execute after the attachment is retrieved. Return values for the completion handler: ProgressUpdate - Returns the current progress of the upload. This is called multiple times until the upload process is complete.
|
| Type | Description |
|---|---|
| AnyPublisher<NowAttachmentMetadata, NowDataError> | Success: NowAttachmentMetadata - Object that contains the metadata for the
requested attachment. Failure: NowDataError
|
The following code example shows how to call this function.
typealias ProgressUpdate = (_ bytesWritten: Int64, _ totalBytesWritten: Int64, _ totalBytesExpectedToWrite: Int64) -> Void
let image = UIImage(named: "photo.png")
let data = image.pngData()
let recordSysId = "" // Sys_id of the record on the SN instance
let config = NowAttachmentUploadConfiguration(
tableName: "tableName",
recordSysId: recordSysId,
fileName: "photo.png",
contentType: "image/png",
encryptionContext: nil)
private func uploadFile(data: Data,
config: NowAttachmentUploadConfiguration,
progress: @escaping ProgressUpdate) {
let publisher = attachmentService.upload(data: data, configuration: config, progressUpdate: progress)
publisher
.receive(on: DispatchQueue.main)
.encode(encoder: JSONEncoder())
.sink { [weak self] completion in
if case let .failure(error) = completion {
// Upload file failed, return NowDataError
}
} receiveValue: { [weak self] (data) in
// Upload file successfully, return data
}
.store(in: &subscriptions)
}