Scripting recipes
Server script recipes for attachments over HTTP and base64, and for testing a record against a condition stored on another record.
These recipes solve jobs that integration developers meet often: moving attachments in and out of Servicely, and checking a record against a condition that an administrator maintains. Each recipe is a server script you adapt, with links to the reference for every object it uses.
What you will learn
- Create an attachment from a base64 string sent to a system controller.
- Download a file from a URL and attach it to a record.
- Send an attachment to another system as base64 or as bytes.
- Test whether a record matches a condition field on another record.
Before you begin
- You can write server scripts, such as script libraries, triggers and system controllers.
- For outbound calls, an administrator has created a System API Outbound Token for the target system. See Outbound credentials in the Administrator guide.
- The record IDs, field names and URLs in the scripts are examples. Replace them with your own.
Step 1: Bring attachments in
From a base64 string
An external system posts a file to a custom endpoint as a base64 string. The controller script creates the Attachment record and links it to the incident’s Attachments field.
{
"incident_id": "00000000000000000000000000000001",
"data": {
"mimeType": "image/png",
"fileName": "example.png",
"base64String": "<base64 data>"
}
}
if (incident_id && data) {
let incRec = Table("Incident", incident_id);
if (incRec) {
Table("Attachment").newRecord()
.MimeType(data.mimeType)
.FileName(data.fileName)
.Data(data.base64String)
.RelatedField("Attachments")
.ParentRecord(incRec.getID() + ":Incident")
.create();
}
}
ParentRecord takes the record ID and the table name, separated by a colon.
From a URL
executeBinary() keeps the response as bytes, and toAttachment saves them as an attachment. See HTTP.
let fileURL = "https://example.com/files/diagram.png";
let incRec = Table("Incident", "00000000000000000000000000000001");
HTTP.get(fileURL).executeBinary().toAttachment({
RelatedField: "Attachments",
ParentRecord: incRec,
ExternalKey: fileURL,
FileName: "diagram.png"
});
Step 2: Send attachments out
apiTokenAuth signs the request with the named outbound token. attachmentRec is an Attachment record your script has already read.
As base64
let tokenName = "Example outbound token";
let targetURL = "https://example.com/api/files";
HTTP.post(targetURL)
.accept("application/json")
.contentType(attachmentRec.MimeType())
.body(Base64Util.encodeBase64String(attachmentRec.Data()))
.apiTokenAuth(tokenName)
.execute();
As bytes
Use this form when the target system expects the raw file.
HTTP.post(targetURL)
.accept("application/json")
.contentType(attachmentRec.MimeType())
.bodyBytes(attachmentRec.Data())
.apiTokenAuth(tokenName)
.execute();
To send attachments as multipart form data, use fieldAttachmentById instead.
Step 3: Check a record against a stored condition
An administrator keeps rules in a custom table with a table field and a condition field, like the Applies to panel of a report. Your script tests a record against one of those conditions.

matchesCriterion on a record takes the condition field’s value and returns true when the record matches.
// incRec is an Incident record. customRec is a record of the custom table,
// whose condition field is named C_Condition.
let matches = incRec.matchesCriterion(customRec.C_Condition());
log.info(matches); // true when the incident's Classification is Other
What you have done
You created attachments from base64 data and from a download, sent attachments to another system in two formats, and tested a record against a condition held in data. The same objects cover other jobs: AttachmentManager moves attachments between fields, and Websocket reports progress to the user while a script runs.