Skip to main content
Solved

Need help with JS includes method

  • October 8, 2021
  • 7 replies
  • 41 views

Rupert_Hoffsch1
Forum|alt.badge.img+21

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!

Best answer by Jono_Prest

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?

7 replies

  • Inspiring
  • 26 replies
  • Answer
  • October 8, 2021

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?


Alexey_Gusev
Forum|alt.badge.img+25
  • Brainy
  • 1261 replies
  • October 8, 2021

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”


kuovonne
Forum|alt.badge.img+29
  • Brainy
  • 6009 replies
  • October 8, 2021

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.


Forum|alt.badge.img+17
  • Inspiring
  • 262 replies
  • October 8, 2021

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.


kuovonne
Forum|alt.badge.img+29
  • Brainy
  • 6009 replies
  • October 8, 2021

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.


Rupert_Hoffsch1
Forum|alt.badge.img+21
  • Author
  • Brainy
  • 292 replies
  • October 9, 2021

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?


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


Rupert_Hoffsch1
Forum|alt.badge.img+21
  • Author
  • Brainy
  • 292 replies
  • October 9, 2021

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.


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