Message

The AddAttachment Message will be used to process inbound and outbound attachments.

In Unifi Integration Designer, click on the 'Messages' icon & then New to begin configuring the AddAttachment Message.

New Message Modal

The fields to be configured for the AddAttachment New Message modal are as follows:

Your AddAttachment New Message modal should look like this:

Submit and view to further configure the Message.

Response Fields

Navigate to Message > Response.

The Response fields to be configured are as follows:

Your Response form should look like this:

Navigate to Message > Bond.

Bond Fields

The Bond fields to be configured are as follows:

*These fields are automatically populated.

Your Bond form should look like this:

Navigate to Outbound > Trigger.

Outbound Trigger Fields

The Outbound Trigger fields to be configured (as required)* are as follows:

*Outbound condition (as required):

It is not always necessary for you to enter a condition. The value given is an example. You may create any condition (or not) to align with your business process requirements. In this case, it makes sense to set the Outbound Condition to false because we do not want Unifi to send this message on any update of the source record. Typically, we would configure different messages to align with state changes (e.g. Close, Resolve).

The code in the 'Outbound condition' script field should look like this:

false

Your Outbound Trigger form should look like this:

Navigate to Outbound > Attachments.

Outbound Attachments Fields

The Outbound Attachments fields to be configured are as follows:

*This field is automatically populated.

Your Outbound Attachments form should look like this:

Navigate to Outbound > Settings.

Outbound Settings Fields

The Outbound Setting fields to be configured are as follows:

The code in the 'Path' field should look like this:

/attachment?file_name=${scratchpad.file_name}&reference=${scratchpad.reference}

The /attachment element of the Path is required when using the Unifi Scripted REST API to automatically generate the Scripted REST Resource. We shall discuss this further on the Scripted REST Resource page.

Please note the following when using the Unifi Scripted REST API to automatically generate the Scripted REST Resource:

If you have attachment messages which were configured before the auto-generated Scripted REST Resources, you must add the /attachment element to the Path of those messages. Otherwise, they may generate "Content type not allowed" errors.

Your Outbound Settings form should look like this:

Navigate to Inbound > Settings.

Inbound Settings Fields

The Inbound Settings fields to be configured are as follows:

*Bond reference method value choices: Internal - lookup using the internal reference only. External - lookup using the external reference only. Both - lookup using both the internal and external references.

Choose the value depending on what is needed.

For attachments we only use the Internal lookup because we are not passing both references. Lookup could be done on External or Both, but the references would need to be added to the inbound request.

Make sure to set either stage.internal_reference or stage.external_reference (or both) to match in the Payload to Stage Script (see below).

Your Inbound Settings form should look like this:

Navigate to Advanced > Script Editor.

Script Editor Fields

When you first open Script Editor, it should look like this:

The Script Editor fields to be configured are as follows:

Stage to Request (Outbound)

The code in the ‘Stage to Request’ script field should look like this:

(function processStageToRequest(request, stage, transaction, message, bond, scratchpad) {

  // This information would normally come from the AttachmentSender
  var sender = new x_snd_eb.AttachmentSender(transaction, bond);
  sender.next();

  var file_name = sender.attachment_name;
  var mime_type = sender.attachment_type;
  var attach_id = sender.attachment_id;


  //-----------------------
  // Path construction
  //-----------------------
  scratchpad.file_name = encodeURIComponent(file_name);
  scratchpad.reference = stage.external_reference;

  // /attachment?file_name=${scratchpad.file_name}&reference=${scratchpad.reference}

  //-----------------------
  // Headers construction
  //-----------------------

  // Compose the scratchpad headers and payload
  scratchpad.headers = {
    "Content-Type": mime_type
  };

  //-----------------------
  // Payload construction
  //-----------------------

  // This is the string that triggers attachment streaming outbound
  payload = 'sys_attachment:' + attach_id;

})(request, stage, transaction, message, bond, scratchpad);

file_name: It is necessary to encode the file_name so that the characters are converted into a format that can be transmitted and understood in the endpoint URL. Not doing so could cause the Transaction to fail (particularly if the file_name contains special characters).

payload: To stream outbound, you will need to set the payload to be "sys_attachment:<sysid>". Unifi will recognise this and automatically stream the attachment out.

You would configure other message data within the URL or the headers.

Payload to Stage (Inbound)

The code in the ‘Payload to Stage’ script field should look like this:

(function processPayloadToStage(payload, stage, transaction, message) {

  // set either internal/external reference on stage depending on what is needed.
  // make sure to configure the Bond reference lookup on the Message to be Internal/External
  stage.internal_reference = payload.reference;

})(payload, stage, transaction, message);

We have chosen to set the internal reference on stage as we have set the Bond reference method to 'Internal' (see above). I_f we had set the Bond reference method to 'External', we would need to set the stage external reference here (or both if we had set the Bond reference method to 'Both')._

Your Script Editor fields should now look like this:

Save the message.

Next, we shall configure the Scripted REST Resource.