Run Dialog: Show/Hide Controls Dynamically

There may be times where input controls on the run dialog for a bot should show or hide dynamically (typically based on the value of another input control). While this isn’t possible directly through the bot’s configuration it is possible through JavaScript bound to one or more parameter controls of the run dialog.

Sample bot params

Let’s assume we have a bot with parameters

param( [string]$Name, [rbsingleselect]$WorkerType, #possible values E or C [string]$EmployeeNumber, [string]$CompanyName, [rbyesno]$IsActive, [int]$Salary )

Control that will cause others to show/hide

The value for $WorkerType can be ‘E' for Employee or 'C’ for Contractor.

Steps to setup dynamic controls

To make controls dynamic there are three general things to do:

  1. Create a function to simplify the showing/hiding of controls.

  2. Add JavaScript to set up the initial state (visible or hidden) of the run dialog controls that will be dynamically shown/hidden.

  3. Set criteria on various controls to show/hide other controls.

Helper show/hide function

To simplify the showing/hiding of fields, utilize a helper function which we will add to the various controls.

function inputVisibility(inputId,isVisible){ //set the visibility of a run dialog control if(document.querySelector('#'+inputId) !== null){ document.querySelector('#'+inputId).parentNode.parentNode.parentNode.hidden = !isVisible; //document.querySelector('#'+inputId).parentNode.parentNode.parentNode.previousSibling.hidden=!isVisible; if(document.querySelector('#'+inputId).type=='radio'){ document.querySelector('#lbl'+inputId.substring(2)).parentNode.hidden=!isVisible; }else{ document.querySelector('#lbl'+inputId).parentNode.hidden=!isVisible; } } }

Dialog Controls - Initial State

To set the run dialog controls' initial visible/hidden state we will add JavaScript to the first control’s On Load event handler.

Click the Script tool on the Name parameter, select the OnLoad event.

Add the inputVisibility function JavaScript as well as lines to set the initial visible/hidden state for any controls that will be dynamically shown/hidden.

In this example those are the EmployeeNumber and CompanyName input controls.

The name of the run dialog input control is the same as the parameter name on the bot and is case sensitive.

function inputVisibility(inputId,isVisible){ //set the visibility of a run dialog control if(document.querySelector('#'+inputId) !== null){ document.querySelector('#'+inputId).parentNode.parentNode.parentNode.hidden = !isVisible; //document.querySelector('#'+inputId).parentNode.parentNode.parentNode.previousSibling.hidden=!isVisible; if(document.querySelector('#'+inputId).type=='radio'){ document.querySelector('#lbl'+inputId.substring(2)).parentNode.hidden=!isVisible; }else{ document.querySelector('#lbl'+inputId).parentNode.hidden=!isVisible; } } } inputVisibility('EmployeeNumber',true); inputVisibility('CompanyName',false); inputVisibility('Salary',false);

Click OK to save the event handler.

Add script to cause show/hide

Add the script to the Change event of the control(s) that will cause other control(s) to be visible or hidden (also adding the helper inputVisibility function).

For the WorkerType parameter → Change event

In this example one controlling control will be the WorkerType control. If the WorkerType is E then show the EmployeeNumber control and hide the CompanyName control, otherwise do the inverse.

To add this script click the JavaScript tool on the WorkerType parameter.

Click OK to save the event handler.

For the IsActive parameter → Change event

[RbYesNo] parameter/control types follow a slightly different control naming pattern. Each radio button has it’s own control id.

  • The “Yes” radio button id is the parameter name prefaced with “r1”

  • The “No” radio button id is the parameter name prefaced with “r2”

To show/hide an RbYesNo type control, you can simply reference one of the radio button ids and the inputVisibility function will handle hiding all.

Eg.
inputVisibility('r1IsActive',false);

In this example another controlling control will be the IsActive control. If IsActive:Yes is checked then show the Salary control, otherwise hide it.

To add this script click the JavaScript tool on the IsActive parameter.

Click OK to save the event handler.

 

Final Behaviour

When the run dialog initially loads the default value of Worker Type is Employee and the Employee Number input control is visible with the Company Name input control hidden.

Changing the Worker Type to Contractor results in the Employee Number input control being hidden and the Company Name input control being shown.

Setting Is Active to Yes shows the Salary input field

 

It is left to the bot developer to have the appropriate logic inside the bot PowerShell to handle the various permutations of some parameters coming in as empty string values.

 

Copy of Example Bot

The below can be copied and pasted into the Bot Studio left nav by right clicking on any folder node and selecting paste.

 

{"Folders":null,"Scripts":[{"ScriptParamValues":null,"ExecutedOn":"1900-01-01T00:00:00Z","LastScriptContentModifiedDate":"2023-11-02T02:56:29.9460724Z","RunStatus":0,"Modules":null,"Content":"","Parameters":null,"ScriptParams":"[{\"Key\":\"$Name\",\"Value\":{\"Name\":\"$Name\",\"Mandatory\":false,\"Position\":0,\"ParameterSetName\":\"__AllParameterSets\",\"ValueFromPipeline\":false,\"ValueFromPipelineByPropertyName\":false,\"ValueFromRemainingArguments\":false,\"HelpMessage\":null,\"DontShow\":false,\"Alias\":null,\"AllowNull\":false,\"AllowEmptyString\":false,\"AllowEmptyCollection\":false,\"ValidateCount\":null,\"ValidateLength\":null,\"ValidatePattern\":null,\"ValidateRange\":null,\"ValidateScript\":null,\"ValidateSet\":null,\"ValidateNotNull\":false,\"ValidateNotNullOrEmpty\":false,\"Type\":\"System.String\",\"TypeName\":\"string\",\"DefaultValue\":null,\"TypeNameForQueryString\":\"string\"}},{\"Key\":\"$WorkerType\",\"Value\":{\"Name\":\"$WorkerType\",\"Mandatory\":false,\"Position\":1,\"ParameterSetName\":\"__AllParameterSets\",\"ValueFromPipeline\":false,\"ValueFromPipelineByPropertyName\":false,\"ValueFromRemainingArguments\":false,\"HelpMessage\":null,\"DontShow\":false,\"Alias\":null,\"AllowNull\":false,\"AllowEmptyString\":false,\"AllowEmptyCollection\":false,\"ValidateCount\":null,\"ValidateLength\":null,\"ValidatePattern\":null,\"ValidateRange\":null,\"ValidateScript\":null,\"ValidateSet\":null,\"ValidateNotNull\":false,\"ValidateNotNullOrEmpty\":false,\"Type\":\"System.String\",\"TypeName\":\"rbsingleselect\",\"DefaultValue\":null,\"TypeNameForQueryString\":\"string\"}},{\"Key\":\"$EmployeeNumber\",\"Value\":{\"Name\":\"$EmployeeNumber\",\"Mandatory\":false,\"Position\":2,\"ParameterSetName\":\"__AllParameterSets\",\"ValueFromPipeline\":false,\"ValueFromPipelineByPropertyName\":false,\"ValueFromRemainingArguments\":false,\"HelpMessage\":null,\"DontShow\":false,\"Alias\":null,\"AllowNull\":false,\"AllowEmptyString\":false,\"AllowEmptyCollection\":false,\"ValidateCount\":null,\"ValidateLength\":null,\"ValidatePattern\":null,\"ValidateRange\":null,\"ValidateScript\":null,\"ValidateSet\":null,\"ValidateNotNull\":false,\"ValidateNotNullOrEmpty\":false,\"Type\":\"System.String\",\"TypeName\":\"string\",\"DefaultValue\":null,\"TypeNameForQueryString\":\"string\"}},{\"Key\":\"$CompanyName\",\"Value\":{\"Name\":\"$CompanyName\",\"Mandatory\":false,\"Position\":3,\"ParameterSetName\":\"__AllParameterSets\",\"ValueFromPipeline\":false,\"ValueFromPipelineByPropertyName\":false,\"ValueFromRemainingArguments\":false,\"HelpMessage\":null,\"DontShow\":false,\"Alias\":null,\"AllowNull\":false,\"AllowEmptyString\":false,\"AllowEmptyCollection\":false,\"ValidateCount\":null,\"ValidateLength\":null,\"ValidatePattern\":null,\"ValidateRange\":null,\"ValidateScript\":null,\"ValidateSet\":null,\"ValidateNotNull\":false,\"ValidateNotNullOrEmpty\":false,\"Type\":\"System.String\",\"TypeName\":\"string\",\"DefaultValue\":null,\"TypeNameForQueryString\":\"string\"}},{\"Key\":\"$IsActive\",\"Value\":{\"Name\":\"$IsActive\",\"Mandatory\":false,\"Position\":4,\"ParameterSetName\":\"__AllParameterSets\",\"ValueFromPipeline\":false,\"ValueFromPipelineByPropertyName\":false,\"ValueFromRemainingArguments\":false,\"HelpMessage\":null,\"DontShow\":false,\"Alias\":null,\"AllowNull\":false,\"AllowEmptyString\":false,\"AllowEmptyCollection\":false,\"ValidateCount\":null,\"ValidateLength\":null,\"ValidatePattern\":null,\"ValidateRange\":null,\"ValidateScript\":null,\"ValidateSet\":null,\"ValidateNotNull\":false,\"ValidateNotNullOrEmpty\":false,\"Type\":\"System.Management.Automation.SwitchParameter\",\"TypeName\":\"rbyesno\",\"DefaultValue\":false,\"TypeNameForQueryString\":\"switch\"}},{\"Key\":\"$Salary\",\"Value\":{\"Name\":\"$Salary\",\"Mandatory\":false,\"Position\":5,\"ParameterSetName\":\"__AllParameterSets\",\"ValueFromPipeline\":false,\"ValueFromPipelineByPropertyName\":false,\"ValueFromRemainingArguments\":false,\"HelpMessage\":null,\"DontShow\":false,\"Alias\":null,\"AllowNull\":false,\"AllowEmptyString\":false,\"AllowEmptyCollection\":false,\"ValidateCount\":null,\"ValidateLength\":null,\"ValidatePattern\":null,\"ValidateRange\":null,\"ValidateScript\":null,\"ValidateSet\":null,\"ValidateNotNull\":false,\"ValidateNotNullOrEmpty\":false,\"Type\":\"System.Int32\",\"TypeName\":\"int\",\"DefaultValue\":0,\"TypeNameForQueryString\":\"int\"}}]","ParameterPromptValues":null,"HasParams":true,"HideDescriptionPanel":false,"VersionNumber":0,"IsVersionDirty":true,"IsChangeMonitoringEnabled":false,"IsSelectiveConnectIdEnabled":false,"ScriptActions":"","CanViewSelfGeneratedRunInstances":false,"RunOnlyAsExplicitScript":false,"IsProvideFullReadiMessage":false,"AllowQuerying":false,"AllowScriptExecutionThroughPowershell":false,"AllowScriptDataAccessThroughRestApi":false,"AllowScriptExecutionThroughRestApi":false,"Alias":null,"IsExecutionRestEndpointDisabled":false,"CanViewScriptRunInstancesTimeline":false,"CanViewScriptLogViews":false,"CanViewScriptExecutionLog":true,"AllowAddDatatableRecordThroughUI":false,"AllowEditDatatableRecordThroughUI":false,"AllowDeleteDatatableRecordsThroughUI":false,"IsDeleteChangeLogs":false,"ScriptExecutionHolder":0,"ChangeLogGroups":null,"ChangeStatus":0,"ChangeCount":0,"IsChangeMonitoringPerformed":false,"ZipContent":"1AAAAB+LCAAAAAAABABdjr0KwkAQhPtA3mHBKxTuFSwkpLBJo2AhKfbCKod7P+xeAvf2phAhmenmg5nJKBiOAG0Dq55axMf3aAYMZH+ZOF0zJiWmqYzmkeRDcq+ZLBxyUvWOCRbkmRR6SALdrq0PmVMlGubgSOyOdilkjHU7WUljGs1VL1PxC53NC1n/3Mf1xw0ZpbbNqW22/gLeaboH1AAAAA==","ZipExecutedContent":null,"ZipParameterPromptValues":"CxAAAB+LCAAAAAAABADtV21v2zYQ/isXbYDsTrDQ7ltSD9i8DAi2GsVidBjWfKCks0WEJjWSsqsG+e89Ui+WYztWh2EfhsCBIx4f3h0f3T2kH4JCq3VhF/jJBpfBe4HMIJBtwzMEm9Mz02yNFjVsmCjRTIIoyDgTajVjheVK0rKfK8nWPIX3DmsIoEv5U2mtko1fMtmcyftKle/QGLZCMr7Nv/9h0VjfxjQg1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":null,"CertificateThumbprint":null,"CertificateName":null,"SignedBy":null,"SignDate":"1900-01-01T00:00:00Z","IsSendAlertOnNoData":false,"ScriptRefreshKey":"","RuleSets":"","ScriptDataType":0,"WorkerCost":"60","UntrackSavingsFlag":false,"ConservedTime":"0.5","WorkerCostCurrency":0,"ROIMetricsCategory":"Uncategorized","ROIMetricsTags":null,"Name":"Dynamic Params","ParentId":"422d1171-53f5-4065-9dc7-951256e52e34","IsScript":true,"CreatedOn":"2023-10-25T20:50:13.7871802Z","UpdatedOn":"2023-12-01T18:34:02.2701661Z","JobScheduleOption":0,"IsShared":false,"IsSharedSolutionNode":false,"SharedSolutionIdentifier":null,"ConnectId":null,"AvailableConnectIds":"","CredentialOption":1,"RetentionOption":3,"RetentionNumber":1,"RetentionUnit":4,"SequenceWithinParent":71,"PermissionOption":0,"JsonDepth":0,"MaxItemsToDequeue":0,"PermittedRunInstances":0,"IgnoreNoDataResults":false,"PartitionKey":"a6d1b007-d7a9-473c-bf54-6e2e8425f557","RowKey":"874eee81-5cef-4a05-a14a-f5d42df119ed","Timestamp":"2023-12-01T18:34:02.3173132+00:00","ETag":"W/\"datetime'2023-12-01T18%3A34%3A02.3173132Z'\""}],"Views":[],"SourceWorkspaceId":"a6d1b007-d7a9-473c-bf54-6e2e8425f557"}