Parsware
All articles

BS Lang

Files from a script

A server-side script can write a file to storage and read it back, and it can put a file into one record's File field. They're two different places for two different kinds of file. We exported expense claims as a CSV into a bucket, then attached a receipt to one claim and found it on the claim's form.

The Attach a receipt to a claim script in the BS editor, calling putRecordFile with the claim, the Receipt field and the file the caller sent, next to the title "Files from a script"

A plugin step or a Custom API can work with files. There are two kinds, and they go in two different places:

  • A file that belongs to no record: an export, a file another system drops off. It goes into storage, in a bucket, under a key.
  • A file that belongs to one record: a receipt, a signed contract, a photo. It goes into that record's File or Image field.

We'll do one of each, with the sample solution in samples/solutions/custom-api-object-storage-export (it imports as Object Storage Export). We put it in our Test environment, because it has its own Expense claim table.

The claims

The sample has an Expense claim table with a reference, a claimant, an amount and a date. We added three claims in its app:

The Expense claims list in the Platform Shell: EXP-201, Dana Hughes, 84.5; EXP-202, Sam Taylor, 312; EXP-203, Robin Park, 46.2

The rest of the sample is five Custom APIs:

The Custom APIs of the sample: Attach a receipt to a claim, POST; Export claims to storage, POST; Fetch one export, GET; Read a claim's receipt, GET; List the exports, GET. All under /api/par/v1/claims.

Write a file to storage

Export claims to storage reads every claim, builds a CSV and stores it:

The Export claims to storage script: runQuery reads the claims, a loop adds one line per claim to a csv string, then putFileAs writes it to the par-exports bucket as text/csv

The line that writes it:

var written = putFileAs('par-exports', key, csv, 'text/csv');

A script's file is text it built. BS Lang has no bytes, so a script can write a CSV, some JSON or a letter, but not an image or a PDF.

Use putFileAs rather than putFile for anything somebody will download. putFile stores text/plain, and a browser shows that in the tab instead of saving it.

You don't have to create the bucket first. The first write creates it.

We called the API for September:

POST /api/par/v1/claims/export/2026-09   →  { "bucket": "par-exports", "key": "claims/2026-09.csv", "rows": 3 }

And the file was in Storage in the admin area, with its size and type:

The Storage screen for the par-exports bucket: one file, claims/2026-09.csv, 103 B, text/csv

Read it back, and list what's there

Fetch one export reads the file with getFile, and List the exports uses listFiles:

GET /api/par/v1/claims/exports/2026-09   →  reference,claimant,amount
                                            EXP-201,Dana Hughes,84.50
                                            EXP-202,Sam Taylor,312.00
                                            EXP-203,Robin Park,46.20

GET /api/par/v1/claims/exports           →  { "keys": "claims/2026-09.csv;", "count": 1 }

A key with nothing at it isn't an error. getFile answers an empty string, so a script can ask "is last month's export there?" and decide. This one refuses in its own words:

GET /api/par/v1/claims/exports/2026-08   →  400  No export has been written for that period yet.

The storage functions:

Function What it does
putFile(bucket, key, text) Writes text as text/plain
putFileAs(bucket, key, text, type) Writes text with the type you give
getFile(bucket, key) Reads it back, or "" if there's nothing there
fileExists(bucket, key) Whether there's a file at that key
listFiles(bucket, prefix) The files under a prefix, with key, size, type and date (up to 200)
deleteFile(bucket, key) Deletes it

Attach a file to a record

A receipt isn't an export. It belongs to one claim, so it goes in the claim's Receipt field, not in a bucket. Then it's read with the claim, deleted with the claim, and only someone who can read the claim can read it.

Attach a receipt to a claim finds the claim by its reference and puts the file there:

The Attach a receipt to a claim script: it looks up the claim with runQuery, refuses when there is none, then calls putRecordFile with par_claim, the claim's id, par_receipt, and the file name, type and base64 content from the request

putRecordFile('par_claim', found[0].id, 'par_receipt',
  receipt.fileName, receipt.contentType, receipt.contentBase64);

The script can't build an image, so it doesn't. The caller sends the file as base64 in the request, and putRecordFile decodes it on the platform's side. The script only carries it.

We sent a photo of a receipt for EXP-201:

POST /api/par/v1/claims/EXP-201/receipt
{ "receipt": { "fileName": "receipt-exp-201.png", "contentType": "image/png", "contentBase64": "iVBORw0…" } }

Then we opened the claim in the app:

The EXP-201 claim form: the claim's details on the left, and a Receipt section on the right with receipt-exp-201.png, 7.4 KB, and Replace, Download and Remove buttons

The receipt is on the claim, with Download beside it, exactly as if someone had uploaded it on the form.

The record's own rules still apply. putRecordFile needs the same right to change the claim that the form would, and the field's size limit is the platform's. Read a claim's receipt uses getRecordFile to answer the file's name, type and size:

GET /api/par/v1/claims/EXP-201/receipt   →  { "fileName": "receipt-exp-201.png", "contentType": "image/png", "sizeBytes": 7592 }

A reference that isn't a claim is refused in the script's words:

POST /api/par/v1/claims/EXP-999/receipt  →  400  There is no claim with that reference.

The export, as text

The script behind Export claims to storage, from the picture above:

