How to break out of a loop if you find one of the values in your database

It took me a while to figure this out, so I’m posting the solution here for anyone searching in the future:

In my app, a user sends an invitation to connect to one of their friends in their phone contacts.

– Friends who are already on the app get a regular invitation; friends who aren’t already on the app get a different kind of invitation.

– Each device contact might have multiple phone numbers (Ex: work phone, cell phone, home phone).

I want to loop through the array of phone numbers. If that phone number belongs to an existing user, I break the loop and send the regular invite. If none of the phone numbers belong to an existing user, I send the other kind of invitation at the end.

I know my users might have multiple emails and phone numbers, so I have a table called user_contact_methods with these fields: id, created_at, user_id, contact_method (examples: phone, email), contact_value (examples: 1-555-555-5555,

My input is an array of phone numbers with an id #, a label (home, cell, etc), and the number: {“id”:123,“label”:“home”,“number”:“1-555-555-5555”}.

I am looking for records where user_contact_method.contact_value are the same as my phone input: phone_number.number

If it finds a record, it returns it as user_phone_1.


(Edited: The original explanation had an error. I fixed it so the above explanation should be correct.)

Note: At first I was trying to use “Precondition” instead of “Conditional”. The difference is that if it doesn’t pass the precondition, the whole query gets stopped. But if it’s a conditional, you can tell it what to do based on whether or not it passes.


I wrote up this little tutorial then immediately realized that it’s not quite correct. This gives me the desired result if the invited user HAS a record in the database. But if the invited user DOES NOT yet have a record in the database, it’s telling me it can’t do the conditional step because it’s not getting a record to compare against:

How else can I express the conditional “if no records are returned…”?

Heads up: I found where I can change the output of the query to state whether or not the record exists:

However, if that record does exist, I need the info from it. I’m trying to figure out how to GET that one record, but I’m not seeing an obvious way to tell the query which phone number from the array was the one that broke the loop.

So it might still be easier if I could judge whether the record exists by whether the returned record is empty/null.

Hi @ErinHWagner

Correct me if I’m wrong, but in your above screenshots it doesn’t appear like you utilized the “Else” parameter of the Conditional statement. In the “Else” is where you can build logic to do something if the “If” statement is not met.

Please, let me know if I understood the question correctly and this is helpful!

@michael Good point. I assumed that if ELSE wasn’t set, that the condition would allow the loops to continue as though the condition weren’t there. I did some experimenting and it appears that is indeed the case.

However, your comment made me think of something else and I believe I have my solution now :slight_smile: I was putting the “is empty” filter onto the wrong part of the query. I’ll correct the instructions in my original post. Thanks!