Scripting Question

Hey everyone,I’m trying to send out an email using automations, but I can’t figure out how to do it correctly….I have a USERS table with USER_EMAIL along with DAYS_TENURE (how long they’ve been signed up)I have a PROGRAMMING table with a DAY_ID column, and a few additional columns with relevant data. For each user, I want to find their DAYS_TENURE value, then find the records in the PROGRAMMING table with the matching value in the DAY_ID column, and email them the 3 additional columns from the PROGRAMMING table.

I think I need to use scripting to do this, but I’m running into issues. I’m more of a SQL and python person, so there’s something I’m missing for javascript…

In my code, I’m trying to do a nested for loop…the outer loop loops through each unique DAY_ID from the PROGRAMMING table, and the inner for loop adds the users from the USER table to a javascript object if their DAYS_TENURE matches the DAY_ID.
For the sake of simplicity, let’s say I have DAY_ID values of 1, 2….and for DAYS_TENURE I have values of 1,2,3,4,5. My code is…

let tableProg = base.getTable('PROGRAMMING');
let queryProg = await tableProg.selectRecordsAsync();

let tableUser = base.getTable('USER');
let queryUser = await tableUser.selectRecordsAsync();

let daysUnique = [...new Set(queryProg.records.map(days => days.getCellValue('DAY_ID')))];

let jsObject = []

daysUnique.forEach(day => {
  queryUser.records.map(record => {
    if (day = record.getCellValue('TENURE_DAY')) {
      jsObject.push({
        dayNum: day
        ,name: record.getCellValue('USER_EMAIL')
        ,tenure: record.getCellValue('TENURE_DAY')
      });
    }
  })
});

console.log(jsObject)

For the outer loop…
daysUnique.forEach(day => {
I expect day to have the value for each unique DAY_ID (1,2). When I run the inner loop, I would expect this value to carry through so I can do this if statement…
if (day = record.getCellValue('TENURE_DAY')) {
However, it seems that when I run this code, day is no longer keeps the value of DAY_ID. It somehow returns the value of DAYS_TENURE from the inner loop (1,2,3,4,5).
I have no idea how it happens, but when I console.log the results, the javascript object dayNum field…
dayNum: day
has values of 1,2,3,4,5……not just 1,2.
I know that’s a lot, but hopefully it makes sense. Any ideas?

The comparison operator in JavaScript is == (or === if you look for object identity). Your

if (day = record.getCellValue('TENURE_DAY'))

line has only one =, which makes it an assignment of the TENURE_DAY value to the day variable. Yes, JS allows assignments in conditions, and yes, that’s not exactly the best language design ever, but when talking about JavaScript, one could argue the latter is par for the course.

2 Likes