Help

Find and replace scripting for Automations

Topic Labels: Scripting extentions
2089 1
cancel
Showing results for 
Search instead for 
Did you mean: 
aaron_altamura
6 - Interface Innovator
6 - Interface Innovator

Hello Friends,

I’m basically trying to make this Find and Replace script run in the automations feature, except instead of manually typing the inputs, just using the input variables entered into the block.

I’m only python literate and thus far have only replaced the “findText” and “replaceText” variables:

let table = base.getTable("crm_deals");
let field = table.getField("Deal owner");
let findText = input.config()["Input variable name goes here"];
let replaceText = "Input variable name goes here";

// Load all of the records in the table
let result = await table.selectRecordsAsync();

// Find every record we need to update
let replacements = [];
for (let record of result.records) {
    let originalValue = record.getCellValue(field);
    // Skip records which don't have the value set, so the value is null
    if (!originalValue) {
        continue;
    }
    let newValue = originalValue.replace(findText, replaceText);
    if (originalValue !== newValue) {
        replacements.push({
            record,
            before: originalValue,
            after: newValue,
        });
    }
}
if (!replacements.length) {
    output.text('No replacements found');
} else {
    output.markdown('## Replacements');
    output.table(replacements);
    let shouldReplace = await input.buttonsAsync('Are you sure you want to save these changes?', [
        {label: 'Save', variant: 'danger'},
        {label: 'Cancel'},
    ]);
    if (shouldReplace === 'Save') {
        // Update the records
        let updates = replacements.map(replacement => ({
            id: replacement.record.id,
            fields: {
                [field.id]: replacement.after,
            }
        }));
        // Only up to 50 updates are allowed at one time, so do it in batches
        while (updates.length > 0) {
            await table.updateRecordsAsync(updates.slice(0, 50));
            updates = updates.slice(50);
        }
    }
}

The first error I’m getting is:

TypeError: originalValue.replace is not a function
at main on line 18

I also suspect more of this script is not viable for the automations feature :frowning:

Any breadcrumbs would be greatly appreciated!

1 Reply 1

This script has a lot of input and output, none of which will work with an automation. For example, the script keeps track of the “before” and “after” text in order to a table of the changes and ask the user whether or not to go ahead with the replacement. In an automation, you wouldn’t need to build that table/array or show it to the user.

Also, this script is designed to run through all the records in a table, not just the single record that triggered the automation.

Finally, it looks like you tried using input.config(), but it looks only partially implemented.

As for your error, the replace() function is a valid JavaScript function for a string. However sometimes the editor guesses incorrectly regarding the values of variables and gives incorrect error messages.

You are trying to figure out many things all at once (JavaScript, the Scripting API, this particular script, etc.), and it is difficult to determine where to start. If you are trying to adapt this script as a learning exercise (versus actually needing an automation with this functionality), I gently suggest that you start with something that won’t need quite so many changes.