Converting a script from Scripting Extension to an Automation Script

Say you found the perfect script for your needs, except that it runs in Scripting Extension, and you want to run it as an automation? How can you get that script to run as an automation? This article explores some of the concepts involved for people who are new to code.

When a simple conversion isn’t possible

In some situations, a script cannot be converted to run as an automation:

  • The script needs user input that cannot be hard-coded in advance or omitted.
  • The script inputs a file from the local computer with input.fileAsync.

In other situations, a script might need significant changes that are beyond the scope of this article. Note that these situations might not show up until a script is run in a production environment with many records.

  • The script takes over 30 seconds to run.
  • The script uses over 512 MB of memory.
  • The script makes over 50 fetch requests.
  • The script makes over 30 selectRecords queries.

Some scripts are designed to process all the records in a table or view. However, when the script runs as an automation, you may want to process only the triggering record. Changing these scripts to process only a single record is also beyond the scope of this article.

Converting script settings

Some (but not all scripts) contain script settings. If a script has a section that looks like this, it uses script settings. This entire section needs to be reworked to hard-code your table and field names, and any other options.

let config = input.config({
    title: 'Your script with settings',
    description: 'A script that uses settings',
    items: [
        input.config.table('selectedTable', {
            label: 'Table to use',
            description: 'Pick any table in this base!',
        }),
        input.config.field('selectedField', {
            label: 'Field inside the above table',
            parentTable: 'selectedTable',
        }),
        input.config.text('companyName', {
            label: 'Company name',
        }),
        input.config.number('maxItemsPerOrder', {
            label: 'Maximum number of items per order',
        }),
    ]
});
let selectedField = config.selectedField;
let companyName = config.companyName;
let maxItemsPerOrder = config.maxItemsPerOrder; 

The automation script version would would look like the following. Replace the names of the table and field with the names that match your base. Replace the text and number with the actual text and numbers you want to use.

const selectedTable = based.getTable("Table Name");
const selectedField = selectedTable.getField("Field Name");
const companyName = "My Company";
const maxItemsPerOrder = 10;

Getting the triggering record

If your script uses input.recordAsync to get a triggering record from the user, you need a completely different method of getting the triggering record and its field values.

For example, convert

let record = await input.recordAsync('Pick a record', table);

to

let inputConfig = input.config();
let recordId = inputConfig.recordId;
let record = await table.selectRecordAsync(recordId, {fields: table.fields});

You will also need to create the input variable recordId in the left panel of the scripting editor and set it to a valid record ID.

image.png

Note that if a script uses input.recordAsync multiple times, it is not a good candidate for converting to an automation.

Removing user input

If your script uses input to get input from the user, such as input.textAsync, or input.buttonsAsync, you will need to hard-code those values.

For example, convert

let catOrDog = await input.buttonsAsync('Cats or dogs?', ['Cats!', 'Dogs!']);

to

let catOrDog = 'Cats!';

Removing Output

If your script uses output to show information on the screen, such as output.text, output.markdown, or output.table, you will need to either delete those lines of code or convert them to use console.log. If your script uses output.clear(), delete it.

For example, convert

output.text('Hello, world!');

to

console.log('Hello, world!');
6 Likes

Thank you for this @Kuovonne! It’s very helpful :slight_smile: