top of page
  • Writer's pictureEd G.

Birthday Bot - Part Three - Formatting Options and Do Until in Power Automate

Updated: Mar 22, 2020

So far, we've worked through the notification process in two different iterations, and how to set up the chatbot interface. We ended the last post by grabbing a list of contacts with upcoming birthdays, and the next few steps will process each of those by choosing a contact method.

 

That list will be an array, so we can use our recently learned trick of using length() to determine if there were any unresolved birthdays or not. If there are no birthdays, I send a response to the user stating that and end the flow. If there are birthdays, I then work through each one to parse the available contact methods and format them to present as options to the user.

Skipping ahead a bit, the AtBot action, Get Choice List Response From User, will present the options with numbers...like this:

  1. firstname.lastname@email.com

  2. 123 Important Work Avenue, Anytown, CA 93843

  3. 453 Homestreet Way, Hometown, MD 10232

  4. (949) 555-1212

The thing that lets AtBot know that there is a new option is the carriage return, but if I don't plan for an empty value and only store the respective values with a line break after, I could end up with an empty choice:


  1. 123 Important Work Avenue, Anytown, CA 93843

  2. 453 Homestreet Way, Hometown, MD 10232

  3. (949) 555-1212

This is a less-than-ideal user experience, so I did a couple of things in the next steps: first, if one of the options was blank, then I switched the value to an empty string (two single quotes with nothing in between). If there was something there, I stored that in a Compose with the Carriage Return variable appended at the end so that when I string everything together, any actual values would include the varCR, and any empty values would just skip to the next.

One thing I also found out was that the 'emailAddresses' from the List Contacts in our Notification flow will actually yield an array. So, in my expression, I checked for how many email addresses there are (again, using Length). If there is more than one, it will present "Multiple Choices" so the user can select which one they want to send to later. If there is only one, then it will just present that email address.

While the expression may look long and scary, it really isn't much more complex than something you might do in Excel.

As a side note, the new expression editor (currently in preview) allows for more room to see what is going on. I'm really liking that so far and encourage you to give it a try.

 

Setting the remaining aspects will use a similar technique. With the addresses, I used concat() to string the various elements together before checking for a 'blank'. This could open up a problem if I was missing an element like City or Zip, but since the whole thing is presented in the choice, the user will have the option to select the best contact method.


 

Here is the expression I used for the Business Address. Note that I am also putting the method, with a ' > ' before the actual value ("Mobile > (949) 555-1212"). This is so we can use a Switch later to choose the contact path. More on that later.

Again, it looks scarier than it really is. Once you find the pattern in these expressions, they become a bit more manageable.

At the end of all of these, I use another Compose, called Options, to bring everything together before adding it to the AtBot action. This also allows me to add a final option to cancel/skip to the next birthday.

 

Regardless of the method chosen, we will need a cool birthday quote to send along with our happy wishes. For this, I created a SharePoint list with about 50 random birthday wishes found on helpful websites everywhere...thank you, internet!

This loop is called a "Do Until" loop which will keep repeating until a certain criterion is met. In this case, I am checking if the Quote ID variable is greater than zero because I know that an approved quote will set that to the accepted quote's ID. If the quote is not approved, it stays at zero and loops through another random quote.

Inside the loop, we'll use the SharePoint "Get Item" with rand(1,51) as the expression inside the ID field. This will pick a number between 1 and 50, which correlates to the 50 quotes I have in my SharePoint list. This is also a handy expression to use when selecting a random element from an array.

It should be noted that this loop could be put just about anywhere depending on how you design your user's journey. Totally up to you.

 

Whatever method is chosen above will be passed along as the value. Meaning, if the user opts for option 4, then the value that is passed as I wrote it is: "Mobile > (949) 555-1212". I did this because the data will change from person to person, so I needed something consistent to build into the Switch action, but I also needed a special character (that didn't look too out-of-place) to parse the label from the data. Inside the switch criteria field, I used a split() function to separate the chosen value into an array with two elements: the consistent label, and the dynamic value.

Closeup of the Switch used.

By grabbing the first element of that array, I can use those labels ("eMail", "Business", "Home", or "Mobile") in my Switch and choose a different path for each.



 

Next week, we will start digging into the actual send actions and the stuff that started this whole journey in the first place. Also, pretty excited today that my first podcast interview was published by Mark Smith. Click on the pic below to give it a listen.

Get in, make mistakes, learn some things, and have fun! Try. Fail. Learn. Repeat.

149 views0 comments
bottom of page