Using session variable for select field - can‘t clear variable

Product: PowerShell Universal
Version: 3.8.8

Hi!
I have to the following dashboard. Whenever I select a radio button, it updates the choices in the select field. Unfortunately, it try’s to the same one multiple times as I receive the following error when selecting a radio button:

Also, the select box always just shows one option, even though there should be 2 at the moment. It shows that it adds 2 options, but I can only select one option.

Any ideas?


$Session:ClientOptions = @()

New-UDBackdrop -Id 'backdrop' -Content {
                New-UDTypography -Text "Loading..." -Variant h2
}

New-UDCard -Title 'Send mail' -Content {
    New-UDForm -Content {
        New-UDRadioGroup -Id "message_category" -Label "Message category" -Content {
            New-UDRow -Columns {
                New-UDColumn -LargeSize 1 -Content {
                    New-UDRadio -Label 'All users' -Value 'All users'
                }
                New-UDColumn -LargeSize 1 -Content {
                    New-UDRadio -Label 'Security' -Value 'Security'
                }
                New-UDColumn -LargeSize 1 -Content {
                    New-UDRadio -Label 'Management' -Value 'Management'
                }
            }
        } -OnChange {
            
            Set-UDElement -Id 'backdrop' -Properties @{
                    open = $true
            }
            $halo_clients = Invoke-PSUScript -Script HaloHelpers/GetHaloCustomersWithMailContacts.ps1 -Wait

            if ($halo_clients.count -lt 10) {
                Show-UDToast -Message "Halo API returned an error" -Duration 3000
            }

            $message_category = (Get-UDElement -Id 'message_category').value
            if ($message_category -eq "All users") {
                $halo_clients = $halo_clients | Where-Object -FilterScript { $_.Mailcontactallusers -ne $null }
            }
            if ($message_category -eq 'Security') {
                $halo_clients = $halo_clients | Where-Object -FilterScript { $_.Mailcontactsecurity -ne $null }
            }
            if ($message_category -eq "Management") {
                $halo_clients = $halo_clients | Where-Object -FilterScript { $_.Mailcontactmanagement -ne $null }
            }
            Show-UDToast -Message "Filtered all clients. Remaining: $($halo_clients.count)" -Duration 3000 #Shows 2

            $Session:ClientOptions = $null #Trying to clear things up
            $Session:ClientOptions = @() #Trying to clear things up
            foreach($halo_client in $halo_clients) { #$halo_clients.count usually = 2
                $Session:ClientOptions += New-UDSelectOption -Name $halo_client.name -Value $halo_client.id
                Show-UDToast -Message "Added $($halo_client.name)" -Duration 3000 #appears 2 times, with individual names
            }
            Show-UDToast -Message "Options: $($Session:ClientOptions.count)" -Duration 5000 #shows 4! 
            Sync-UDElement -Id 'dySelect'
            Set-UDElement -Id 'backdrop' -Properties @{
                    open = $false
            }
        }

        New-UDDynamic -Id 'dySelect' -Content {
            New-UDSelect -Id 'select_clients' -Label 'Select Clients' -Option { $Session:ClientOptions }
        }
        New-UDCheckbox -Id 'chkSimulate' -Label 'Simulate (Do not actually send)' -Checked $true
    } -OnSubmit {
        Show-UDModal -Content {
            New-UDTypography -Text "Submitted"
        } -Footer {
            New-UDButton -Text "Close" -OnClick { Hide-UDModal }
        } -Persistent
    }

}

Solved it by doing it this way:


            $options = @()
            foreach($halo_client in $halo_clients) {
                $options += New-UDSelectOption -Name $halo_client.name -Value $halo_client.id
                Show-UDToast -Message "Added $($halo_client.name)" -Duration 3000
            }
            $Session:ClientOptions = $options
            Show-UDToast -Message "Options: $($Session:ClientOptions.count)" -Duration 5000
            Sync-UDElement -Id 'dySelect'
            Set-UDElement -Id 'backdrop' -Properties @{
                    open = $false
            }

@2clickk you can alos use switches, just a tip for your code :slight_smile:

            $message_category = (Get-UDElement -Id 'message_category').value
            if ($message_category -eq "All users") {
                $halo_clients = $halo_clients | Where-Object -FilterScript { $_.Mailcontactallusers -ne $null }
            }
            if ($message_category -eq 'Security') {
                $halo_clients = $halo_clients | Where-Object -FilterScript { $_.Mailcontactsecurity -ne $null }
            }
            if ($message_category -eq "Management") {
                $halo_clients = $halo_clients | Where-Object -FilterScript { $_.Mailcontactmanagement -ne $null }
            }

Looks much nicer like this

            $message_category = (Get-UDElement -Id 'message_category').value

switch ($message_category) {
 "All users" {
$halo_clients = $halo_clients | Where-Object -FilterScript { $_.Mailcontactallusers -ne $null }
}
"Security {
 $halo_clients = $halo_clients | Where-Object -FilterScript { $_.Mailcontactsecurity -ne $null }
}
"Management" {
$halo_clients = $halo_clients | Where-Object -FilterScript { $_.Mailcontactmanagement -ne 
}
}

But you can also just do the utput like the last thing in your $halo_clients then just write the line something like this.

$message_category = (Get-UDElement -Id 'message_category').value
if ($null -ne $message_category) {
$halo_clients = $halo_clients | Where-Object -FilterScript { $_.Mailcontact$($message_category) -ne $null }
}

But then you need to change the value from “all Users” to “allusers” for the online above would work

1 Like