Public/Invoke-SfObjectUpdate.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<#
    .SYNOPSIS
    Updates a Salesforce object

    .DESCRIPTION
    Updates a Salesforce object

    .INPUTS
    A PSCustomObject that has the fields to update.

    .OUTPUTS
    None

    .PARAMETER Object
    A PSCustomObject that has the fields to update.

    .EXAMPLE
    PS> $du = @{
        attributes = @{
            url = "/services/data/v48.0/sobjects/phecc__Device__c/a0N3t000008XXe5EAG"
        }
        phecc__Replace_Request_Date__c = "2020-10-12"
    }
    PS> Invoke-SfObjectUpdate $du

    .LINK
    https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_update_fields.htm?search_text=Patch
    Set-Config

    .NOTES
    Assumes config is initialized for org access.
#>

function Invoke-SfObjectUpdate {

    [CmdletBinding()]
    [OutputType([PSCustomObject])]
    param(
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline)]
        [ValidateNotNullOrEmpty()]
        [PSCustomObject]
        $Object
    )

    begin {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started"
    }

    end {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete"
    }

    process {
        Write-Debug "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)"
        # e.g. /services/data/v48.0/sobjects/phecc__Device__c/a0N3t000008XXe4EAG
        $Type = ($Object.attributes.url -split "/")[5]
        $Id = ($Object.attributes.url -split "/")[6]
        $Object = $Object | Select-Object -Property * -ExcludeProperty attributes
        $Body = $Object | ConvertTo-Json -Depth 100
        Invoke-SfApi "/sobjects/$($Type)/$($Id)" -Method Patch -Body $Body
    }
}