How can I repeat a custom control x number of times, based on previous user input?

Hey everyone,

I’m trying to create a dynamic list of a custom control that will have x number of blank entries presented on the page based on a number the journey user gave me on a previous page. Here’s the real world example:

I’m trying to get someone to RSVP to an event and they’re first going to confirm they’re going, then if they are, they’ll tell me how many guests are coming with them. Then, I have to ask them all what dining menu selections they want for each person. I’ve been following this link on CCs/Dynamic Lists but I’m stuck on how I set the initial set of CC objects based on the # of guests the user told me.

I have my Guest input CC set up:

Then I have a list summary CC, but I’m a little confused on how I’m telling the list summary the number of items that are needed in that list, especially if I’m starting with just a number (the number of guests the user told me) and I need to parse that into a list of indexes.

Any thoughts here?

1 Like

Hey @davidpairkit !

Let’s give your (list) use case a closer look. Things we know:

  • You have a List of < Guest > custom control
  • You are collecting the number of guests on the previous view/web page
  • Once the List is loaded it should be pre-populated with ‘x’ number of empty Guest obj (so that users fill in the info for each Guest)

Setting up your (fixed) list

Assumptions:

  • I’ll be using guest_list to refer to the List of < Guest > you are using as the List Custom Control’s input (data binding)
  • I’ll be using number_of_guests to refer to the number of guests the user entered in the previous step
  • A Guest obj looks like:
{
  "guestName":<String>,
  "vegan":<bool>,
  "allergies":<bool>,
  "firstCourse":<String>,
  "mainCourse":<String>,
  "dessert":<String>
}

Setting up the List of < Guest >

What you’ll want to do is:

  1. Have a Set Variable action on: your web page updated event / on the web control collecting the number of guests’ onChange event
  2. The variable you want to set is guest_list
  3. The value you want to set is: one empty Guest per number_of_guests. Here’s what the value will look like:
FROM guest IN number_of_guests SELECT {
  "guestName":NULL,
  "vegan":NULL,
  "allergies":NULL,
  "firstCourse":NULL,
  "mainCourse":NULL,
  "dessert":NULL
}

This Airscript expression will output a list of empty Guest objs, and LENGTH(guest_list) = number_of_guests .

Let me know if this helps, cheers !

1 Like

Thanks @juanchax !!!

I still am a bit confused on the last part - I updated the variable names to match my object, but where I’m setting variable guest_list to

FROM
guest
IN
number_of_guests
SELECT
{
“GroupID”: NULL,
“Name”: NULL,
“RSVP”: NULL,
“FirstCourseSelection”: NULL,
“EntreeSelection”: NULL,
“DessertSelection”: NULL,
“Vegan”: NULL,
“HaveDietaryNeeds”: NULL,
“DietaryNeeds”: NULL,
“Email”: NULL,
“Mobile”: NULL
}

it keeps resolving to null, even when testing in Connection Builder with sample data that I’m plugging in. I’m not quite sure I understand the query here and what it’s doing, especially with “number_of_guests” being of type number. Can you elaborate on the query?

Oh yeah, I see the bit I missed including in my first reply (my bad!). Adding that here :upside_down_face:


I’ve broken this down into the tiny steps so that it’s easier to follow exactly what I’m doing in each, feel free to scroll to the bottom for the final expression (can be used in a UDF’s body)

Transforming the number_of_guests integer into a list of things with LENGTH(list) = number_of_guests

  1. First we’ll need to grab that integer and cast it as a string: FORMAT_NUMBER(number_of_guests)

  2. Take that string and transform it into anything lengthy (this can be done in a number of ways, this is just my quick hack): MD5(FORMAT_NUMBER(number_of_guests))

  3. Now that we have a long string, let’s get a substring of length=number_of_guests: SUBSTRING(MD5(FORMAT_NUMBER(number_of_guests)),0, number_of_guests)

  4. Great! We are ready to take that substring and turn it into a list we can use for creating your empty guest list: SPLIT(SUBSTRING(MD5(FORMAT_NUMBER(number_of_guests)),0, number_of_guests),"")
    Using number_of_guests=5 as an example, the output of this bit will be:

    [
      "e",
      "4",
      "d",
      "a",
      "3"
    ]
    
  5. Finally, use the expression above inside that SELECT clause :

    FROM 
      guest 
    IN 
      SPLIT(SUBSTRING(MD5(FORMAT_NUMBER(number_of_guests)),0, number_of_guests),"") 
    SELECT 
      { 
        "guestName":NULL, 
        "vegan":NULL,
        "allergies":NULL, 
        "firstCourse":NULL, 
        "mainCourse":NULL, 
        "dessert":NULL
       }
    

Now the solution might be complete. Just test it out and lmk if it works as you need @davidpairkit !

3 Likes

Exactly what was needed here, thanks so much @juanchax !

Could you use the RANGE function in the IN clause to get the list you need?

RANGE(1, number_of_guests)

Results in [1, 2, …] that you iterate over to build your blank objects.

3 Likes

Hey @kyung - that did the trick, too! Thanks for sharing!