Managing Business Rules in Dataverse Using PowerShell

Loading

Business Rules in Dataverse allow you to apply logic and validations at the data layer without writing code. Using PowerShell, you can automate the management of Business Rules, including:

Listing all Business Rules
Creating a new Business Rule
Updating an existing Business Rule
Enabling/Disabling Business Rules
Deleting Business Rules


Step 1: Prerequisites

1. Required Permissions

  • System Administrator or Customization Role in Dataverse.
  • Power Platform API access via PowerShell.

2. Install and Import Required PowerShell Modules

Ensure the Power Platform PowerShell modules are installed.

powershellCopyEdit# Install Power Platform PowerShell modules
Install-Module -Name Microsoft.PowerPlatform.Cds.Client -Scope CurrentUser -Force

# Import the module
Import-Module Microsoft.PowerPlatform.Cds.Client

Step 2: Connect to Dataverse Using PowerShell

Option 1: Interactive Login

# Connect to Dataverse interactively
$connection = Connect-CdsService -ConnectionString "AuthType=OAuth;Url=https://yourorg.crm.dynamics.com;Prompt=Login"

A sign-in window will appear for authentication.

Option 2: Using Service Principal (App Registration)

For automation scripts, use Azure AD App Registration credentials.

# Define credentials
$clientId = "your-app-client-id"
$clientSecret = "your-app-client-secret"
$tenantId = "your-tenant-id"
$orgUrl = "https://yourorg.crm.dynamics.com"

# Convert secret to secure string
$secureSecret = ConvertTo-SecureString $clientSecret -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($clientId, $secureSecret)

# Connect to Dataverse
$connection = Connect-CdsService -Url $orgUrl -ClientId $clientId -ClientSecret $secureSecret -TenantId $tenantId

Step 3: Listing All Business Rules in Dataverse

To retrieve all Business Rules in a specific table:

# Define table (entity) name
$tableName = "account" # Change this to the desired table name

# Fetch all Business Rules
$businessRules = Get-CdsRecord -Connection $connection -EntityLogicalName workflow -Filter "category eq 2 AND primaryentity eq '$tableName'"

# Display Business Rules
$businessRules | Select-Object name, statecode, workflowid

This script:
Fetches all Business Rules from the specified table.
Displays their name, state (enabled/disabled), and ID.


Step 4: Creating a New Business Rule

This example creates a Business Rule that sets a field value when a condition is met.

# Define parameters
$ruleName = "Set Account Credit Limit"
$tableName = "account"

# Create a new Business Rule
$businessRule = New-CdsRecord -Connection $connection -EntityLogicalName workflow -Fields @{
"name" = $ruleName;
"category" = 2; # 2 = Business Rule
"primaryentity" = $tableName;
"statecode" = 0; # Draft state
}

Write-Host "Business Rule '$ruleName' created successfully!"

This script:
Creates a Business Rule in Draft mode for the specified table.
You must define actions separately (e.g., setting field values).


Step 5: Enabling a Business Rule

Once a Business Rule is created, it must be activated before applying changes.

# Define Business Rule Name
$ruleName = "Set Account Credit Limit"

# Get Business Rule ID
$businessRule = Get-CdsRecord -Connection $connection -EntityLogicalName workflow -Filter "name eq '$ruleName'"
$ruleId = $businessRule.workflowid

# Activate the Business Rule
Set-CdsRecord -Connection $connection -EntityLogicalName workflow -Id $ruleId -Fields @{ "statecode" = 1; "statuscode" = 2 }

Write-Host "Business Rule '$ruleName' activated successfully!"

This script:
Finds the Business Rule ID by name
Sets it to Active (statecode = 1, statuscode = 2)


Step 6: Disabling a Business Rule

If a Business Rule needs to be modified or removed, it must first be deactivated.

# Define Business Rule Name
$ruleName = "Set Account Credit Limit"

# Get Business Rule ID
$businessRule = Get-CdsRecord -Connection $connection -EntityLogicalName workflow -Filter "name eq '$ruleName'"
$ruleId = $businessRule.workflowid

# Deactivate the Business Rule
Set-CdsRecord -Connection $connection -EntityLogicalName workflow -Id $ruleId -Fields @{ "statecode" = 0; "statuscode" = 1 }

Write-Host "Business Rule '$ruleName' deactivated successfully!"

This script:
Finds the Business Rule ID
Sets it to Draft (statecode = 0, statuscode = 1)


Step 7: Updating an Existing Business Rule

To modify a Business Rule, first deactivate it, then update the rule.

# Define Business Rule Name
$ruleName = "Set Account Credit Limit"

# Get Business Rule ID
$businessRule = Get-CdsRecord -Connection $connection -EntityLogicalName workflow -Filter "name eq '$ruleName'"
$ruleId = $businessRule.workflowid

# Deactivate the Business Rule before modifying
Set-CdsRecord -Connection $connection -EntityLogicalName workflow -Id $ruleId -Fields @{ "statecode" = 0; "statuscode" = 1 }

# Update the Business Rule name
Set-CdsRecord -Connection $connection -EntityLogicalName workflow -Id $ruleId -Fields @{ "name" = "Updated Account Credit Limit Rule" }

Write-Host "Business Rule updated successfully!"

This script:
Deactivates the Business Rule
Updates the rule name


Step 8: Deleting a Business Rule

To remove a Business Rule, first deactivate it, then delete it.

# Define Business Rule Name
$ruleName = "Set Account Credit Limit"

# Get Business Rule ID
$businessRule = Get-CdsRecord -Connection $connection -EntityLogicalName workflow -Filter "name eq '$ruleName'"
$ruleId = $businessRule.workflowid

# Deactivate the Business Rule before deleting
Set-CdsRecord -Connection $connection -EntityLogicalName workflow -Id $ruleId -Fields @{ "statecode" = 0; "statuscode" = 1 }

# Delete the Business Rule
Remove-CdsRecord -Connection $connection -EntityLogicalName workflow -Id $ruleId

Write-Host "Business Rule '$ruleName' deleted successfully!"

This script:
Finds the Business Rule ID
Deactivates it before deletion
Deletes the rule from Dataverse

Leave a Reply

Your email address will not be published. Required fields are marked *