var claims = runQuery({
  entity: 'par_claim',
  select: ['par_name', 'par_claimant', 'par_amount'],
  order: [{ field: 'par_name', descending: false }]
});

var csv: string = 'reference,claimant,amount\n';
for (var i: int = 0; i < claims.length(); i++) {
  csv = csv + `${claims[i].par_name},${claims[i].par_claimant},${claims[i].par_amount}` + '\n';
}

var key: string = 'claims/' + getInput('period') + '.csv';
var written = putFileAs('par-exports', key, csv, 'text/csv');
if (written == false) {
  throwValidationError('The export could not be written to storage.');
}

setOutput('bucket', 'par-exports');
setOutput('key', key);
setOutput('rows', claims.length());

Two spellings in it are the ones people get wrong. claims.length() needs its brackets, or the loop never runs and the export is a header with nothing under it. And the amount reaches the line through a template, ${…}, because BS Lang refuses 'text' + 1. BS Lang in ten minutes explains both rules.

A query from a script returns at most 200 rows, so this sample suits a modest export. For a large one, page through the query, or let a scheduled function do the work in the background, where a long run doesn't hold up a caller.

Choosing a key

A key is the file's full path inside its bucket, and a little planning makes a bucket easy to work with later:

  • Group by what it is, then when: claims/2026-09.csv, invoices/2026-09.csv. listFiles('par-exports', 'claims/') then lists every claims export and nothing else.
  • Put dates year first: 2026-09, so the keys sort in date order.
  • Make the same request write the same key. Exporting September twice overwrites claims/2026-09.csv rather than leaving two files nobody can tell apart.
  • Use your prefix in bucket names, such as par-exports. A bucket name is shared by every customer of the storage service, so a specific one avoids a clash.

A link somebody can download

A stored file is in a bucket, and the people who need it usually aren't signed in to the storage screen. fileLink makes a download link that works without signing in, for a number of minutes:

var link = fileLink('par-exports', key, 60);
if (!isEmpty(link)) {
  sendEmail('par_mail_service', {
    to: 'finance@example.com',
    subject: `Claims export for ${getInput('period')}`,
    body: `The export is ready. It can be downloaded for the next hour: ${link}`
  });
}

The link stops working when its time runs out, so an email forwarded next month opens nothing. 0 asks for the longest the installation allows. Export files to object storage covers the storage side.

That is different from a file on a record, which never gets a link like this: a record's file is only ever served to somebody who may read the record, through the record. Large files: where they go explains why. A link is right for a storage file, which belongs to no record, and wrong for a record's file, whose permissions should follow the record.

Storage or the record?

The file Put it Read it with Who can get it
An export, a report run, a file another system drops off Storage, with putFileAs getFile, listFiles, fileLink Whoever may use the bucket, or holds a link
A receipt, a contract, a photo of one thing The record's field, with putRecordFile getRecordFile Whoever may read the record

When in doubt, ask whether the file would make sense without the record. A claim's receipt doesn't, so it goes on the claim and is deleted with it. A month's export of every claim does, so it goes in storage and lives on its own.

Tidying up

Storage keeps whatever is written to it until something deletes it. A script that writes a file every day should decide how long those files are worth keeping, and remove the older ones:

var old = listFiles('par-exports', 'claims/2025-');
for (var i: int = 0; i < old.length(); i++) {
  deleteFile('par-exports', old[i].key);
}

deleteFile answers true when nothing is left at the key, including when nothing was ever there, so a tidy-up that runs twice is harmless. Files on records need no tidying: they go with their record.

Questions that come up

Can a script make a PDF or an image? No. A script builds text, so it can write CSV, JSON, HTML or plain text. A PDF comes from a report, and an image from somebody's camera or computer, through a form or an integration.

Where do I see what a script wrote? Under Storage in the maker portal's Admin rail, by bucket, with each file's size and type. What's in object storage walks through it.

Does the export count against our storage allowance? Yes. Storage files and record files come out of the same allowance, which the Storage screen shows at the top.

As whoever is calling

Every one of these runs as the caller. A script can't write to a bucket, or to a record, that the person or app calling the API couldn't write to themselves. An API is never a way around permissions.

Three things we fixed

We found three problems writing this article, all fixed in the same change:

  • listFiles failed. The storage service had started answering its lists a page at a time, and the script side still expected a plain list, so every listFiles stopped with a server error. It now reads the page.
  • A script's refusal was reworded. "There is no claim with that reference." reached the caller as "The request does not match this API's contract: There is no claim with that reference..", blaming a request that was fine. A refusal now arrives exactly as the script wrote it, which matters most on a portal, where a visitor reads it.
  • The editor underlined working code. It didn't know the file functions at all, so every putFileAs and putRecordFile was marked Function not found, and receipt.fileName was marked wrong too. Both are fixed, and there's now a test that fails if the server gains a function the editor doesn't know about.

Where it works

The file functions are available in Custom APIs and plugin steps, which run on the server. The storage ones work in scheduled jobs too. They aren't in form rules or command buttons, which run in the browser.

Try it

Import the sample, add a few claims, and call the export for this month. Then find the file under Storage. Attach a small image to one claim and open the claim.

Next: one script, two interpreters, and how we keep the browser and the server agreeing about BS Lang.