Blog
SharePoint - How to programmatically set and clear single select choice items in a combo box on a form
There is often the requirement to programmatically set and clear combo box values with formula, and the syntax to do this may not be entirely clear. This post walks through a use case scenario of how to set and clear combo box values depending on the value of another control.
As an example, let's take the example of a form that's based on a SharePoint list of issues. This form includes two pertinent fields - 'close datetime', and issue status. The functionality that we'll build will configure the form so that when a user enters a close datetime, the form will set the issue status to 'closed'. If the user clears a 'close datetime' value that has been set, the form will clear the issue status value. The structure of the SharePoint list looks like this:
IssueStatus is a single-select choice column and the acceptable values are: - In Progress, Open, and Closed.
Setting and Clearing a Combobox item
To build this functionality, the technique we'll use sets the value of a variable when the 'close datetime' value changes. The DefaultSelectedItems property of the IssueStatus comboboxrefers to this variable to determine what to show in the dropdown.
Taking an auto-generated app that's based on our SharePoint list, we open the edit form and set the OnChange property of the date picker control (DateValue2) to the following formula.
If(IsBlank(DateValue2.SelectedDate),
UpdateContext({locNewStatus:"null"}),
UpdateContext({locNewStatus:"Closed"})
)
This formula specifies that if the value of the DateValue2 picker is blank, we set the value of a variable called locNewStatus to the literal string value "null". If the value is not blank, we set it to "Closed".
Next, we set the DefaultSelectedItems property of the Issue Status combo box to the following formula:
If(locNewStatus="",
Parent.Default,
If(locNewStatus="null",
{},
LookUp(Choices(Issue.IssueStatus),Value=locNewStatus)
)
)
This formula specifies that if locNewStatus is equal to an empty string (that is, the user hasn't amended the close datetime value), we set the DefaultSelectedItems of the combobox to Parent.Default in order to load the existing value, if the form is in edit mode.
If locNewStatus equals "null", we set DefaultSelectedItems to an empty record in order to clear the contents of the combobox. Otherwise, we lookup the choice value that matches the value of the variable - "Closed" in this example.
For the technique to work, it's important to initialise the value of locNewStatus to an empty string when the form loads, and we can do this by applying the following formula to the OnVisible property of the screen.
UpdateContext({locNewStatus:""})There is a final key step that is needed to clear an existing choice field value. When a user clears the 'close datetime' field and the issue status combobox displays an empty record, this alone isn't enough to remove the existing choice value when the user attempts to save the record through a call to SubmitForm. To clear a choice value, it's necessary to set the Id of the choice value to -1 by amending the formula in the Update property of the data card that contains the IssueStatus combobox using the formula beneath (DataCardValue14 is the name of the combobox control):
If(locNewStatus="null",
{
'@odata.type':"#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference",
Id:-1,
Value:""
},
DataCardValue14.Selected
)
Conclusion
- Categories:
- forms
- Forms - How to set the value of a field to todays date for new records only
- Apps - How to create an app from a hand drawn image
- Forms - How to highlight user modified field values on a form
- Forms - How to append text to field in a data source
- Forms - How to calculate values (eg sums and products) and store the results in SharePoint or other datasource
- SharePoint - How to clear datetime fields/set an empty datetime value on a form
- Forms - How to convert a display form to an edit form
- Forms - How to copy/save an existing record on a form as a new record
- Forms - How to show Office 365 user profile details on a form
- Forms - How to hide fields that are blank, or have not been completed
- Forms - How to select-all / unselect-all checkbox or toggle controls on a form
- Forms - How to set the data source of a form to a collection
- Forms - The best practice for setting the data item on a form
- Controls - How to set default control and form values
- Forms - How to conditionally make form fields mandatory