Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PSBlog003
$CustomerNbr = "ABARTENDE"
$Description = "This was auto created with PowerShell"
$InventoryID = "CONSULTING"
$Qty = "1.000000"
$UnitPrice = "100.000000"
$UOM = "HOUR"
#The @" and "@ tags declare a here string which allows you to have string declarations that run multiple lines
$body = @"
{
`"Customer`": {`"value`": `"$CustomerNbr`"},
`"Description`": {`"value`": `"$Description`"},
`"Details`": [{`"InventoryID`": {`"value`": `"$InventoryID`"},
`"Qty`": {`"value`": $Qty},
`"UnitPrice`": {`"value`": $UnitPrice},
`"UOM`": {`"value`": `"$UOM`"}}]
}
"@ #this closing tag must be anchored to the far left or it will cause an issue
$Uri = "$AcumaticaEndPoint/entity/default/18.200.001/Invoice"
#We can load up the parameters into a Hash Table then pass the $hash table as a single
#argument. This is called splatting
$HashArguments = @{
#a benefit of splatting is it’s easier to add inline commentary to explain your code.
Uri = "$AcumaticaEndPoint/entity/default/18.200.001/Invoice"
#The put type is needed to insert a new Invoice
Method = 'PUT'
#no need to create a new header lets use the same header we used in previous calls
Headers = $headers
Body = $body
#This WebSession object prevents the "Not Logged in error" should it be missing or not initialized
WebSession = $WebSession
}
#The below line replaces the -Uri $Uri -Method 'PUT' -Headers $headers -Body $body with splatting
#Note that we use a @ char instead of $ to indicate we are splatting the parameters.
$response = Invoke-RestMethod @HashArguments
#We Pipe the response to the convert to Json cmdlet first to convert it to a clean JSON string.
#We then immediately pipe the value to the ConvertFrom-Json which will create a convenient PSObject
#Based on that JSON string to get our values.
$InvoiceAsPSObject = $response | ConvertTo-Json | ConvertFrom-Json
#Now we can load the target data we need further down into variables
$ReferenceNbr = $InvoiceAsPSObject.ReferenceNbr.value
$EntityID = $InvoiceAsPSObject.id
#This next line simply displays info to the output window its equivalent to what you would expect from a
#Console.WriteLine($”Invoice {referenceNbr} created”) line in a .NET console app
"Invoice $ReferenceNbr Created"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.