---
title: "Scripting recipes"
site: api
version: 1.11
versions: "all"
url: https://docs-new.servicely.ai/api/1.11/scripting/recipes/
type: tutorial
application: platform
last_updated: 2026-10-18
commit: f37120b6b82b
---


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](page:admin/integrations/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](page:rest/custom-endpoints) as a base64 string. The controller script creates the Attachment record and links it to the incident's `Attachments` field.

```json
{
    "incident_id": "00000000000000000000000000000001",
    "data": {
        "mimeType": "image/png",
        "fileName": "example.png",
        "base64String": "<base64 data>"
    }
}
```

```javascript
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](page:scripting/http).

```javascript
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

```javascript
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.

```javascript
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.

<Image src="asset:screenshots/api/scripting-recipes/image.png">An Applies to panel with the table set to Incident and a condition of Classification is Other.</Image>

`matchesCriterion` on a record takes the condition field's value and returns true when the record matches.

```javascript
// 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](page:scripting/attachment-manager) moves attachments between fields, and [Websocket](page:scripting/websocket) reports progress to the user while a script runs.

## Related

- [HTTP](page:scripting/http)
- [AttachmentManager](page:scripting/attachment-manager)
- [MarkupConverter and HtmlUtils](page:scripting/markup-converter-and-html-utils)
- [Custom endpoints](page:rest/custom-endpoints)
