如何发布重复检测规则?

问题描述 投票:0回答:1

我们有重复的检测规则,一旦我们将其导入其他环境,该规则就会进入非活动状态。 如何使用 Web API 使用 PowerShell 启用规则。

dynamics-crm dynamics-crm-online
1个回答
0
投票

您可以通过调用 webapi 操作PublishDuplicateRule来发布(启用)规则。

下面的 shell 展示了如何从 TestDuplicateRule 解决方案查询所有未发布的重复规则,然后发布所有这些规则。

# Change below values to match your environment
$clientId = "your-client-id"
$clientSecret = "your-client-secret"
$tenantId = "your-tenant-id"
$resource = "your-org-url" # Sample: https://myorg.crm5.dynamics.com/

# Get the access token
$tokenResponse = Invoke-RestMethod -Method Post -Uri "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" -ContentType "application/x-www-form-urlencoded" -Body @{
  grant_type    = "client_credentials"
  client_id     = $clientId
  client_secret = $clientSecret
  scope         = "$resource/.default"
}

$accessToken = $tokenResponse.access_token

# Define the solution name
$solutionName = 'TestDuplicateRule' # Replace with your solution name

# Fetch duplicate detection rules in the specified solution
$fetchXml = @"
<fetch>
<entity name='duplicaterule'>
  <attribute name='duplicateruleid' />
  <filter>
    <condition attribute='statecode' operator='ne' value='1' />
  </filter>
  <link-entity name='solutioncomponent' from='objectid' to='duplicateruleid' link-type='inner'>
    <link-entity name='solution' from='solutionid' to='solutionid' link-type='inner'>
      <filter>
        <condition attribute='uniquename' operator='eq' value='$solutionName' />
      </filter>
    </link-entity>
  </link-entity>
</entity>
</fetch>
"@

# Encode the FetchXML query
$encodedFetchXml = [System.Web.HttpUtility]::UrlEncode($fetchXml)

# Fetch duplicate rules using the Web API
$fetchUrl = "$resource/api/data/v9.2/duplicaterules?fetchXml=$encodedFetchXml"
$duplicateRulesResponse = Invoke-RestMethod -Method Get -Uri $fetchUrl -Headers @{ Authorization = "Bearer $accessToken" }

# Publish each duplicate rule
foreach ($rule in $duplicateRulesResponse.value) {
  $ruleId = $rule.duplicateruleid

  # Create the payload
  $payload = @{
      entity = @{
          "@odata.type" = "Microsoft.Dynamics.CRM.duplicaterule"
          duplicateruleid = $ruleId
      }
  } | ConvertTo-Json

  # Call the PublishDuplicateRule action
  $publishUrl = "$resource/api/data/v9.2/duplicaterules($ruleId)/Microsoft.Dynamics.CRM.PublishDuplicateRule"
  Invoke-RestMethod -Method Post -Uri $publishUrl -Headers @{ Authorization = "Bearer $accessToken" } -ContentType "application/json" -Body $payload
}
© www.soinside.com 2019 - 2024. All rights reserved.