PowerShell Dynamics CRM: Create an Account

Back to Index

Creates a new account.

Documentation: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/webapi/create-entity-web-api

CURL Command

curl -X POST https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/accounts \
  -H "Accept: application/json" \
  -H "Content-Type: application/json; charset=utf-8" \
  -H "OData-MaxVersion: 4.0"  \
  -H "OData-Version: 4.0" \
  -H "Authorization: Bearer DYNAMICS_CRM_ACCESS_TOKEN" \
  -d '{
    "name": "Sample Account",
    "creditonhold": false,
    "address1_latitude": 47.639583,
    "description": "This is the description of the sample account",
    "revenue": 5000000,
    "accountcategorycode": 1
}'

PowerShell Example

[Reflection.Assembly]::LoadFile("C:\myAssemblies\ChilkatDotNet2.dll")

$rest = New-Object Chilkat.Rest

#  URL: https://my-dynamics-domain.api.crm.dynamics.com/api/data/v9.0/accounts
$bTls = $true
$port = 443
$bAutoReconnect = $true
$success = $rest.Connect("my-dynamics-domain.api.crm.dynamics.com",$port,$bTls,$bAutoReconnect)
if ($success -ne $true) {
    $("ConnectFailReason: " + $rest.ConnectFailReason)
    $($rest.LastErrorText)
    exit
}

$json = New-Object Chilkat.JsonObject
$json.UpdateString("name","Sample Account")
$json.UpdateBool("creditonhold",$false)
$json.UpdateNumber("address1_latitude","47.639583")
$json.UpdateString("description","This is the description of the sample account")
$json.UpdateNumber("revenue","5000000")
$json.UpdateNumber("accountcategorycode","1")

$rest.AddHeader("Content-Type","application/json; charset=utf-8")
$rest.AddHeader("OData-Version","4.0")
$rest.AddHeader("Accept","application/json")
$rest.AddHeader("OData-MaxVersion","4.0")
$rest.AddHeader("Authorization","Bearer DYNAMICS_CRM_ACCESS_TOKEN")

$sbRequestBody = New-Object Chilkat.StringBuilder
$json.EmitSb($sbRequestBody)
$sbResponseBody = New-Object Chilkat.StringBuilder
$success = $rest.FullRequestSb("POST","/api/data/v9.0/accounts",$sbRequestBody,$sbResponseBody)
if ($success -ne $true) {
    $($rest.LastErrorText)
    exit
}

$respStatusCode = $rest.ResponseStatusCode
if ($respStatusCode -ge 400) {
    $("Response Status Code = " + $respStatusCode)
    $("Response Header:")
    $($rest.ResponseHeader)
    $("Response Body:")
    $($sbResponseBody.GetAsString())
    exit
}