Help

Need help with JS includes method

Topic Labels: Scripting extentions
Solved
Jump to Solution
1886 7
cancel
Showing results for 
Search instead for 
Did you mean: 

I’m looping over some records and want to increment ‘i’ ONLY if both conditions below are met:

let i = 0;
for (let record of taskRecords.records) {
    if(id.includes(record.getCellValue('ID Lookup')) && record.getCellValue('Stage').includes(stage)) {
        i++;
    }
}

console.log(i);

My problem is the second condition. The cell value of the field ‘Stage’ is “This is just some dummy text | this is a string” and it’s searching for “this is a string” (this is called ‘stage’ and is a string from a previous step in an automation).

i remains 0 in this case. Can anyone explain why includes doesn’t work on the second condition? Thanks!

1 Solution

Accepted Solutions
Jono_Prest
6 - Interface Innovator
6 - Interface Innovator

What type of field is your ‘Stage’ field?

Depending on the field type it may need to be coerced into a string. Maybe try using the method getCellValueAsString instead of getCellValue?

See Solution in Thread

7 Replies 7
Jono_Prest
6 - Interface Innovator
6 - Interface Innovator

What type of field is your ‘Stage’ field?

Depending on the field type it may need to be coerced into a string. Maybe try using the method getCellValueAsString instead of getCellValue?

Hi,

just guessing - string method ‘includes’ is case-sensitive, if your “this is a string” in cell starting from capital, try to search for “his a string”

Here’s a generic troubleshooting tip.

Assign the cell values to variables. Then console.log all the variables before your `if`` statement. One or more of the values may not be what you expect.

On top of that, if you’re polling non-array stuff with Array.prototype.includes, you’re going to have a bad time, that syntax only works due to destructuring coercion shenanigans which really aren’t the type of behavior you want your code to depend on. Look into substring/substr methods if the cell value is a string.

Both strings and arrays have an includes() method. So, if you don’t know what data type you have (array versus string), you might get expected results for the wrong reasons.

This was it. I was running .includes on an object and not a string. So that worked, thank you!

As @kuovonne mentioned, .includes exists for strings as well. For my understanding (JS noob here): where do you see the issue with that? Thanks