How to look if condition met for any item in collection

:bullseye: What is your goal?

If any item in a collection meets condition, then do something…

:thinking: What is the problem?

Sorry for asking a basic question, but I am not lucky finding the right tutorial for many days now (yes, I started the Make academy, but it will take time…).

So, there has to be a simpler way of doing this. I have a collection of items in the Cognito Forms module. Next comes Google Sheets module. There I want it to output a “No” if at least one of the items in the respective collection is anything other than a “No” (it can be either Yes or No in this case).

Right now I am manually writing a looooong IF formula, listing each of the 18 items. And I need to do this many times in several scenarions. It works, but it’s a lot of work and I’ll have to setup a bunch of filters in the same way on top of that…

Please help me out of this. Thank you so much!

:camera_with_flash: Screenshots: scenario setup, module configuration, errors

1 Like

Hey there,

firstly → your logic is a bit wrong here. Don’t use one if with a bunch of ands inside, use a nested if chain. So if choice 1 is yes, then no, otherwise if choice 2 is yes, then no, otherwise if choice 3 is yes, then no and so on and so on.

Now for easier processing you can maybe try turning the entire collection to an array using the toArray() function. Then you can use a single contains() function to check if at least one of the values is a yes.

3 Likes

Thank you Stoyan,

unfortunately I couldn’t get the toArray solution to work. It keeps returning a negative, even if there is a Yes in the collection.

if(
contains(
toArray(1.);
“Yes”
);
no;
yes
)

Something is apparently wrong with the formula or the values in the collection.

Also, why is that logic I originally used not good? I mean it works… Would you mind to explain? Thank you.

1 Like

This is because the toArray function turns the collection into an a array of key:value pairs. To get an array of the values, use the map function before the contains:

contains(map(toArray(3); “value”); “yes”)

Screenshot 2025-12-12 00.01.12

1 Like

Its a developer thing about optimizing cpu usage. Technically one value needs to be Yes for everything to be No. So no need to check all the values, just keep checking them one by one until the first Yes.

And about the toArray, can you post the incoming data here so we can check and see how to best transform it?

I see, good to know. Thanks.

I was trying multiple versions of what dreamsuite_mike suggests, with parentheses and without, but none is working for me so far. This doesn’t seem to work :backhand_index_pointing_down:

Here is the part of the output data containing the collection:

Sorry I am pasting examples in 2 languages, but it’s the same thing.

Yeah its like Mike said in the post above. You need to add map() so you get the primitive array with the values. Here is the formula:

{{if(contains(map(toArray(1.DoYouHaveAPastHistoryOfHaveYouBeenDiagnosedWithOrAreYouCurrentlyExperiencingAnyOfTheFollowing); "value"); "Yes") = true; "No"; "Yes")}}

Thank you for the exact formula, Stoyan. Still no luck, unfortunately… :lying_face: I must still be doing something wrong. I am also not entirely sure if I am getting it right with the { vs. ( parentheses. My latest attempt is this:

But it returns a “yes” irrespective of whether there is a “Yes” in the collection or not.

It must have broken during copy/pasting. Remove all of the double quotes and check for any empty spaces.

1 Like

It would be easier if you could provide a blueprint export and an output export of the Cognito forms bundle (instead of just a screenshot).

This way, we can amend it on our end and provide the exact module or value to copy back into your scenario.

To allow others to better assist you, please provide the following:

2. Scenario Blueprint

Providing your scenario will allow others to quickly recreate and see how you have set up the mappings in each module, and also allows us take screenshots or provide module exports of any solutions we have for you in return. This would benefit you in implementing our suggestions as you can view screenshots of the changes or paste corrected modules back into your scenario.

Need help❓View instructions ◀

A. Export Blueprint

In the scenario editor, click the three dots at the top-right, then select “Export blueprint”.

  • You can upload files and images to this forum by clicking on the upload button:

    Uploading an attachment here will look like this:
    blueprint.json (12.3 KB)

B. Create Public Share Link

In the scenario editor, click on the Share button:

Read more about the scenario sharing feature in the Help Centre.

3. Properly-formatted Output Bundles from a Scenario Run

Please provide the input/output bundles of each of the relevant modules by running the scenario, or get the bundles from a previous run from the Scenario “History” tab.

Providing the bundles from a scenario run will allow others to replicate what is going on, especially if there are complex data structures (nested arrays and collections) and external services. This helps mapping the correct raw variable names from collections (instead of the label/display name).

Need help❓View instructions ◀

Click on the white speech bubbles on the top-right of each module and select “Download output (or input) bundles”.

A. Upload a Text File

Save each bundle contents in a plain text editor as a moduleNumber-output.txt file. Open the file to check if it has not added additional formatting or encoded the plain text.

  • You can upload files and images to this forum by clicking on the upload button:

    Uploading an attachment here will look like this:
    module1-input.txt (1.2 KB)
    module1-output.txt (3.4 KB)

B. Insert a Formatted Code Block

If you are unable to upload files on this forum, alternatively you can paste the bundles AND format them correctly.

:warning: Formatting IS Important! :warning:

Here are some ways to provide text content in a way that it won’t be modified by the forum.

  • Method 1: Type code fence manually
    Manually type three backticks ``` in a separate line before and after the content, like this,

    ```
    text goes here
    ```
    
  • Method 2: Highlight the pasted content, then click the preformatted text/code button

  • Method 3: Upload your file elsewhere and share the public link
    This method is only advised for large files exceeding the forum upload limit.

Sharing these details will make it easier for others to assist you, so that you can get better answers, faster.

@samliew

Eureka! :star_struck: Finally it works. No more looong formulas! Thank you guys.

Thank you Sam, will do next time.

While we are at this, a situation related to this one (and I promise this is my last question, than I am back to learning):

Is there any chance this could be applied to filter as well? Something like this? (But this exact example doesn’t work):

Yes, just remove the if(). The contains() function will return a boolean value of true or false, so change the filter from text equal to to boolean equal to.

1 Like

Hell Yeah!! Works. :smiling_face_with_sunglasses: You’re a gem, Stoyan.

1 Like