Create New Records in Account Entity using PowerShell Script in D365

First, we need to install PowerShell Microsoft.Xrm.Data.PowerShell module for the current user blow executing the below script.

Install-Module Microsoft.Xrm.Data.PowerShell -Scope CurrentUser

Next, we need to run the below lines of script.

  1. This command sets the execution policy for PowerShell scripts to RemoteSigned for the current user scope.
    Set-ExecutionPolicy –ExecutionPolicy RemoteSigned –Scope CurrentUser

  2. This command sets the security protocol used by the .NET framework for network communications to TLS 1.2.

    [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
    

To connect to a specific environment we need to write two lines of script with username and password and environment URL as shown below.

$Username="[email protected]"
$Password="Welc@me@123"
$pwd = ConvertTo-SecureString $Password -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential($Username, $pwd)
$conn = Connect-CrmOnline -Credential $credentials -ServerUrl "https://org18828102.crm5.dynamics.com/" -ForceOAuth

Once the above script runs successfully to check CRM is connected, we need to run $conn.

If CRM is connected, we will get IsReady as true. Please refer below screenshot.

Next, to create a record in the CRM Account entity we need fellow below format.

Syntax

New-CrmRecord [-conn <CrmServiceClient>] [-EntityLogicalName] <String> [-Fields] <Hashtable> [<CommonParameters>]

Ex

$parentId = New-CrmRecord account @{"name"="parent account name"}
$parentReference = New-CrmEntityReference -EntityLogicalName account -Id $parentId
New-CrmRecord -conn $conn -EntityLogicalName account -Fields @{"name"="Test Account";"industrycode"=New-CrmOptionSetValue -Value 1;"parentaccountid"=$parentReference; "overriddencreatedon"=[datetime]"2000-01-01"}

Thank you..!


Similar Articles