CURD (Create, Update, Retrieve and Delete) Operation in D365 using Power Shell Script

Connect D365 using below details:

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

Set-ExecutionPolicy –ExecutionPolicy RemoteSigned –Scope CurrentUser

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

$Username="APY***@Trailcrm.com"

$Password="apy****"

$pwd = ConvertTo-SecureString $Password -AsPlainText -Force 

$credentials = New-Object System.Management.Automation.PSCredential($Username, $pwd)

$conn = Connect-CrmOnline -Credential $credentials -ServerUrl "https://apytrailcrm****.crm.dynamics.com/" -ForceOAuth

Create a record using powershell

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"="account name";"industrycode"=New-CrmOptionSetValue -Value 1;"parentaccountid"=$parentReference;"apy_boolean"= $true;"overriddencreatedon"=[datetime]"2000-01-01"} 

Update a record using powershell

Syntax:

    Set-CrmRecord [-conn <CrmServiceClient>] [-CrmRecord] <PSObject> [-Upsert <SwitchParameter>] [-PrimaryKeyField <String>] [<CommonParameters>]

    Set-CrmRecord [-conn <CrmServiceClient>] [-EntityLogicalName] <String> [-Id] <Guid> [-Fields] <Hashtable> [-Upsert <SwitchParameter>] [-PrimaryKeyField <String>] [<CommonParameters>]

Ex:

    Set-CrmRecord -conn $conn -EntityLogicalName account -Id 3f239485-6d37-****-****-6045bd006e13 -Fields @{"name"="account name";"industrycode"=New-CrmOptionSetValue -Value 1;"parentaccountid"=$parentReference;"apy_boolean"= $true;"overriddencreatedon"=[datetime]"2000-01-01"}

Get the record using power shell

Syntax:

    Get-CrmRecords [-conn <CrmServiceClient>] [-EntityLogicalName] <String> [[-FilterAttribute] <String>] [[-FilterOperator] <String>] [[-FilterValue] <String>] [[-Fields] <String[]>] [[-AllRows] <SwitchParameter>] [[-TopCount] <Int32>] [<CommonParameters>]

$acc = Get-CrmRecords -conn $conn -EntityLogicalName account -FilterAttribute name -FilterOperator "eq" -FilterValue "account name" -Fields name,accountnumber

In $acc Get only one record we can use below

$onerecord = $acc.CrmRecords[0]

Get the records by using View Name:

Get-CrmRecordsByViewName -conn $conn -ViewName "Active Accounts"

Delete a record using powershell

Syntax:

    Remove-CrmRecord [-conn <CrmServiceClient>] [-CrmRecord] <PSObject> [<CommonParameters>]

    Remove-CrmRecord [-conn <CrmServiceClient>] [-EntityLogicalName] <String> [-Id] <Guid> [<CommonParameters>]

Ex: 

    Remove-CrmRecord -conn $conn -EntityLogicalName account -Id 3f239485-6d37-****-****-6045bd006e13


Comments

Popular posts from this blog

Basic Plugin Code in D365 using C#

Meta Data Using WebApiRequest