Activate Business Rules Using Powershell in D365

Introduction

In Dynamics CRM, business rules play a pivotal role in automating business logic without the need for extensive coding. However, as business processes evolve, certain rules may become inactive, necessitating a streamlined approach to reactivate them. This article explores the automation of activating inactive business rules in Dynamics CRM using a PowerShell script. The script targets specific conditions defined in a Fetch XML query, offering a selective and efficient way to re-enable business rules.

Use Cases

  1. Activation of Inactive Business Rules: The primary objective of the script is to identify and activate inactive business rules within the Dynamics CRM environment. This automation is particularly valuable when dealing with a large number of rules that may have been deactivated due to changes in business processes or system configurations.

  2. Selective Activation Based on Conditions: The script's design allows for a targeted approach to reactivating business rules that meet specific criteria. By specifying conditions in the Fetch XML query, organizations can selectively activate rules based on their relevance to particular scenarios, ensuring a focused and controlled reactivation process.

Script to Activate Business Rules

Step 1. Install and import PowerShell Microsoft.Xrm.Data.PowerShell module using the below script.

$Module = "Microsoft.Xrm.Data.PowerShell"
Install-Module -Name $Module -Scope CurrentUser -AllowClobber -Force
Import-Module -Name $Module -Force

Step 2. Set up a connection to the Dynamics CE environment using the below script.

Prerequisite

The application should be registered with Dynamics CRM API permission in Azure Portal and the application user should have sufficient roles to perform desired operations.

$conn = Get-CrmConnection -ConnectionString "AuthType=ClientSecret;Url=<URL>;ClientId=<ClientId>;ClientSecret=<ClientSecret>"
Write-Host "Connected to env" $conn.ConnectedOrgFriendlyName

The above set of commands employs theGet-CrmConnectioncmdlet with a connection string containing details for Client Secret authentication, including the CRM URL, client ID, and client secret.

This is a recommended way of connecting to the Dynamics environment securely and can be easily implemented in automation pipelines.

Step 3. Fetch XML query to retrieve inactive Business Rules. The below script will save FetchXML into the $fetchXmlworkflow variable.

$fetchXmlworkflow = @"
<fetch version="1.0" output-format="xml-platform" mapping="logical">
  <entity name="workflow">
    <all-attributes />   
    <filter type="and">   
      <condition attribute="category" operator="eq" value="2" />
      <condition attribute="statecode" operator="eq" value="0" />  
    </filter>
  </entity>
</fetch>
"@

Step 4. Fetch CRM records based on the provided Fetch XML and Display the total count of retrieved workflow records using the below script.

$workflowrecords = Get-CrmRecordsByFetch -conn $conn -Fetch $fetchXmlworkflow

Write-Host "Total Count: $($workflowrecords.Count)"

Step 5. Use the below script to loop through all inactive business rules and activate them.

# Check if any inactive business rules are found
if ($workflowrecords.Count -gt 0) {

    # Iterate through each workflow record
    foreach ($workflowrecord in $workflowrecords.CrmRecords) {   

        # Display the name of the business rule being activated
        Write-Host "Activating Business Rule: $($workflowrecord.name) "

        # Activate the business rule using Set-CrmRecordState
        Set-CrmRecordState -conn $conn -EntityLogicalName workflow -Id $workflowrecord.workflowid -StateCode 1 -StatusCode 2
    }  
}
else {
    # Display a message if no inactive business rules are found
    Write-Host "No Inactive Business Rules Found"
}

Step 6. Below is a sample output from my execution.

Output  execution

Note. The same procedure can be implemented for deactivating business rules, just Statecode and Statuscode values need to be updated.

Conclusion

Automating the activation of inactive business rules in Dynamics CRM using PowerShell offers organizations a powerful tool to maintain and update business logic in response to evolving business requirements. The script's ability to selectively reactivate rules based on specified conditions streamlines the process, allowing administrators and business analysts to efficiently manage and ensure the relevance of business rules in their CRM environment. By combining the flexibility of Dynamics CRM's business rules with the automation capabilities of PowerShell, organizations can achieve a more dynamic and responsive CRM system.

Please feel free to reach out in case of any concerns.


Similar Articles