Help

IF Statement to stop script from running if a field has value

Topic Labels: Scripting extentions
Solved
Jump to Solution
1790 2
cancel
Showing results for 
Search instead for 
Did you mean: 
capt
5 - Automation Enthusiast
5 - Automation Enthusiast

Hi

I managed to get a button to run a script to populate a linked field with a unique value, thanks to the exellent advice by @JonathanBowen

The link to the topic is here

As the populated field only ever has the value pasted by the script or is empty, I wonder if I can adapt the script to only run if my target field is empty and display a warning and abort if it isn’t?

For ease, here is the script…

let repoT = base.getTable('Repo');
let sourceT = base.getTable('Source');

let record = await input.recordAsync('Pick a record', sourceT);
// let name = record.getCellValue('Part 1') + '-' + record.getCellValue('Part 2');
let name = record.getCellValue('Data');
// create the record in the repo table
let newRepoRecord = await repoT.createRecordAsync({
        'Name': name
})
    // set variable for today's date
let now = new Date().toLocaleDateString("en-GB");
//note that newRepoRecord is the record ID of the created record
// see this by logging the result:

console.log(newRepoRecord);

// now take the newly created record ID
// and set the linked field from this
let updatedRecord = await sourceT.updateRecordAsync(record, {
    'Repo': [{id: newRepoRecord}],
    'Part Created': "Part Entered " + now
})

The field I want to check is ‘Repo’ and if Repo already has content then I don’t want the script to run.

Many thanks
Tom

1 Solution

Accepted Solutions
Justin_Barrett
18 - Pluto
18 - Pluto

if statements in JavaScript are structured like this:

if (condition) {
    // execute if true
}

To execute some other code if the condition is false, use this structure:

if (condition) {
    // execute if true
} else {
    // execute if false
}

Here’s how I applied that to your script:

let repoT = base.getTable('Repo');
let sourceT = base.getTable('Source');

let record = await input.recordAsync('Pick a record', sourceT);
// let name = record.getCellValue('Part 1') + '-' + record.getCellValue('Part 2');

if (!record.getCellValue('Repo')) {
    let name = record.getCellValue('Data');
    // create the record in the repo table
    let newRepoRecord = await repoT.createRecordAsync({
            'Name': name
    })
        // set variable for today's date
    let now = new Date().toLocaleDateString("en-GB");
    //note that newRepoRecord is the record ID of the created record
    // see this by logging the result:

    console.log(newRepoRecord);

    // now take the newly created record ID
    // and set the linked field from this
    let updatedRecord = await sourceT.updateRecordAsync(record, {
        'Repo': [{id: newRepoRecord}],
        'Part Created': "Part Entered " + now
    })
} else {
    console.log("Repo is already linked for this record");
}

record.getCellValue('Repo') will return null if the field is empty, and null is equivalent to false for the purposes of these conditional tests, while any data returned would be equivalent to true. Because you want to run the remaining code if it is empty, the exclamation point is added in front of the condition to invert the result, making false become true and vice versa. In short, the if structure as written above effectively says, “if the field is empty, run this stuff; otherwise display a message”.

See Solution in Thread

2 Replies 2
Justin_Barrett
18 - Pluto
18 - Pluto

if statements in JavaScript are structured like this:

if (condition) {
    // execute if true
}

To execute some other code if the condition is false, use this structure:

if (condition) {
    // execute if true
} else {
    // execute if false
}

Here’s how I applied that to your script:

let repoT = base.getTable('Repo');
let sourceT = base.getTable('Source');

let record = await input.recordAsync('Pick a record', sourceT);
// let name = record.getCellValue('Part 1') + '-' + record.getCellValue('Part 2');

if (!record.getCellValue('Repo')) {
    let name = record.getCellValue('Data');
    // create the record in the repo table
    let newRepoRecord = await repoT.createRecordAsync({
            'Name': name
    })
        // set variable for today's date
    let now = new Date().toLocaleDateString("en-GB");
    //note that newRepoRecord is the record ID of the created record
    // see this by logging the result:

    console.log(newRepoRecord);

    // now take the newly created record ID
    // and set the linked field from this
    let updatedRecord = await sourceT.updateRecordAsync(record, {
        'Repo': [{id: newRepoRecord}],
        'Part Created': "Part Entered " + now
    })
} else {
    console.log("Repo is already linked for this record");
}

record.getCellValue('Repo') will return null if the field is empty, and null is equivalent to false for the purposes of these conditional tests, while any data returned would be equivalent to true. Because you want to run the remaining code if it is empty, the exclamation point is added in front of the condition to invert the result, making false become true and vice versa. In short, the if structure as written above effectively says, “if the field is empty, run this stuff; otherwise display a message”.

capt
5 - Automation Enthusiast
5 - Automation Enthusiast

Absolutely brilliant @Justin_Barrett , thanks so much!!!

This works like a charm
Best wishes
Tom