ForEach and Query AdditionalScope

  • Goal: I have a multiselect drop down where you can pick multiple records. I want to associate contacts with multiple records so I can see which projects they are associated with

  • Steps: I created two queries, one is a sql query for the retool database and another is a JS query. The JS query runs through the iterations of the multiselect using forEach and calls the sql query through each iteration to add the records that are associated with that specific multiselect record

  • Details:
    const projectsLinked = multiselect1.selectedItems;

projectsLinked.forEach(async row => {
await insertNewContact.trigger({
additionalScope: {
project_record_index: row.project_Num_ID
}
});
})

the project information is located in a separate table from the contact information. I'm using the project_Num_ID to associate the contact with whatever project. However, the values from the multiselect never get assigned to the AdditionalScope field, it's always null even though when I hover over row.project_Num_ID I see the list of IDs.

Here is the SQL that I'm triggering from the JS. I am thinking that the additional values from the multiselect which need to be stored in the project_record_index column in my tblUserManagement table get passed from the JS as an additional scope to the SQL query but it's not working. Values are null in project_record_index

insert into "tblUserManagement" ( "firstName", "lastName", "legalAddressNumber", "legalAddressStreet","legalAddressCity", "legalAddressState","legalAddressZip","client_phone","clientEmail") values ({{textInput15.value}},{{textInput16.value}},{{numberInput6.value}},{{textInput17.value}},{{textInput18.value}},{{textInput19.value}},{{numberInput7.value}},{{textInput21.value}},{{textInput20.value}})

Thanks in advance for your help.

Resolved. I had to make sure that the AdditionalScope variable was included in the SQL, verbatim.

const projectsLinked = multiselect1.selectedItems;

projectsLinked.forEach(async row => {
await insertNewContact.trigger({
additionalScope: {
projectScopeIndex: row.project_Num_ID
}
});

})

Here is the SQL:

insert into "tblUserManagement" ( "firstName", "lastName", "legalAddressNumber", "legalAddressStreet","legalAddressCity", "legalAddressState","legalAddressZip","client_phone","clientEmail","project_record_index") values ({{textInput15.value}},{{textInput16.value}},{{numberInput6.value}},{{textInput17.value}},{{textInput18.value}},{{textInput19.value}},{{numberInput7.value}},{{textInput21.value}},{{textInput20.value}},{{projectScopeIndex}})