如何使用Dynamics CRM 2011中的插件识别重复记录 [英] How to identify duplicate records with a plugin in Dynamics CRM 2011

查看:145
本文介绍了如何使用Dynamics CRM 2011中的插件识别重复记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



它的作用是基本上是检查帐户名称并识别帐户名称是创建的重复。



所以,如果有一个帐户名称,巴克莱例如,我尝试再次创建这个我会提醒用户一个错误消息,这是以前创建的,并阻止添加此记录。

  public void Execute(IServiceProvider serviceProvider)
{
var context =(IPluginExecutionContext)serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

if(context.InputParameters.Contains(Target)&&b $ b context.InputParameters [Target]是Entity)
{
//从输入参数获取目标实体。
实体实体=(实体)context.InputParameters [Target];

if(entity.LogicalName ==account)
{
bool x = true;

if(entity.Attributes.Contains(Name)!= recordNamesinCRM)
{
}
else
{
throw new InvalidPluginExecutionException(你不能拥有重复的国家/地区代码!);
}
}
}
}

上面的代码我只是使用recordNamesinCRM作为一个例子,但我确定有一个内置的功能或比较的方式来比较创建一个新的名称与系统中的其余部分或一种计数重现的实例。

解决方案

您可以使用RetrieveDuplicatesRequest,如下例所示:

  ///< summary> 
///检查重复Guid
///< / summary>
///< param name =account>< / param>
///< returns>第一个重复的帐户ID,如果发现重复项,而Guid.Empty,如果不是< / returns>
public Guid DuplicateExists(Account account)
{
RetrieveDuplicatesRequest request = new RetrieveDuplicatesRequest();
request.BusinessEntity = account;
request.MatchingEntityName = Account.EntityLogicalName;
request.PagingInfo = new PagingInfo();
request.PagingInfo.PageNumber = 1;
request.PagingInfo.Count = 1;

RetrieveDuplicatesResponse response =(RetrieveDuplicatesResponse)ServiceProxy.Execute(request);
return response.DuplicateCollection.Entities.Count> 0? response.DuplicateCollection.Entities [0] .Id:Guid.Empty;
}

请参阅 http://crm-edinburgh.com/2011/08/crm-sdk-using-detect-duplicates - 设置代码/ 为例。


I am looking to design some logic inside of my create plugin for the entity 'account'.

What it does is basically check account Names and identifies account names which are duplicates on creation.

So if there is an account Name, Barclays for example, and I try to create this again I'm going to alert the user with an error message that this has been created before and prevents this record from being added.

public void Execute(IServiceProvider serviceProvider)
{
   var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

   if (context.InputParameters.Contains("Target") &&
       context.InputParameters["Target"] is Entity)
   {
      // Obtain the target entity from the input parmameters.
      Entity entity = (Entity)context.InputParameters["Target"];

      if (entity.LogicalName == "account")
      {
         bool x = true;

         if (entity.Attributes.Contains("Name") != recordNamesinCRM)
         {                    
         }
         else 
         {
           throw new InvalidPluginExecutionException("You Cannot Have Duplicate Country Codes!.");
         }          
      } 
   }
}

In the code above I am just using "recordNamesinCRM" as an example but I'm sure there is a built in function or way of comparing on create a new name with the rest in the system or a way of counting reoccurring instances.

解决方案

You can use the RetrieveDuplicatesRequest as per this example here:

    /// <summary>
    /// Checks for duplicate Guid
    /// </summary>
    /// <param name="account"></param>
    /// <returns>First duplicate account id, if any duplicates found, and Guid.Empty if not</returns>
    public Guid DuplicateExists(Account account)
    {
        RetrieveDuplicatesRequest request = new RetrieveDuplicatesRequest();
        request.BusinessEntity = account;
        request.MatchingEntityName = Account.EntityLogicalName;
        request.PagingInfo = new PagingInfo();
        request.PagingInfo.PageNumber = 1;
        request.PagingInfo.Count = 1;

        RetrieveDuplicatesResponse response = (RetrieveDuplicatesResponse)ServiceProxy.Execute(request);
        return response.DuplicateCollection.Entities.Count > 0 ? response.DuplicateCollection.Entities[0].Id : Guid.Empty;
    }

See http://crm-edinburgh.com/2011/08/crm-sdk-using-detect-duplicates-settings-in-code/ for an example.

这篇关于如何使用Dynamics CRM 2011中的插件识别重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