Dec 14, 2023 02:48 PM
Solved! Go to Solution.
Dec 18, 2023 01:54 PM - edited Dec 18, 2023 01:59 PM
Hey Ben
Thank you for taking the time to look at this. It works great! Thank you!!
Ken
Dec 14, 2023 05:19 PM
Hey @KenF!
Your script isn't behaving as expected because of your matchCycleID variable.
Specifically, you are directly assigning it the value returned by invoking the input.config method.
When called, the input.config method returns an object whose properties are the values you've set in the script's input configuration within the sidebar.
This can also be deduced from the exception that was returned when you attempt to run the script.
This condition will always return 'false' since the types 'string' and '{ readonly matchCycleID: string; }' have no overlap.(2367)
This is correct, since you're trying to compare the string value of a given record's match cycle ID is against an object.
To fix this, you'll need to properly access the value of the matchCycleID property. You can do this by making a small tweak as seen in this example:
const { matchCycleID } = input.config();
const table = base.getTable("TRK: KPI");
const query = await table.selectRecordsAsync({ fields: ["Status","Tracker Cycle ID"] });
for (let record of query.records) {
if (record.getCellValueAsString('Tracker Cycle ID') === matchCycleID) {
console.log("match");
await table.updateRecordAsync(record, {"Status": {"name": "Closed Resolved"}})
}
};
That should work as expected.
As a maintenance item, it's worth noting that if your script ever has to handle a situation where it needs to update a large number of records or just generally has to handle a scaled database, you're going to run into issues, as calling the updateRecordAsync method one at a time can be incredibly slow.
This will result in your script hitting the thirty second execution time restriction and returning an error.
As a rule of thumb, when updating multiple records, you'll want to leverage the updateRecordsAsync method. This method will allow you to update records in batches of fifty.
If you have to update more than fifty records, you can simply create multiple batches.
Here's an example of one of the many forms its implementation may take:
const { matchCycleID } = input.config();
const table = base.getTable('TRK: KPI');
const fields = ['Status', 'Tracker Cycle ID'];
function filterRecord (record) {
if (record.getCellValueAsString(fields[1]) === matchCycleID) {
return true;
}
}
function mapUpdate (record) {
return Object.defineProperties({}, {
id: { value: record.id, enumerable: true },
fields: {
value: { [fields[0]]: { name: 'Closed Resolved' } },
enumerable: true
}
});
}
let records = await table.selectRecordsAsync({ fields: fields })
.then(query => query.records.filter(filterRecord));
let recordUpdates = records.map(mapUpdate);
if (recordUpdates.length) {
while (recordUpdates.length) {
await table.updateRecordAsync(recordUpdates.slice(0, 50));
recordUpdates = recordUpdates.slice(50);
}
};
There's a good chance that script will actually work perfectly if you try to use it, but I might've missed a small detail somewhere.
I'll reiterate that there are countless ways to approach this. Regardless, I'm happy to answer any questions or assist in troubleshooting this further if you continue to run into any issues.
Dec 18, 2023 01:54 PM - edited Dec 18, 2023 01:59 PM
Hey Ben
Thank you for taking the time to look at this. It works great! Thank you!!
Ken