public/Update-SfStatePicklistValue.ps1
|
function Update-SfStatePicklistValue { <# .SYNOPSIS Updates the State picklist entry via direct web access to the Salesforce Classic Setup UI. .DESCRIPTION The given CountryCode and the given StateCode must exist. The command updates the picklist label (i.e. the state name) and the integration value. Active flag and visible flag are intentionally left unchanged: These settings are handled by Build-SfAddressSettingsMetadata and a subsequent 'sf project deploy start'. .LINK https://codeberg.org/uwe-braig/sf-metadata-toolkit #> [CmdletBinding()] param ( # Web session (including the Session ID cookie) as created by New-SfWebSession. [Parameter(Mandatory, Position = 0)] [PSCustomObject] $WebSession, # Country ISO code of the country the state entry belongs to. [Parameter(Mandatory, Position = 1)] [string] $CountryCode, # State ISO code of the state entry to be updated. [Parameter(Mandatory, Position = 2)] [string] $StateCode, # Picklist label (i.e. country name) to be set. [Parameter(Mandatory)] [string] $Label, # Integration value to be set. [Parameter(Mandatory)] [string] $IntegrationValue ) $ScriptName = 'Update-SfStatePicklistValue' $ErrorActionPreference = 'Stop' [bool] $IsDebug = $DebugPreference -ne 'SilentlyContinue' [bool] $IsVerbose = $VerbosePreference -ne 'SilentlyContinue' # --- get the form from the Salesforce Classic Setup UI $SetupUiUrl = $WebSession.InstanceUrl + "/i18n/ConfigureState.apexp?countryIso=$CountryCode&stateIso=$StateCode" $HttpGetResult = Invoke-WebRequest -Method Get -WebSession $WebSession.WebSession -Uri $SetupUiUrl if ($HttpGetResult.StatusCode -eq 200) { Write-Verbose "$ScriptName|HTTP GET|StatusCode=$($HttpGetResult.StatusCode)|StatusDescription=$($HttpGetResult.StatusDescription)" } else { throw "$ScriptName|HTTP Get Error|StatusCode=$($HttpGetResult.StatusCode)|StatusDescription=$($HttpGetResult.StatusDescription)" } # --- prepare body and copy Visualforce view state $PostBody = @{} $ViewStateInputFields = $HttpGetResult.InputFields | Select-Object Id, Name, Type, Value | Where-Object { $_.Id -like 'com.salesforce.visualforce.ViewState*' } foreach ($item in $ViewStateInputFields) { $PostBody.Add($item.Id, $item.Value) } Write-Debug "$ScriptName|View State Input Fiels|BEGIN" Write-Debug ($ViewStateInputFields | Format-Table | Out-String) Write-Debug "$ScriptName|View State Input Fiels|END" # --- prepare payload form fields $PayloadInputFields = $HttpGetResult.InputFields | Select-Object Id, Name, Type, Disabled, Checked, Value | Where-Object { $_.Id -like 'configurecountry:form*' } foreach ($item in $PayloadInputFields) { switch -Wildcard ($item.Id) { '*editName' { $NameField = $item } '*editIntVal' { $IntegrationValueField = $item } '*editActive' { $ActiveField = $item } '*editVisible' { $VisibleField = $item } '*saveButtonTop' { $SaveButton = $item } } } # ---- add our actual payload data $PostBody.Add('configurecountry:form', 'configurecountry:form') $PostBody.Add($SaveButton.Id, $SaveButton.Value) $PostBody.Add($NameField.Id, $Label) $PostBody.Add($IntegrationValueField.Id, $IntegrationValue) # leave Active checkbox 'as is' if ($ActiveField.Checked -eq 'checked') { $PostBody.Add($ActiveField.Id, 'true') } else { $PostBody.Add($ActiveField.Id, 'false') } # leave Visible checkbox 'as is' if ($VisibleField.Checked -eq 'checked') { $PostBody.Add($VisibleField.Id, 'true') } else { $PostBody.Add($VisibleField.Id, 'false') } Write-Debug "$ScriptName|Post Form|BEGIN" Write-Debug ($PostBody.GetEnumerator() | Sort-Object -Property Name | Format-Table | Out-String) Write-Debug "$ScriptName|Post Form|END" # ---- now post it $HttpPostResult = Invoke-WebRequest -Method Post -WebSession $WebSession.WebSession -Uri $SetupUiUrl -Body $PostBody if ($HttpPostResult.StatusCode -eq 200) { if ($HttpPostResult.Content.Contains('<h4>Errors</h4>')) { throw "$ScriptName|HTTP POST OK BUT WITH ERRORS|Content=$($HttpPostResult.Content)" } else { Write-Verbose "$ScriptName|HTTP POST OK|StatusCode=$($HttpPostResult.StatusCode)|StatusDescription=$($HttpPostResult.StatusDescription)" } } else { throw "$ScriptName|HTTP POST NOT OK|StatusCode=$($HttpPostResult.StatusCode)|StatusDescription=$($HttpPostResult.StatusDescription)" } } |