public/Set-SfStateAndCountryPicklistValue.ps1

function Set-SfStateAndCountryPicklistValue {

    <#
        .SYNOPSIS
        Applies the create and update actions from Compare-SfStateAndCountrySettings to the org.

        .DESCRIPTION
        Dispatcher for Add-SfCountryPicklistValue, Add-SfStatePicklistValue, Update-SfCountryPicklistValue
        and Update-SfStatePicklistValue. Each input object is routed by its Action property:

        - CreateCountryViaSetup -> Add-SfCountryPicklistValue
        - CreateStateViaSetup -> Add-SfStatePicklistValue
        - UpdateCountryViaSetup -> Update-SfCountryPicklistValue
        - UpdateStateViaSetup -> Update-SfStatePicklistValue

        Deploy and AddToDataDictionary are known Action values that this dispatcher does not act on -
        Deploy is applied via a regular Metadata API deployment instead, and AddToDataDictionary means
        the entry has to be added to the Data Dictionary manually. Equal means nothing needs to change.
        All three are only reported via Write-Verbose. Any other, unknown Action value is not applied
        either and is reported via Write-Warning, because it is not a value this dispatcher recognizes.

        Input objects are processed in the order received. Compare-SfStateAndCountrySettings sorts by
        _CountryCode, _StateCode, so a country is created before its states. Reordering the input can break
        this, because Add-SfStatePicklistValue requires the country to exist.

        .OUTPUTS
        [PSCustomObject]
        Only with -PassThru: the unchanged input objects, in the order received.

        .EXAMPLE
        Compare-SfStateAndCountrySettings -MetadataModel $MetadataModel |
            Set-SfStateAndCountryPicklistValue -WebSession $WebSession

        .EXAMPLE
        Compare-SfStateAndCountrySettings -MetadataModel $MetadataModel |
            Set-SfStateAndCountryPicklistValue -WebSession $WebSession -PassThru |
            Where-Object { $_.Action -eq 'AddToDataDictionary' }

        .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,

        # Action objects in the format returned by Compare-SfStateAndCountrySettings.
        [Parameter(Mandatory, ValueFromPipeline)]
        [PSCustomObject] $InputObject,

        # Pass the input objects through to the pipeline after they have been processed.
        [Parameter()]
        [switch] $PassThru
    )

    begin {
        $ScriptName = 'Set-SfStateAndCountryPicklistValue'
        $ErrorActionPreference = 'Stop'
        [bool] $IsDebug   = $DebugPreference   -ne 'SilentlyContinue'
        [bool] $IsVerbose = $VerbosePreference -ne 'SilentlyContinue'
    }

    process {
        [string] $Action = $InputObject.Action
        [string] $Target = "CountryCode=$($InputObject._CountryCode)|StateCode=$($InputObject._StateCode)"

        # Parameters shared by all four Add-/Update- functions; state actions add StateCode below.
        $Params = @{
            WebSession       = $WebSession
            CountryCode      = $InputObject._CountryCode
            Label            = $InputObject._Label
            IntegrationValue = $InputObject._IntegrationValue
        }

        switch ($Action) {
            'CreateCountryViaSetup' {
                Write-Verbose "$ScriptName|$Action|$Target"
                Add-SfCountryPicklistValue @Params
            }
            'CreateStateViaSetup' {
                Write-Verbose "$ScriptName|$Action|$Target"
                $Params['StateCode'] = $InputObject._StateCode
                Add-SfStatePicklistValue @Params
            }
            'UpdateCountryViaSetup' {
                Write-Verbose "$ScriptName|$Action|$Target"
                Update-SfCountryPicklistValue @Params
            }
            'UpdateStateViaSetup' {
                Write-Verbose "$ScriptName|$Action|$Target"
                $Params['StateCode'] = $InputObject._StateCode
                Update-SfStatePicklistValue @Params
            }
            'Deploy' {
                Write-Verbose "$ScriptName|$Action for $Target ignored - applied via Metadata API deployment"
            }
            'Equal' {
                Write-Verbose "$ScriptName|$Action for $Target ignored - nothing to change"
            }
            'AddToDataDictionary' {
                Write-Verbose "$ScriptName|$Action for $Target ignored - add to the Data Dictionary manually"
            }
            default {
                Write-Warning "$ScriptName|Unknown action, not applied to org|$Action|$Target"
            }
        }

        if ($PassThru) {
            Write-Output $InputObject
        }
    }
}