Help

Comparison logic does not work even if the values being compared are the same

Topic Labels: Automations
Solved
Jump to Solution
1228 2
cancel
Showing results for 
Search instead for 
Did you mean: 
KenF
4 - Data Explorer
4 - Data Explorer
Hello all
 
My script below, looks through records in a table to find any records that have a field value that matches the record that kicked-off the script (automation) when a button was clicked. The field used for the condition is a Single Line Text field (Tracker Cycle ID). The actual value being compared is passed into the script via input.config (matchCycleID). 
 
I do not get an error but the script does not work as intended and the script editor tells me "This condition will always return 'false' since the types 'string' and '{ readonly matchCycleID: string; }' have no overlap.(2367)".
 
I would assume the value of the field being passed into the script via input.config is a different type of 'string' than the value in the record being compared (record.getCellValueAsString('Tracker Cycle ID')).
 
As a test, if i rewrite the comparison (line 6 below) as:
 
if (record.getCellValueAsString('Tracker Cycle ID') === "Tracker ID 4") {
 
then it works as intended. Is there some way to make this comparison work that I am missing?
 
 
 
let matchCycleID = input.config()
const table = base.getTable("TRK: KPI")
const query = await table.selectRecordsAsync({fields: ["Status","Tracker Cycle ID"]})
console.log(matchCycleID)
for (let record of query.records) { 
    if (record.getCellValueAsString('Tracker Cycle ID') === matchCycleID) {
      console.log("match")
      await table.updateRecordAsync(record, {"Status": {"name": "Closed Resolved"}})
    }
}
1 Solution

Accepted Solutions
KenF
4 - Data Explorer
4 - Data Explorer

Hey Ben

Thank you for taking the time to look at this. It works great! Thank you!!

Ken

See Solution in Thread

2 Replies 2

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.

KenF
4 - Data Explorer
4 - Data Explorer

Hey Ben

Thank you for taking the time to look at this. It works great! Thank you!!

Ken