New-UDApp -Title 'SyncVMwareVMtoSharePoint' -Pages @(
    New-UDPage -Name "Home" -Content {
        New-UDForm -Content {
            New-UDTypography -Text 'Enter the information below (all fields required) and click the Submit button'
            New-UDRow -Columns {
                New-UDColumn -SmallSize 6 -LargeSize 6 -Content {
                    New-UDTextbox -Label 'RequestedForEmail' -Id RequestedForEmail
                    New-UDTextbox -Label 'ADDomain' -Id ADDomain
                    New-UDTextbox -Label 'SamAccountName' -Id SamAccountName
                    New-UDTextbox -Label 'Description' -Id Description
                    New-UDTextbox -Label 'PasswordstateList' -Id PasswordstateList
                    New-UDTextbox -Label 'PasswordstateTitle' -Id PasswordstateTitle
                }
            }          
        } -OnValidate {
            $FormContent = $EventData

            if ($EventData.RequestedForEmail -eq $null -or $EventData.RequestedForEmail -eq '' -or $EventData.ADDomain -eq $null -or $EventData.ADDomain -eq '' -or $EventData.SamAccountName -eq $null -or $EventData.SamAccountName -eq '' -or $EventData.Description -eq $null -or $EventData.Description -eq '' -or $EventData.PasswordstateList -eq $null -or $EventData.PasswordstateList -eq '' -or $EventData.PasswordstateTitle -eq $null -or $EventData.PasswordstateTitle -eq '') {
                # ('Office365', 'VMware_Systems', 'CSRC', 'Shared Linux Password List', 'Peoplesoft Share PW', 'Cohesity', 'VDI')
                New-UDFormValidationResult -ValidationError "All fields are required."
            }
            else { 
                if ($FormContent.ADDomain -ne 'nd.gov') {
                    New-UDFormValidationResult -ValidationError "Only nd.gov ADDomain is supported at this time."
                }
                else {
                    If (@('Office365', 'VMware_Systems', 'CSRC', 'Shared Linux Password List', 'Peoplesoft Share PW', 'Cohesity', 'VDI') -notcontains $EventData.PasswordstateList) {
                        New-UDFormValidationResult -ValidationError "PasswordstateList must match one of the following:  VMware_Systems, Cohesity, VDI, Office365, CSRC, Shared Linux Password List, Peoplesoft Share PW"
                    }
                }
                New-UDFormValidationResult -Valid
            }
        } -OnSubmit {            
            $InvokePSUScriptParams = @{
                Script             = 'New-ITDADServiceAccountRitm_script.ps1';
                RequestedForEmail  = $EventData.RequestedForEmail
                ADDomain           = $EventData.ADDomain
                SamAccountName     = $EventData.SamAccountName
                Description        = $EventData.Description
                PasswordstateList  = $EventData.PasswordstateList
                PasswordstateTitle = $EventData.PasswordstateTitle

            }
            $InvokePSUScriptResult = Invoke-PSUScript @InvokePSUScriptParams -Wait
            Show-UDToast -Message ("ServiceNow Request " + $InvokePSUScriptResult.number + " created on behalf of $User" ) -Duration 100000

        }
    }
)