Skip to main content
Best answer

How to repeat the loop item name when adding rows in Excel

  • January 28, 2025
  • 1 reply
  • 9 views

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

Best answer by Troy TessaloneBest answer by Troy Tessalone

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 = [];

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: classRoles[index]
        });
    });
});

return { results: group };

 

OUTPUTS

 

LOOPING

 

View original
Did this topic help you find an answer to your question?

1 reply

Troy Tessalone
Forum|alt.badge.img+14
  • Zapier Expert
  • 30739 replies
  • Answer
  • January 28, 2025

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 = [];

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: classRoles[index]
        });
    });
});

return { results: group };

 

OUTPUTS

 

LOOPING