Optimizing Dynamics 365: Thread Jobs for Multiple Record Updates

Utilizing Thread Jobs for Updating Multiple Records in Dynamics 365 using PowerShell Script.

$conn = Get-CrmConnection -InteractiveMode
$conn.BypassPluginExecution = $true
  • $conn = Get-CrmConnection -InteractiveMode: This command is likely attempting to establish a connection to a Dynamics 365 instance in interactive mode. This means it will prompt you for credentials or other necessary information interactively.
  • $conn.BypassPluginExecution = $true: This command sets the BypassPluginExecution property of the connection object to $true. This property indicates whether to bypass the execution of plug-ins and custom workflow activities in Dynamics 365. Setting it to $true means that plug-ins and custom workflow activities will be bypassed during the execution of operations through this connection.

PowerShell Script

Connection IsReady = True.

Connection IsReady

After establishing the connection, verify the results of the contact before proceeding with any updates.

Establishing the connection

Here, we are retrieving all the Contact Records and Updating First and Last Name Attributes using Thread Job.

Sample Code

# Update Related Account

$relatedContactFetchXml = @"
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
  <entity name="contact">
    <attribute name="fullname" />
    <attribute name="telephone1" />
    <attribute name="contactid" />
    <order attribute="fullname" descending="false" />

$counter = [pscustomobject] @{ Value = 0 }
$groupSize = 100

$relatedContactFetchXml = (Get-CrmRecordsByFetch -conn $conn -Fetch $relatedContactFetchXml -AllRows).CrmRecords;

$relatedContactFetchXml | Group-Object -Property {  [math]::Floor($counter.Value++ / $groupSize) } |

Foreach {
    Start-ThreadJob -ArgumentList @($conn, $_.Group) {
        param($conn, $group)
        $stripNonDigits = [regex]::new("[^\d]")
        $executeReq = [Microsoft.Xrm.Sdk.Messages.ExecuteMultipleRequest]::new()
        $executeSettings = [Microsoft.Xrm.Sdk.ExecuteMultipleSettings]::new()
        $executeSettings.ContinueOnError = $true
        $executeSettings.ReturnResponses = $false
        $executeReq.Requests = [Microsoft.Xrm.Sdk.OrganizationRequestCollection]::new()
        $executeReq.Settings = $executeSettings
        foreach($record in $group) {
            $updateEntity = [Microsoft.Xrm.Sdk.Entity]::new("contact", "$($record.ReturnProperty_Id)")
            $updateEntity.Attributes.Add("approvedforconnectandshare", $true)
            $updateEntity.Attributes.Add("approvedforwebsite", $true)
            $updateReq = [Microsoft.Xrm.Sdk.Messages.UpdateRequest]::new()
            $updateReq.Target = $updateEntity
        Write-Host "`n------------------------`nExecuting Batch`n------------------------`n"
        $null = $conn.Execute($executeReq)


After Execution of the Sample Code.

The results are shown below.


Similar Articles