C# 使用C在Dynamics CRM中合并联系人#

C# 使用C在Dynamics CRM中合并联系人#,c#,dynamics-crm-2011,dynamics-crm,C#,Dynamics Crm 2011,Dynamics Crm,因此,我正在编写一个控制台应用程序,将重复的联系人合并到CRM部署中 该代码分为两部分。获取重复记录GUID的部分和执行实际合并的部分。我的问题在于前者 我使用客户的电话号码来检查代码的唯一性,代码中有一个txt文件,其中每一个号码都包含在一个新行中 我需要填写此文本文件中的联系人列表,并将其传递给合并方法 我可以定义一个硬编码字符串,它是这样工作的,但是 实际执行合并的部分可以工作,但将所有这些重复项填入列表并将其传递的部分不能工作。我试着把它当作一个字符串列表来填充,但显然这不是它处理联系人

因此,我正在编写一个控制台应用程序,将重复的联系人合并到CRM部署中

该代码分为两部分。获取重复记录GUID的部分和执行实际合并的部分。我的问题在于前者

我使用客户的电话号码来检查代码的唯一性,代码中有一个txt文件,其中每一个号码都包含在一个新行中

我需要填写此文本文件中的联系人列表,并将其传递给合并方法

我可以定义一个硬编码字符串,它是这样工作的,但是

实际执行合并的部分可以工作,但将所有这些重复项填入列表并将其传递的部分不能工作。我试着把它当作一个字符串列表来填充,但显然这不是它处理联系人的方式

代码包括在下面

using System;
using System.ServiceModel;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Zeno.Business;
using Zeno.Configuration;
using Zeno.CRMEntityModel;
using System.Collections.Generic;
using System.IO;

namespace MergeTool
{

    public static class MergeContact
    {

        public static ContactBL bl = new ContactBL();



        /// <param name="serverConfig">Contains server connection information.</param>
        /// <param name="promptForDelete">When True, the user will be prompted to delete
        /// all created entities.</param>
        public static void Merge()
        {
            List<Contact> contactList = getAccountGuids();

            for (int i = 0; i < contactList.Count; i++)
            {
                Contact contact = contactList[0];
                Contact _subContact = contactList[1];
                EntityReference target = new EntityReference();

                target.Id = contact.ContactId.Value;
                target.LogicalName = Contact.EntityLogicalName;

                MergeRequest merge = new MergeRequest();
                merge.SubordinateId = _subContact.ContactId.Value;
                merge.Target = target;
                merge.PerformParentingChecks = false;

                Contact updateContent = new Contact();
                updateContent.zeno_nebimcustomernumber = _subContact.zeno_nebimcustomernumber;
                //updateContent....
                if (string.IsNullOrEmpty(contact.FirstName))
                {
                    updateContent.FirstName = _subContact.FirstName;
                }
                //further if conditions clipped for brevity

                merge.UpdateContent = updateContent;

                MergeResponse merged = (MergeResponse)bl.Execute(merge);

            }
        }

        public static List<Contact> getAccountGuids()
        {
            //TO DO
            // Get all duplicate contact mobile phone numbers

            string mobilePhone = "+90(505)220 72 29";

            return bl.RetrieveContactListByMobilePhone(mobilePhone);
        }
    }
}
使用系统;
使用System.ServiceModel;
使用Microsoft.Crm.Sdk.Messages;
使用Microsoft.Xrm.Sdk;
使用Microsoft.Xrm.Sdk.Client;
使用Microsoft.Xrm.Sdk.Query;
使用Zeno.Business;
使用Zeno.Configuration;
使用Zeno.CRMEntityModel;
使用System.Collections.Generic;
使用System.IO;
名称空间合并工具
{
公共静态类合并联系人
{
public static ContactBL=新ContactBL();
///包含服务器连接信息。
///如果为True,将提示用户删除
///所有创建的实体。
公共静态无效合并()
{
List contactList=getAccountGuids();
对于(int i=0;i
根据要求,我已将联系人的内容包括在下面

using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Zeno.CRMEntityModel;
using Zeno.Repository;

namespace Zeno.Business
{
    public class ContactBL:BLBase
    {

        private ContactRepository contactRepository;

        public ContactBL()
            : base()
        {
        }

        public ContactBL(IOrganizationService service)
            : base(service)
        {
        }

        public Guid CheckPhoneNumber(string phoneNumber)
        {
            this.contactRepository = new ContactRepository(this.Connection);

            return contactRepository.CheckPhoneNumber(phoneNumber);
        }




        public List<Contact> RetrieveContactListByMobilePhone(string phoneNumber)
        {
            this.contactRepository = new ContactRepository(this.Connection);

            return contactRepository.RetrieveContactListByMobilePhone(phoneNumber);
        }

    }
}
使用Microsoft.Xrm.Sdk;
使用制度;
使用System.Collections.Generic;
使用系统数据;
使用System.Linq;
使用系统文本;
使用Zeno.CRMEntityModel;
使用Zeno.Repository;
名称空间Zeno.Business
{
公共类ContactBL:BLBase
{
私有联系人存储库联系人存储库;
公众联络
:base()
{
}
公共联系人BL(IOR组织服务)
:基地(服务)
{
}
公共Guid CheckPhoneNumber(字符串phoneNumber)
{
this.contactRepository=新的contactRepository(this.Connection);
返回contactRepository.CheckPhoneNumber(电话号码);
}
公用列表检索ContactListByMobilePhone(字符串电话号码)
{
this.contactRepository=新的contactRepository(this.Connection);
返回contactRepository.RetrieveContactListByMobilePhone(电话号码);
}
}
}

有一种更好的方法来查找重复项。在CRM中创建重复检测规则,然后从C#向CRM发送请求以取回重复项(后者将使用这些规则为您查找重复项)。这是最简单的方法

以下摘自:

//需要分页信息。
var请求=新检索的副本请求
{
BusinessEntity=新帐户{Name=“Microsoft”}.ToEntity(),
MatchingEntityName=Account.EntityLogicalName,
PaginInfo=新建PaginInfo(){PageNumber=1,Count=50}
};
Console.WriteLine(“检索副本”);
var响应=(检索到的副本响应)\u serviceProxy.Execute(请求);
对于(int i=0;i
看起来你忘了
i

您不希望在主循环中使用索引
0
1
,因为这会反复合并前两个实体。您应该使用迭代器变量
i

不要使用绝对索引初始化联系人:

        Contact contact = contactList[0];
        Contact _subContact = contactList[1];
您应该使用迭代器变量

        Contact contact = contactList[i];
        Contact _subContact = contactList[i+1];

更新:可能的
IndexOutOfBoundsException

请注意可能由于联系人列表[i+1]
引用而引发的IndexOutOfBoundsException。您应该通过与
contactList.Count-1
而不是
contaclist.Count
进行比较来避免这种情况:

// BAD! : contactList[i+1] would throw IndexOutOfBoundsException on the last cycle.
for(int i=0; i < contactList.Count; i++) 

// GOOD : contactList[i+1] would point to the last item in the last cycle so it should be okay.
for(int i=0; i < contactList.Count - 1; i++)
//糟糕!:contactList[i+1]将在最后一个周期抛出IndexOutOfBoundsException。
对于(int i=0;i
你能发布内容吗
// BAD! : contactList[i+1] would throw IndexOutOfBoundsException on the last cycle.
for(int i=0; i < contactList.Count; i++) 

// GOOD : contactList[i+1] would point to the last item in the last cycle so it should be okay.
for(int i=0; i < contactList.Count - 1; i++)