function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andy Morton 14Andy Morton 14 

Create custom record with HTTP REST API Powershell not working

Hello all,

I've been working on some API integration between Salesforce and SQL using Powershell. 

So far I've managed to get the majority working with querying Salesforce for some record details, grabbing information from SQL and inserting the SQL data into the Salesforce records. 

However, I'm having some trouble trying to get it to create a new record. 

We have a custom object (Usage_Billings__c) which the SQL data is put into. Inserting this into existing records is fine but if I try to create a new record it fails with error "Unexpected character ('P' (code 80)): was expecting comma to separate OBJECT entries at [line:3, column:33]","errorCode":"JSON_PARSER_ERROR"

I'm fairly new to API integrations and haven't had much luck with finding a solution online (according to some posts, what I'm doing should work fine). 

My Script:

(I've omitted our SF connection section which works fine as can gather the information fine as well as our sandbox URL)
 
$GUID = "810Pu000001MruoIAC"

$getContracturl = "https://MYSANDBOX.my.salesforce.com/services/data/v57.0/query?q=SELECT+FIELDS(ALL)+FROM+servicecontract+WHERE+id+=+'$GUID'+LIMIT+50"
$getContractResponse = Invoke-RestMethod -Uri $getContracturl -Method 'Get' -Headers $headers -TimeoutSec 0.03
$getContractResponse | ConvertTo-Json -Depth 10

$accountid = $getContractResponse.records.accountid
$dat_size = "150"
$lic_365 = "1"
$lic_google = "1"



$createUrl = "https://MYSANDBOX.my.salesforce.com/services/data/v57.0/sobjects/Usage_Billing__c"
$requestbody =@"
{

    "Service_Contract__c" : $GUID,
    "Stor__c" : $dat_size,
    "X365_Users__c" : $lic_365,
    "G_Users__c" : $lic_google
 


}
"@


Write-Host $requestbody

$createBilling = Invoke-RestMethod -Uri $createUrl -Method 'Post' -Headers $headers -Body $requestbody -TimeoutSec 0.03
$createBilling | ConvertTo-JSON
Anyone able to point me in the right direction?

All help appreciated.

Andy
 
Best Answer chosen by Andy Morton 14
Andy Morton 14Andy Morton 14
Never mind, realised I didn't encapsulate the $GUID variable in quotations. 

"Service_Contract__c" : "$GUID",

All working now - will leave this here in case anyone else comes across same issue (shows that something simple can waste loads of time :) )

All Answers

Andy Morton 14Andy Morton 14
Output from the Write-Host $request body if it helps:

{
    "Service_Contract__c" : 810Pu000001MruoIAC,
    "Stor__c" : 150,
    "X365_Users__c" : 1,
    "G_Users__c" : 1
}
Andy Morton 14Andy Morton 14
Never mind, realised I didn't encapsulate the $GUID variable in quotations. 

"Service_Contract__c" : "$GUID",

All working now - will leave this here in case anyone else comes across same issue (shows that something simple can waste loads of time :) )
This was selected as the best answer