Jun 10, 2021 02:54 AM
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
Solved! Go to Solution.
Jun 10, 2021 04:51 AM
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”.
Jun 10, 2021 04:51 AM
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”.
Jun 10, 2021 06:44 AM
Absolutely brilliant @Justin_Barrett , thanks so much!!!
This works like a charm
Best wishes
Tom