Skip to main content

I am trying to use a loop to create rows in Excel for each item in the loop. My loop items are a list of records from a Salesforce query: Class A, Class B.

For each class, I would like to add rows to an Excel spreadsheet: Teacher 1, Teacher 2, Teacher 3. (Imagine that the same 3 teachers teach every class).

Can any suggest how to adjust the Add Rows action or how I should do this differently?

This is how my Add Rows action looks: 

 

Here is the Loop action:

I want the output to be: 

Class A Teacher 1

Class A

Teacher 2
Class A Teacher 3
Class B Teacher 1
Class B Teacher 2
Class B Teacher 3

 

Instead, I am getting: 

Class A Teacher 1

 

Teacher 2
  Teacher 3
Class B Teacher 1
  Teacher 2
  Teacher 3

Hi ​@CarolynR 

You essentially need a loop (classes) within a loop (teachers).

e.g. For each class, iterate these teachers.

 

One option would be to create a Code step that creates the array of data for you to then iterate thru in the Looping step.

Code help: https://zapier.com/apps/code/integrations#help

 

JAVASCRIPT

INPUTS

CODE

const classIds = inputData.class_id.split(",").map(item => item.trim());
const userIds = inputData.user_id.split(",").map(item => item.trim());
const classRoles = inputData.class_role.split(",").map(item => item.trim());

let group = u];

if (userIds.length !== classRoles.length) {
throw new Error("user_id and class_role must have the same number of values.");
}

classIds.forEach(classId => {
userIds.forEach((userId, index) => {
group.push({
class_id: classId,
user_id: userId,
class_role: classRolesRindex]
});
});
});

return { results: group };

 

OUTPUTS

 

LOOPING

 


Reply