NowAttachmentService class - iOS

  • Rversion finale: Australia
  • Mis à jour 12 mars 2026
  • 29 minutes de lecture
  • 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.

    Remarque :
    NowAttachmentService accepts all Content-Type values (/). Specify the file content type when uploading an attachment. The content type is stored with the file metadata, enabling other tools to correctly identify and parse the file.
    Tableau 1. Properties
    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.

    Remarque :
    Validation of an attachment can cause a performance impact. You can skip the validation step by setting the validateAttachment parameter to false. You can validate the attachment at a later stage if needed by calling the Attachment.validate() function.
    Tableau 2. Parameters
    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:
    • true: Validate the attachment.
    • false: Do not validate the attachment.

    Default: true

    Tableau 3. Returns
    Type Description
    NowAttachment Returned when the method is successful. Specified attachment.
    NowDataError Thrown when the method fails.
    • accessToken(AccessTokenProviderError)
      • AccessTokenProviderError: The access token provider's error code or message.
        • accessTokenRetrievalFailed
        • userSessionError(_ error: Error)

      Thrown when there is an error in the access token.

    • attachmentValidation

      Thrown when an attachment fails validation.

    • badResponse(statusCode: HTTPStatusCode)
      • HTTPStatusCode: Status code received from the instance.

      Thrown when a request returns an unexpected response

    • cannotDecodeModel(DecodingError)
      • DecodingError: Decoding error detected.

      Thrown when a Codable model cannot be decoded from JSON.

    • cannotDecodeProperty(type: Any, from: String)
      • type: Wrapped type to decode from a string.
      • from: String to decode to the specified type.

      Thrown when a string-wrapped value cannot be decoded from JSON.

    • cannotEncodeModel(EncodingError)
      • EncodingError: Encoding error detected.

      Thrown when a Codable model cannot be encoded to JSON.

    • cannotParseResponse

      Thrown when a response from the instance cannot be parsed into its expected format.

    • invalidURL

      Thrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.

    • missingAttachmentMetadata

      Thrown when the attachment metadata header is missing.

    • missingServiceConfiguration

      Thrown when an expected service configuration is missing.

    • missingSysID

      Thrown when an expected sys_id parameter is missing.

    • network(NetworkServiceError)
      • genericError(String)
      • operationCanceled
      • service​Disabled
      • serverError(Error)
      • systemError(Error)

      Thrown when a network service encountered an error.

    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.

    Remarque :
    Validation of an attachment can cause a performance impact. You can skip the validation step by setting the validateAttachment parameter to false. You can validate the attachment at a later stage if needed by calling the Attachment.validate() function.
    Tableau 4. Parameters
    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:
    • true: Validate the attachment.
    • false: Do not validate the attachment.

    Default: true

    completion @escaping (Result​<NowAttachment, NowDataError>) Completion handler to execute after the attachment is retrieved.
    Return values for the completion handler:
    • Success: NowAttachment - Requested attachment
    • Failure: NowDataError
      • accessToken(AccessTokenProviderError)
        • AccessTokenProviderError: The access token provider's error code or message.
          • accessTokenRetrievalFailed
          • userSessionError(_ error: Error)

        Thrown when there is an error in the access token.

      • attachmentValidation

        Thrown when an attachment fails validation.

      • badResponse(statusCode: HTTPStatusCode)
        • HTTPStatusCode: Status code received from the instance.

        Thrown when a request returns an unexpected response

      • cannotDecodeModel(DecodingError)
        • DecodingError: Decoding error detected.

        Thrown when a Codable model cannot be decoded from JSON.

      • cannotDecodeProperty(type: Any, from: String)
        • type: Wrapped type to decode from a string.
        • from: String to decode to the specified type.

        Thrown when a string-wrapped value cannot be decoded from JSON.

      • cannotEncodeModel(EncodingError)
        • EncodingError: Encoding error detected.

        Thrown when a Codable model cannot be encoded to JSON.

      • cannotParseResponse

        Thrown when a response from the instance cannot be parsed into its expected format.

      • invalidURL

        Thrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.

      • missingAttachmentMetadata

        Thrown when the attachment metadata header is missing.

      • missingServiceConfiguration

        Thrown when an expected service configuration is missing.

      • missingSysID

        Thrown when an expected sys_id parameter is missing.

      • network(NetworkServiceError)
        • genericError(String)
        • operationCanceled
        • service​Disabled
        • serverError(Error)
        • systemError(Error)

        Thrown when a network service encountered an error.

    Tableau 5. Returns
    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.

    Remarque :
    Validation of an attachment can cause a performance impact. You can skip the validation step by setting the validateAttachment parameter to false. If needed, you can validate the attachment at a later stage by calling the Attachment.validate() function.
    Remarque :
    This method has been deprecated. You should use the async/await implementation of the method instead.
    Tableau 6. Parameters
    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:
    • true: Validate the attachment.
    • false: Don’t validate the attachment.

    Default: true

    Tableau 7. Returns
    Type Description
    AnyPublisher​<NowAttachment, NowDataError> Success: NowAttachment - Contains the specified attachment.

    Failure: NowDataError

    • accessToken(AccessTokenProviderError)
      • AccessTokenProviderError: The access token provider's error code or message.
        • accessTokenRetrievalFailed
        • userSessionError(_ error: Error)

      Thrown when there is an error in the access token.

    • attachmentValidation

      Thrown when an attachment fails validation.

    • badResponse(statusCode: HTTPStatusCode)
      • HTTPStatusCode: Status code received from the instance.

      Thrown when a request returns an unexpected response

    • cannotDecodeModel(DecodingError)
      • DecodingError: Decoding error detected.

      Thrown when a Codable model cannot be decoded from JSON.

    • cannotDecodeProperty(type: Any, from: String)
      • type: Wrapped type to decode from a string.
      • from: String to decode to the specified type.

      Thrown when a string-wrapped value cannot be decoded from JSON.

    • cannotEncodeModel(EncodingError)
      • EncodingError: Encoding error detected.

      Thrown when a Codable model cannot be encoded to JSON.

    • cannotParseResponse

      Thrown when a response from the instance cannot be parsed into its expected format.

    • invalidURL

      Thrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.

    • missingAttachmentMetadata

      Thrown when the attachment metadata header is missing.

    • missingServiceConfiguration

      Thrown when an expected service configuration is missing.

    • missingSysID

      Thrown when an expected sys_id parameter is missing.

    • network(NetworkServiceError)
      • genericError(String)
      • operationCanceled
      • service​Disabled
      • serverError(Error)
      • systemError(Error)

      Thrown when a network service encountered an error.

    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.

    Tableau 8. Parameters
    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.

    Tableau 9. Returns
    Type Description
    [NowAttachmentMetadata] Returned when the method is successful. Array that contains the metadata for the matching attachments.
    NowDataError Thrown when the method fails.
    • accessToken(AccessTokenProviderError)
      • AccessTokenProviderError: The access token provider's error code or message.
        • accessTokenRetrievalFailed
        • userSessionError(_ error: Error)

      Thrown when there is an error in the access token.

    • attachmentValidation

      Thrown when an attachment fails validation.

    • badResponse(statusCode: HTTPStatusCode)
      • HTTPStatusCode: Status code received from the instance.

      Thrown when a request returns an unexpected response

    • cannotDecodeModel(DecodingError)
      • DecodingError: Decoding error detected.

      Thrown when a Codable model cannot be decoded from JSON.

    • cannotDecodeProperty(type: Any, from: String)
      • type: Wrapped type to decode from a string.
      • from: String to decode to the specified type.

      Thrown when a string-wrapped value cannot be decoded from JSON.

    • cannotEncodeModel(EncodingError)
      • EncodingError: Encoding error detected.

      Thrown when a Codable model cannot be encoded to JSON.

    • cannotParseResponse

      Thrown when a response from the instance cannot be parsed into its expected format.

    • invalidURL

      Thrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.

    • missingAttachmentMetadata

      Thrown when the attachment metadata header is missing.

    • missingServiceConfiguration

      Thrown when an expected service configuration is missing.

    • missingSysID

      Thrown when an expected sys_id parameter is missing.

    • network(NetworkServiceError)
      • genericError(String)
      • operationCanceled
      • service​Disabled
      • serverError(Error)
      • systemError(Error)

      Thrown when a network service encountered an error.

    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.

    Tableau 10. Parameters
    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:
    • Success: NowAttachmentMetadata - Objects containing the requested metadata.
    • Error: NowDataError
      • accessToken(AccessTokenProviderError)
        • AccessTokenProviderError: The access token provider's error code or message.
          • accessTokenRetrievalFailed
          • userSessionError(_ error: Error)

        Thrown when there is an error in the access token.

      • attachmentValidation

        Thrown when an attachment fails validation.

      • badResponse(statusCode: HTTPStatusCode)
        • HTTPStatusCode: Status code received from the instance.

        Thrown when a request returns an unexpected response

      • cannotDecodeModel(DecodingError)
        • DecodingError: Decoding error detected.

        Thrown when a Codable model cannot be decoded from JSON.

      • cannotDecodeProperty(type: Any, from: String)
        • type: Wrapped type to decode from a string.
        • from: String to decode to the specified type.

        Thrown when a string-wrapped value cannot be decoded from JSON.

      • cannotEncodeModel(EncodingError)
        • EncodingError: Encoding error detected.

        Thrown when a Codable model cannot be encoded to JSON.

      • cannotParseResponse

        Thrown when a response from the instance cannot be parsed into its expected format.

      • invalidURL

        Thrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.

      • missingAttachmentMetadata

        Thrown when the attachment metadata header is missing.

      • missingServiceConfiguration

        Thrown when an expected service configuration is missing.

      • missingSysID

        Thrown when an expected sys_id parameter is missing.

      • network(NetworkServiceError)
        • genericError(String)
        • operationCanceled
        • service​Disabled
        • serverError(Error)
        • systemError(Error)

        Thrown when a network service encountered an error.

    Tableau 11. Returns
    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.

    Remarque :
    This method has been deprecated. You should use the async/await implementation of the method instead.
    Tableau 12. Parameters
    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.

    Tableau 13. Returns
    Type Description
    AnyPublisher​<[NowAttachmentMetadata], NowDataError> Success: NowAttachmentMetadata - Objects that contain the metadata for the matching attachments.

    Failure: NowDataError

    • accessToken(AccessTokenProviderError)
      • AccessTokenProviderError: The access token provider's error code or message.
        • accessTokenRetrievalFailed
        • userSessionError(_ error: Error)

      Thrown when there is an error in the access token.

    • attachmentValidation

      Thrown when an attachment fails validation.

    • badResponse(statusCode: HTTPStatusCode)
      • HTTPStatusCode: Status code received from the instance.

      Thrown when a request returns an unexpected response

    • cannotDecodeModel(DecodingError)
      • DecodingError: Decoding error detected.

      Thrown when a Codable model cannot be decoded from JSON.

    • cannotDecodeProperty(type: Any, from: String)
      • type: Wrapped type to decode from a string.
      • from: String to decode to the specified type.

      Thrown when a string-wrapped value cannot be decoded from JSON.

    • cannotEncodeModel(EncodingError)
      • EncodingError: Encoding error detected.

      Thrown when a Codable model cannot be encoded to JSON.

    • cannotParseResponse

      Thrown when a response from the instance cannot be parsed into its expected format.

    • invalidURL

      Thrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.

    • missingAttachmentMetadata

      Thrown when the attachment metadata header is missing.

    • missingServiceConfiguration

      Thrown when an expected service configuration is missing.

    • missingSysID

      Thrown when an expected sys_id parameter is missing.

    • network(NetworkServiceError)
      • genericError(String)
      • operationCanceled
      • service​Disabled
      • serverError(Error)
      • systemError(Error)

      Thrown when a network service encountered an error.

    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.

    Tableau 14. Parameters
    Name Type Description
    for sysId String Sys_id of the attachment whose metadata to retrieve.
    Tableau 15. Returns
    Type Description
    NowAttachmentMetadata Returned when the method is successful. Metadata for the requested attachment.
    NowDataError Thrown when the method fails.
    • accessToken(AccessTokenProviderError)
      • AccessTokenProviderError: The access token provider's error code or message.
        • accessTokenRetrievalFailed
        • userSessionError(_ error: Error)

      Thrown when there is an error in the access token.

    • attachmentValidation

      Thrown when an attachment fails validation.

    • badResponse(statusCode: HTTPStatusCode)
      • HTTPStatusCode: Status code received from the instance.

      Thrown when a request returns an unexpected response

    • cannotDecodeModel(DecodingError)
      • DecodingError: Decoding error detected.

      Thrown when a Codable model cannot be decoded from JSON.

    • cannotDecodeProperty(type: Any, from: String)
      • type: Wrapped type to decode from a string.
      • from: String to decode to the specified type.

      Thrown when a string-wrapped value cannot be decoded from JSON.

    • cannotEncodeModel(EncodingError)
      • EncodingError: Encoding error detected.

      Thrown when a Codable model cannot be encoded to JSON.

    • cannotParseResponse

      Thrown when a response from the instance cannot be parsed into its expected format.

    • invalidURL

      Thrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.

    • missingAttachmentMetadata

      Thrown when the attachment metadata header is missing.

    • missingServiceConfiguration

      Thrown when an expected service configuration is missing.

    • missingSysID

      Thrown when an expected sys_id parameter is missing.

    • network(NetworkServiceError)
      • genericError(String)
      • operationCanceled
      • service​Disabled
      • serverError(Error)
      • systemError(Error)

      Thrown when a network service encountered an error.

    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.

    Tableau 16. Parameters
    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:
    • Success: NowAttachmentMetadata - Requested metadata
    • Failure: NowDataError
      • accessToken(AccessTokenProviderError)
        • AccessTokenProviderError: The access token provider's error code or message.
          • accessTokenRetrievalFailed
          • userSessionError(_ error: Error)

        Thrown when there is an error in the access token.

      • attachmentValidation

        Thrown when an attachment fails validation.

      • badResponse(statusCode: HTTPStatusCode)
        • HTTPStatusCode: Status code received from the instance.

        Thrown when a request returns an unexpected response

      • cannotDecodeModel(DecodingError)
        • DecodingError: Decoding error detected.

        Thrown when a Codable model cannot be decoded from JSON.

      • cannotDecodeProperty(type: Any, from: String)
        • type: Wrapped type to decode from a string.
        • from: String to decode to the specified type.

        Thrown when a string-wrapped value cannot be decoded from JSON.

      • cannotEncodeModel(EncodingError)
        • EncodingError: Encoding error detected.

        Thrown when a Codable model cannot be encoded to JSON.

      • cannotParseResponse

        Thrown when a response from the instance cannot be parsed into its expected format.

      • invalidURL

        Thrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.

      • missingAttachmentMetadata

        Thrown when the attachment metadata header is missing.

      • missingServiceConfiguration

        Thrown when an expected service configuration is missing.

      • missingSysID

        Thrown when an expected sys_id parameter is missing.

      • network(NetworkServiceError)
        • genericError(String)
        • operationCanceled
        • service​Disabled
        • serverError(Error)
        • systemError(Error)

        Thrown when a network service encountered an error.

    Tableau 17. Returns
    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.

    Remarque :
    This method has been deprecated. You should use the async/await implementation of the method instead.
    Tableau 18. Parameters
    Name Type Description
    for sysId String Sys_id of the attachment whose metadata to retrieve.
    Tableau 19. Returns
    Type Description
    AnyPublisher​<[NowAttachmentMetadata], NowDataError> Success: NowAttachmentMetadata - Object that contains the metadata for the requested attachment.

    Failure: NowDataError

    • accessToken(AccessTokenProviderError)
      • AccessTokenProviderError: The access token provider's error code or message.
        • accessTokenRetrievalFailed
        • userSessionError(_ error: Error)

      Thrown when there is an error in the access token.

    • attachmentValidation

      Thrown when an attachment fails validation.

    • badResponse(statusCode: HTTPStatusCode)
      • HTTPStatusCode: Status code received from the instance.

      Thrown when a request returns an unexpected response

    • cannotDecodeModel(DecodingError)
      • DecodingError: Decoding error detected.

      Thrown when a Codable model cannot be decoded from JSON.

    • cannotDecodeProperty(type: Any, from: String)
      • type: Wrapped type to decode from a string.
      • from: String to decode to the specified type.

      Thrown when a string-wrapped value cannot be decoded from JSON.

    • cannotEncodeModel(EncodingError)
      • EncodingError: Encoding error detected.

      Thrown when a Codable model cannot be encoded to JSON.

    • cannotParseResponse

      Thrown when a response from the instance cannot be parsed into its expected format.

    • invalidURL

      Thrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.

    • missingAttachmentMetadata

      Thrown when the attachment metadata header is missing.

    • missingServiceConfiguration

      Thrown when an expected service configuration is missing.

    • missingSysID

      Thrown when an expected sys_id parameter is missing.

    • network(NetworkServiceError)
      • genericError(String)
      • operationCanceled
      • service​Disabled
      • serverError(Error)
      • systemError(Error)

      Thrown when a network service encountered an error.

    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.

    Tableau 20. Parameters
    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.

    Tableau 21. Returns
    Type Description
    Paginator​<[NowAttachmentMetadata]> Success: Array of NowAttachmentMetadata objects.

    Failure: NowDataError object.

    • accessToken(AccessTokenProviderError)
      • AccessTokenProviderError: The access token provider's error code or message.
        • accessTokenRetrievalFailed
        • userSessionError(_ error: Error)

      Thrown when there is an error in the access token.

    • attachmentValidation

      Thrown when an attachment fails validation.

    • badResponse(statusCode: HTTPStatusCode)
      • HTTPStatusCode: Status code received from the instance.

      Thrown when a request returns an unexpected response

    • cannotDecodeModel(DecodingError)
      • DecodingError: Decoding error detected.

      Thrown when a Codable model cannot be decoded from JSON.

    • cannotDecodeProperty(type: Any, from: String)
      • type: Wrapped type to decode from a string.
      • from: String to decode to the specified type.

      Thrown when a string-wrapped value cannot be decoded from JSON.

    • cannotEncodeModel(EncodingError)
      • EncodingError: Encoding error detected.

      Thrown when a Codable model cannot be encoded to JSON.

    • cannotParseResponse

      Thrown when a response from the instance cannot be parsed into its expected format.

    • invalidURL

      Thrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.

    • missingAttachmentMetadata

      Thrown when the attachment metadata header is missing.

    • missingServiceConfiguration

      Thrown when an expected service configuration is missing.

    • missingSysID

      Thrown when an expected sys_id parameter is missing.

    • network(NetworkServiceError)
      • genericError(String)
      • operationCanceled
      • service​Disabled
      • serverError(Error)
      • systemError(Error)

      Thrown when a network service encountered an error.

    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.

    Tableau 22. Parameters
    Name Type Description
    sysId String Sys_id of the attachment to delete.
    Tableau 23. Returns
    Type Description
    None Nothing is returned if the method was successful.
    NowDataError Thrown when the method fails.
    • accessToken(AccessTokenProviderError)
      • AccessTokenProviderError: The access token provider's error code or message.
        • accessTokenRetrievalFailed
        • userSessionError(_ error: Error)

      Thrown when there is an error in the access token.

    • attachmentValidation

      Thrown when an attachment fails validation.

    • badResponse(statusCode: HTTPStatusCode)
      • HTTPStatusCode: Status code received from the instance.

      Thrown when a request returns an unexpected response

    • cannotDecodeModel(DecodingError)
      • DecodingError: Decoding error detected.

      Thrown when a Codable model cannot be decoded from JSON.

    • cannotDecodeProperty(type: Any, from: String)
      • type: Wrapped type to decode from a string.
      • from: String to decode to the specified type.

      Thrown when a string-wrapped value cannot be decoded from JSON.

    • cannotEncodeModel(EncodingError)
      • EncodingError: Encoding error detected.

      Thrown when a Codable model cannot be encoded to JSON.

    • cannotParseResponse

      Thrown when a response from the instance cannot be parsed into its expected format.

    • invalidURL

      Thrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.

    • missingAttachmentMetadata

      Thrown when the attachment metadata header is missing.

    • missingServiceConfiguration

      Thrown when an expected service configuration is missing.

    • missingSysID

      Thrown when an expected sys_id parameter is missing.

    • network(NetworkServiceError)
      • genericError(String)
      • operationCanceled
      • service​Disabled
      • serverError(Error)
      • systemError(Error)

      Thrown when a network service encountered an error.

    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.

    Tableau 24. Parameters
    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:
    • Success: Void
    • Error: NowDataError
      • accessToken(AccessTokenProviderError)
        • AccessTokenProviderError: The access token provider's error code or message.
          • accessTokenRetrievalFailed
          • userSessionError(_ error: Error)

        Thrown when there is an error in the access token.

      • attachmentValidation

        Thrown when an attachment fails validation.

      • badResponse(statusCode: HTTPStatusCode)
        • HTTPStatusCode: Status code received from the instance.

        Thrown when a request returns an unexpected response

      • cannotDecodeModel(DecodingError)
        • DecodingError: Decoding error detected.

        Thrown when a Codable model cannot be decoded from JSON.

      • cannotDecodeProperty(type: Any, from: String)
        • type: Wrapped type to decode from a string.
        • from: String to decode to the specified type.

        Thrown when a string-wrapped value cannot be decoded from JSON.

      • cannotEncodeModel(EncodingError)
        • EncodingError: Encoding error detected.

        Thrown when a Codable model cannot be encoded to JSON.

      • cannotParseResponse

        Thrown when a response from the instance cannot be parsed into its expected format.

      • invalidURL

        Thrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.

      • missingAttachmentMetadata

        Thrown when the attachment metadata header is missing.

      • missingServiceConfiguration

        Thrown when an expected service configuration is missing.

      • missingSysID

        Thrown when an expected sys_id parameter is missing.

      • network(NetworkServiceError)
        • genericError(String)
        • operationCanceled
        • service​Disabled
        • serverError(Error)
        • systemError(Error)

        Thrown when a network service encountered an error.

    Tableau 25. Returns
    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.

    Remarque :
    This method has been deprecated. You should use the async/await implementation of the method instead.
    Tableau 26. Parameters
    Name Type Description
    sysId String Sys_id of the attachment to delete.
    Tableau 27. Returns
    Type Description
    AnyPublisher<Void, NowDataError> Success: Nothing returned

    Failure: NowDataError

    • accessToken(AccessTokenProviderError)
      • AccessTokenProviderError: The access token provider's error code or message.
        • accessTokenRetrievalFailed
        • userSessionError(_ error: Error)

      Thrown when there is an error in the access token.

    • attachmentValidation

      Thrown when an attachment fails validation.

    • badResponse(statusCode: HTTPStatusCode)
      • HTTPStatusCode: Status code received from the instance.

      Thrown when a request returns an unexpected response

    • cannotDecodeModel(DecodingError)
      • DecodingError: Decoding error detected.

      Thrown when a Codable model cannot be decoded from JSON.

    • cannotDecodeProperty(type: Any, from: String)
      • type: Wrapped type to decode from a string.
      • from: String to decode to the specified type.

      Thrown when a string-wrapped value cannot be decoded from JSON.

    • cannotEncodeModel(EncodingError)
      • EncodingError: Encoding error detected.

      Thrown when a Codable model cannot be encoded to JSON.

    • cannotParseResponse

      Thrown when a response from the instance cannot be parsed into its expected format.

    • invalidURL

      Thrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.

    • missingAttachmentMetadata

      Thrown when the attachment metadata header is missing.

    • missingServiceConfiguration

      Thrown when an expected service configuration is missing.

    • missingSysID

      Thrown when an expected sys_id parameter is missing.

    • network(NetworkServiceError)
      • genericError(String)
      • operationCanceled
      • service​Disabled
      • serverError(Error)
      • systemError(Error)

      Thrown when a network service encountered an error.

    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.

    Tableau 28. Parameters
    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.

    Tableau 29. Parameters
    Name Type Description
    data Data Attachment to upload.
    configuration NowAttachmentUpload Configuration Configuration information that defines the upload parameters for the attachment, such as:
    • Table and record to attach the file to
    • Attachment file name
    • Type of attachment
    • Whether the file is encrypted
    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.

    ProgressUpdate = (_bytesWritten: Int64, _totalBytesWritten: Int64, _totalBytesExpectedToWrite: Int64) -> Void

    Tableau 30. Returns
    Type Description
    NowAttachmentMetadata Returned when the method is successful. Object that contains the uploaded metadata.
    NowDataError Thrown when the method fails.
    • accessToken(AccessTokenProviderError)
      • AccessTokenProviderError: The access token provider's error code or message.
        • accessTokenRetrievalFailed
        • userSessionError(_ error: Error)

      Thrown when there is an error in the access token.

    • attachmentValidation

      Thrown when an attachment fails validation.

    • badResponse(statusCode: HTTPStatusCode)
      • HTTPStatusCode: Status code received from the instance.

      Thrown when a request returns an unexpected response

    • cannotDecodeModel(DecodingError)
      • DecodingError: Decoding error detected.

      Thrown when a Codable model cannot be decoded from JSON.

    • cannotDecodeProperty(type: Any, from: String)
      • type: Wrapped type to decode from a string.
      • from: String to decode to the specified type.

      Thrown when a string-wrapped value cannot be decoded from JSON.

    • cannotEncodeModel(EncodingError)
      • EncodingError: Encoding error detected.

      Thrown when a Codable model cannot be encoded to JSON.

    • cannotParseResponse

      Thrown when a response from the instance cannot be parsed into its expected format.

    • invalidURL

      Thrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.

    • missingAttachmentMetadata

      Thrown when the attachment metadata header is missing.

    • missingServiceConfiguration

      Thrown when an expected service configuration is missing.

    • missingSysID

      Thrown when an expected sys_id parameter is missing.

    • network(NetworkServiceError)
      • genericError(String)
      • operationCanceled
      • service​Disabled
      • serverError(Error)
      • systemError(Error)

      Thrown when a network service encountered an error.

    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.

    Tableau 31. Parameters
    Name Type Description
    data Data Attachment to upload.
    configuration NowAttachmentUpload Configuration Configuration information that defines the upload parameters for the attachment, such as:
    • Table and record to attach the file to
    • Attachment file name
    • Type of attachment
    • Whether the file is encrypted
    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.

    ProgressUpdate = (_bytesWritten: Int64, _totalBytesWritten: Int64, _totalBytesExpectedToWrite: Int64) -> Void

    completion @escaping (Result<NowAttachmentMetadata, NowDataError>) Completion handler to execute after the metadata is uploaded.
    Return values for the completion handler:
    • Success: NowAttachmentMetadata - Object containing the metadata for the uploaded attachment.
    • Error: NowDataError
      • accessToken(AccessTokenProviderError)
        • AccessTokenProviderError: The access token provider's error code or message.
          • accessTokenRetrievalFailed
          • userSessionError(_ error: Error)

        Thrown when there is an error in the access token.

      • attachmentValidation

        Thrown when an attachment fails validation.

      • badResponse(statusCode: HTTPStatusCode)
        • HTTPStatusCode: Status code received from the instance.

        Thrown when a request returns an unexpected response

      • cannotDecodeModel(DecodingError)
        • DecodingError: Decoding error detected.

        Thrown when a Codable model cannot be decoded from JSON.

      • cannotDecodeProperty(type: Any, from: String)
        • type: Wrapped type to decode from a string.
        • from: String to decode to the specified type.

        Thrown when a string-wrapped value cannot be decoded from JSON.

      • cannotEncodeModel(EncodingError)
        • EncodingError: Encoding error detected.

        Thrown when a Codable model cannot be encoded to JSON.

      • cannotParseResponse

        Thrown when a response from the instance cannot be parsed into its expected format.

      • invalidURL

        Thrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.

      • missingAttachmentMetadata

        Thrown when the attachment metadata header is missing.

      • missingServiceConfiguration

        Thrown when an expected service configuration is missing.

      • missingSysID

        Thrown when an expected sys_id parameter is missing.

      • network(NetworkServiceError)
        • genericError(String)
        • operationCanceled
        • service​Disabled
        • serverError(Error)
        • systemError(Error)

        Thrown when a network service encountered an error.

    Tableau 32. Returns
    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.

    Remarque :
    This method has been deprecated. You should use the async/await implementation of the method instead.
    Tableau 33. Parameters
    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:
    • Table and record to attach the file to
    • Attachment file name
    • Type of attachment
    • Whether the file is encrypted
    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.

    ProgressUpdate = (_bytesWritten: Int64, _totalBytesWritten: Int64, _totalBytesExpectedToWrite: Int64) -> Void

    Tableau 34. Returns
    Type Description
    AnyPublisher​<NowAttachmentMetadata, NowDataError> Success: NowAttachmentMetadata - Object that contains the metadata for the requested attachment.

    Failure: NowDataError

    • accessToken(AccessTokenProviderError)
      • AccessTokenProviderError: The access token provider's error code or message.
        • accessTokenRetrievalFailed
        • userSessionError(_ error: Error)

      Thrown when there is an error in the access token.

    • attachmentValidation

      Thrown when an attachment fails validation.

    • badResponse(statusCode: HTTPStatusCode)
      • HTTPStatusCode: Status code received from the instance.

      Thrown when a request returns an unexpected response

    • cannotDecodeModel(DecodingError)
      • DecodingError: Decoding error detected.

      Thrown when a Codable model cannot be decoded from JSON.

    • cannotDecodeProperty(type: Any, from: String)
      • type: Wrapped type to decode from a string.
      • from: String to decode to the specified type.

      Thrown when a string-wrapped value cannot be decoded from JSON.

    • cannotEncodeModel(EncodingError)
      • EncodingError: Encoding error detected.

      Thrown when a Codable model cannot be encoded to JSON.

    • cannotParseResponse

      Thrown when a response from the instance cannot be parsed into its expected format.

    • invalidURL

      Thrown when a URL cannot be formed. For example, if the string contains characters that are illegal in a URL or is an empty string.

    • missingAttachmentMetadata

      Thrown when the attachment metadata header is missing.

    • missingServiceConfiguration

      Thrown when an expected service configuration is missing.

    • missingSysID

      Thrown when an expected sys_id parameter is missing.

    • network(NetworkServiceError)
      • genericError(String)
      • operationCanceled
      • service​Disabled
      • serverError(Error)
      • systemError(Error)

      Thrown when a network service encountered an error.

    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)
      }