· 4 years ago · Apr 14, 2021, 10:28 PM
1<#
2.Synopsis
3 Update a customer custom field using the Syncro API
4.DESCRIPTION
5 To use this script, you have to create a API Token that has those permissions:
6 - Customer - Edit
7.NOTES
8 Version: 1.0
9 Author: Alexandre-Jacques St-Jacques
10 Creation Date: 14-04-2021
11 Purpose/Change: Initial script development
12#>
13
14# The API token used for the request. Create a API key with permission "Customer - Edit". It is advised to populated it with the "Script variable" feature of syncro
15#$ApiToken = "test"
16# Will usually be syncromsp.com
17$ApiBaseURL = $env:RepairTechApiBaseURL
18# Your account sub domain will magically be imported.
19$ApiSubDomain = $env:RepairTechApiSubDomain
20
21function Customer-Update-Field {
22 param(
23 [Parameter(Mandatory=$true)]
24 [String]
25 $ApiToken,
26 [Parameter(Mandatory=$true)]
27 [String]
28 $CustomerId,
29 [Parameter(Mandatory=$true)]
30 [String]
31 $CustomField,
32 [Parameter(Mandatory=$true)]
33 [String]
34 $CustomFieldValue
35 )
36
37$headers = @{
38 Content='application/json'
39 Authorization="Bearer $ApiToken"
40}
41
42$payload = @"
43{
44 "properties": {
45 "$CustomField": "$CustomFieldValue"
46 }
47}
48"@
49
50 $ApiPath = "/api/v1/customers"
51
52 $resp = try {
53 Invoke-RestMethod -Method PUT "https://$($ApiSubDomain).$($ApiBaseURL)$($ApiPath)/$($CustomerId)" -Headers $headers -Body "$payload" -ContentType "application/json"
54 } catch {
55 Write-Host "ERROR!"
56 $result = $_.Exception.Response.GetResponseStream()
57 $reader = New-Object System.IO.StreamReader($result)
58 $reader.BaseStream.Position = 0
59 $reader.DiscardBufferedData()
60 $responseBody = $reader.ReadToEnd();
61 Write-Host $responseBody
62 }
63}
64
65# This is the function call to update the custom field of a customer. I would recommend you to populate $CustomerID using a script variable that uses the "{{customer_id}}" platform variable
66Customer-Update-Field -ApiToken $ApiToken -CustomerId $CustomerID -CustomField "Customer test" -CustomFieldValue "testing"