In my previous post I demonstrated how to use a Flic button with CDS to create a Fire Evacuation record and notify Users that an office is being evacuated. The eagle-eyed amongst you may have noticed that the Office option set field value was hard-coded, meaning no matter where you were when you triggered the Flic button it would create an evacuation record against the specifically named office, so you’d have to have a Flic button for each office in your organisation, which might be a challenge if you regularly visit multiple offices in your organisation.

In this blog I’m going to show you how we can use the location data from the Flic buttons and dynamically set the option set field for the office location.

The Scenario

In this scenario, my organisation has 4 offices spread throughout the UK:

  • Glasgow
  • Edinburgh
  • Birmingham
  • London

I may be required to visit any of these offices throughout the course of my working week, and to reduce confusion for me I’d like to ensure I only have to carry one Flic button that I can use to trigger the process to record an Evacuation and to notify others within my organisation, and have the Flow recognise which Office I am in and ensure the record is populated accordingly. Furthermore, if I am too far from any of the offices when I click the button, I’d like the Flow to notify me of this if I accidentally try to trigger the process to record the evacuations.

The Setup

For this flow we’ll be using the same Evacuation entity I configured in the previous blog. I’m also going to be using a Bing Maps – Get Route action, so you need to ensure you’ve signed up for a Bing Maps API key. The final preparation step is to get the Office Option Set values (as highlighted below) for use in the Flow.

The Solution

This Flow is a bit longer than the previous one, so I’ve included an image below of the whole flow, and I’ll go through each step in turn to explain what I’m using them for.

1. Trigger – Same as in the previous blog, this is triggered using a “Click” event from a Flic button

2. Compose – as per the previous blog, this is an expression to take the DateTime value from the Click Time value and format it to make it more “friendly”, using the expression below

@{formatDateTime(triggerBody()?['clicked_at'], 'dd MMMM yyyy')}

3. Initialize Offices Array – this is just an Array variable we’re going to use later in the Flow

4. Compose Office Info – In this Compose action I’m inputting an array that includes the Office location, the Office address, and the Option Set Value that corresponds to the office

5. Parse Office Info – I’m using a Parse JSON action to parse the array I created in the previous step, using the following schema

{
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "Office": {
                "type": "string"
            },
            "Address": {
                "type": "string"
            },
            "OptionSetValue": {
                "type": "integer"
            }
        },
        "required": [
            "Office",
            "Address",
            "OptionSetValue"
        ]
    }
}

6. For Each Office loop – For each office from the array we created in Step 4, we carry out the following actions

6A. Get Distance to Office – For this action we’re using the Bing Maps Get Route action to calculate the distance from where we were when we clicked the Flic button (using the Latitude and Longitude that are included as parameters from the Flic trigger) to the Office (using the address we put into the array in step 4)

6B. Compose Office Distance – for this step we’re going to take the Distance output from Step 6A, and the Office name and Option Set value that we parsed from the array we created in Step 4 and put them into a new array element

6C. Append to Offices Array – we take the output from the Compose Office Distance step above and append it to the Offices Array we created in Step3

7. Initialize SuccessfulActions – we initialize a new integer variable called SuccessfulActions that we’ll use in Step 9 and Step 10 below, ensuring the default value is set to 0

8. Parse Offices – next we use another Parse JSON action to parse the Offices Array variable that we’ve been populating in step 6, using the schema below:

{
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "Office": {
                "type": "string"
            },
            "Distance": {
                "type": "number"
            },
            "OptionSetValue": {
                "type": "integer"
            }
        },
        "required": [
            "Office",
            "OptionSetValue",
            "Distance"
        ]
    }
}

9. For Each Office Distance loop – in this loop we’re going to be checking if the Flic has been triggered within a set distance of any of the offices, and if so we’ll be creating a new evacuation record

9A. Check if the Flic has been triggered close to the Office – we use a Condition action to check if the Distance from the Office is less than a set amount. In my example I’ve set it to check that the distance is less than 1 mile, but you can set accordingly

9B. Create an Evacuation Record – if the condition above is satisfied then we use a CDS Create Record action to create a new Evacuation record, and we set the Title using the Office value from the Offices Array and the formatted DateTime from Step 2. We then set the Office Option Set using the OptionSetValue from the array, and set the Start Time using the Click Time from the Flic Trigger

9B. Increment Successful Actions – We then increment the SuccessfulActions variable we initialised in Step 7 by 1, as this will be used in Step 10 below

10. Check if the Flic has been triggered too far from any office – we use a Condition action to check if the SuccessfulActions variable equal 0. If so, we send a mobile notification to the triggering user to to let them know they’re too far from the office to trigger an evacuation record

Conclusions

This Flow hopefully demonstrates a viable way to use array variables to dynamically set fields on CDS records; I’ve had particular issues with Option Set fields in the past so I’m happy to have found a potential solution to this problem. Ideally I’d be able to retrieve the Option Set values dynamically, but I’m not aware of any way to retrieve them from CDS. If you are reading this and know of a better way please let me know!