How to treat first item in a foreach (or similar) different than the rest

I need a way to do a slightly different operation on the first item in a foreach loop. I could use something different than foreach, but the result need to be similar to this code:

 If(
   count(thisRow.Organizations)=1,
     ModifyRows(thisRow,thisRow.Organisation,thisRow.Organizations),
     RunActions(
       thisRow.Organizations.ForEach(
         AddRow(
           [TableName],
           [TableName].FirstName,thisRow.FirstName,
           [TableName].LastName,thisRow.LastName,
           [TableName].Mail,thisRow.Mail,
           )
       ),
       thisRow.DeleteRows()
     )
)

Code explanation
Simply put, it checks if there is just one organization for each candidate row. If it’s only one, then it copies that organization to the new organizations row.

If not (there is more than one organization), it splits each candidate row and duplicates them for each organization with one unique organization in the “Organizations”.

Solution needed
In the latter case, I need a way to identify the first (or last) row that is duplicated. Perhaps set a checkmark in a new column? Because I have a automation rule to send the candidates a response mail. And the way it is now, the candidates will get duplicate mails. This automation HAS to be on a timer, and can not run before the rows are split because of another operation that happens when new rows are added. The “on form submit” trigger does not work well with gmail pack unfortunately. Has to be some kind of bug.

I need to add something like this on one of the duplicated rows:
[TableName].UniqueRow=true

How should I go about this? I’ve not been able to find anything in my search so far.

Perhaps use the find formula, something like this?

If(
   count(thisRow.Organizations)=1,
     ModifyRows(thisRow,thisRow.Organisation,thisRow.Organizations),
     RunActions(
       thisRow.Organizations.ForEach(
         AddRow(
           [TableName],
           [TableName].FirstName,thisRow.FirstName,
           [TableName].LastName,thisRow.LastName,
           [TableName].Mail,thisRow.Mail,
           [TableName].NewColumn,thisRow.Organizations.Find(CurrentValue)=1
           )
       ),
       thisRow.DeleteRows()
     )
)
1 Like

Hi Rickard

How would this find formula work exatly? I’ve seen some examples, and it looks like it can find for example text within something. Not sure if this accomplishes what I want. I can’t make your example work.

This is a different language for the columns and a bit more complex than the slimmed down example I gave, but it might give some better idea:

Even though if this might find the first item of the organizations, I don’t see how exactly I could set the checkmark column “NewColumn” to true with this.

Sorry if I’m missing obvious stuff here. Just starting to get the grasp of some of the simpler functions here. Coding is not my cup of tea :slight_smile:

Hi Carl,

A clarification please.

Do you need a slightly different process every time for the first item in the list?
Or do you want a slightly different process if the list is a single item?

If the latter, I would suggest that you do an if() to check if the list is single. And then proceed correspondingly.

The first is a little more difficult. What about adding a ranking column. Then you do an if() to check for the rank=1, “then” covers your first item, and your “else” users ForEach to go through the rest of the list.

It’s just a ramble…
Ranking Pete

1 Like

Haha don’t worry I can read norrbagge :sweden: - It seems like you got the order a bit wrong, see what the error message says. The highlighted red error message is mentioning that the parameter “column” is wrong, it expects a column or list of columns, not a boolean.

Try swapping the position of our new formula line with the line above it :slight_smile:


Find works on anything that’s iterable, which text is also. It returns the index of the object (needle) inside the iterable (haystack). If it doesn’t exist then it returns -1

2 Likes

You are absolutely right, Rickard! That worked very well. I do not know exactly how, but as long as it does the right thing I’m happy. Hehe. I created a new column with checkmark and it works very well by marking one of the items as unique. That way I can run automations for sending mail just on the unique rows. Beautiful! Here’s the formula in case someone want to see later on:

It was my stupid mistake of entering the new line of code in the middle of an AddRow part. I’m tempted to pass partial blame to Coda for not having wider screen for editing the formulas, but I’ll take the responsibility of my stupid oversight yet again :slightly_smiling_face:

Thanks for the help!

1 Like

No, you make a lot of sense. And it’s partially what I’ve been doing so far. Having an If statement to figure out if there are multiple organizations. If yes, then I want to identify one of the new rows as Unique.

So, it’s a yes to the first part of your question. I need to just do one slight variation for the first organization if multiple. All subsequent organizations just need the same type of data. Each organization will have a new row if multiple organizations is identified, and only one of them has to be marked as unique as to not sending multiple mails to the same candidate.

I tested the additional code from Rickard now, and it worked very well after I placed it in the correct line in my formula. It marks the first item if there are multiple, as unique.

Thanks for the ideas! :slight_smile:

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.