Mar 11, 2020 10:11 AM
I’ve been trying to play around with the scripting base.
I’m very green in JS - so green that I just made it out of the codecademy foundations.
I’m trying to make something very basic.
I want my script to copy value in Field A if there is no value in Field B.
I’ve been banging my head against the wall for a few hours while trying to find help in the examples. But can’t get anything to work. Wondering if there is someone in here that can check my script and point me in the right direction?
let table = base.getTable("Test table");
let fromField = table.getField("fromField");
let toField = table.getField("destinationField");
let result = await table.selectRecordsAsync();
for (let record of result.records) {
let originalValue = record.getCellValue(fromField);
//console.log(originalValue)
if(record.getCellValue(toField)!==null){
table.updateRecordAsync(record, {
fromField: originalValue,
tofield: originalValue
}
}else{
continue
}
}
Mar 11, 2020 10:53 AM
You almost had it! The problem seemed to be with your call to updateRecordAsync()
. You passed in the Field objects when the object needs to be “field names or IDs” as a String. The fastest thing I tried was pasting the Field names from lines 2 and 3 in (calling fromField.id
did not seem to do the trick).
One bug I noticed, while the code you have will copy a value to Field B if there’s not a value already there, it will copy a blank cell from Field A. This might be intentional, but if you don’t want Field B to be overwritten if nothing exists in Field A, edit the if
statement to include originalValue &&
.
Modified script is below.
let table = base.getTable("test");
let fromField = table.getField("fromField");
let toField = table.getField("destField");
let result = await table.selectRecordsAsync();
for (let record of result.records) {
let originalValue = record.getCellValue(fromField);
let destValue = record.getCellValue(toField);
// If nothing in Field B AND there was something in Field A
if(destValue !== null && originalValue){
//Copy the info
table.updateRecordAsync(record, {
//The keys here need to be Strings (fromField.id did not work for me)
"fromField": originalValue,
"destField": originalValue
});
}
}
Mar 11, 2020 06:17 PM
Note: if(destValue !== null && originalValue)
should probably be if(destValue === null && originalValue)
and you should add an await
before table.updateRecordAsync
.
Mar 13, 2020 12:05 PM
Thank you both @Matthew_Thomas & @Shrey_Banga