My Problem
I had a problem today when I worked on a Power Apps Form for applying for Training, Accommodation, and or Travel (any or all of these).
There was a Purpose field in the form which the user can enter one to many possible options (Training, Accommodation, Travel).
When the form saves the data into a SharePoint list, a Power Automate process is started. In that process I need to set Boolean variables for each chosen option (to pass to the next step).
It was in determining each of those values where I came across the problem. I thought it would be as easy as using a Contains expression, but not so, why’s that?
In my scenario the column was a Choice with Checkboxes (allow multiple selections).
Why doesn’t a Contains Expression work on a multi-valued field?
The reason a Contains expression doesn’t work, is because, even though the field is an array, it is an array of objects, rather than an array of values. Because of this you need to iterate the array first, then get the objects’ value.
Here is the json representation of the field which shows it as an array of objects…
"PurposeOfForm": [{
"@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
"Id": 0,
"Value": "Travel"
}, {
"@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
"Id": 1,
"Value": "Accommodation"
}, {
"@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
"Id": 4,
"Value": "Training"
}
],
The Solution
When working out the solution, I came up with two options. For my particular scenario I choose solution 1, but solution 2 is also a good solution so I thought I’d record it.
Edit 2021-09-16
I actually ended up using Solution 2, as even though a switch would be the preferred control for most programmers, I found that I needed get the values from the options, so now I say that my preferred solution is solution 2 because is it more versatile, that is, I can set my variables and output all the values (join(variables('purposeValueArray'), ' | ')
)
Solution 1: Simple iteration with a Switch
For this I use the supplied iterator action and a Switch action to set the variables.
In case you don’t know what a switch is, it essentially looks at a variable and provides conditional outcomes (or cases) based the variables value.
My situation in code would look something like this…
switch (MyValue)
case: "Accommodation"
// if the value in the variable MyValue = Accommodation then do this...
isAccommodation = true;
case: "Training"
// if the value in the variable MyValue = Training then do this...
isTraining = true;
default:
// if nothing else do this...
isOther = true;
// illustration only - not valid JavaScript
But we are not using code, so here is how I did it.
Step 1 – Initialize the variables
I’ve created my three Boolean variables at the start of my process, all set to false
Step 2 – Add a Switch action
Add a Switch action
Set the value ‘On’ field to a value called [field name] Value (or ‘Purpose of Form Value’ in my case)
Once you set the on, the Switch action will embeded in an ‘Apply to each’ action (see below).
Step 3 – Set up your cases
Inside your Switch action set the Case value, then add an action to set the variable (or whatever you want to do)
Add a Case for each expected value in the multivalued field.
Solution 2: Create an array of values
This second solution quite simply creates an array of values so that you can use the Contains expression (as I wanted to at the start).
Step 1 – Initialize the variables
In addition to the variables in solution 1 above, I have added two more, first is of type array and the second is of type object.
Step 2 – Iterate the object array
Add an ‘Apply to each’ action setting the array to be the multivalued field.
Then set the object variable to be the Current item, and add the value from the object to an array.
The expression for getting the objects value is…
variables('purposeObject')['Value']
Note, you don’t need to use the object as I have done above, if you only want the value string then you can just…
- add an Append to Array action
- set the value field to the object value (in my case ‘Purpose of Form Value’)
This wraps the Append action in the Apply to each action
Step 3 – Check the value array for your value
In whatever action you like, use the Contains expression to see if the value is in the array, like so
contains(variables('purposeValueArray'), 'Accommodation')
One thought on “Working with SharePoint List Multi Value Fields in Power Automate”
An intrigսing discussіon is definitely worth сomment.
There’s no doubt that that you should publish more aboᥙt this topic,
it may not Ьe a taboo matter but typically people do not
discuss such subjects. To the next! Ꭺll the best!!