因此,我正在编写一个控制台应用程序,用于合并CRM部署中的重复联系人。
这段代码有两部分。获取重复记录GUID的部分和执行实际合并的部分。我的问题在于前者。
我使用客户的电话号码来检查唯一性,正如您在代码中看到的那样,并且有一个txt文件,其中每个数字都包含在新行中。
我需要从这个文本文件中填写联系人列表,并将其传递给合并方法。
我可以定义一个硬编码字符串它是这样工作的但是
实际进行归并的部分是有效的,但将所有这些重复项填入List并将其传递下去的部分却没有。我试着填充它,好像它是一个字符串列表,但显然这不是它与联系人的工作方式。
代码包含在下面。
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);
}
}
}
根据要求,我在下面附上了联系人清单的内容。
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);
}
}
}
有一种更好的方法来查找重复项。在CRM中创建重复检测规则,然后从c#中,您可以向CRM发送请求以获取副本(它将使用这些规则为您查找副本)。这是最简单的方法。
以下节选自MSDN:
// PagingInfo is Required.
var request = new RetrieveDuplicatesRequest
{
BusinessEntity = new Account { Name = "Microsoft" }.ToEntity<Entity>(),
MatchingEntityName = Account.EntityLogicalName,
PagingInfo = new PagingInfo() { PageNumber = 1, Count = 50 }
};
Console.WriteLine("Retrieving duplicates");
var response =(RetrieveDuplicatesResponse)_serviceProxy.Execute(request);
for (int i = 0; i < response.DuplicateCollection.Entities.Count; i++)
{
var crmAccount = response.DuplicateCollection.Entities[i].ToEntity<Account>();
Console.WriteLine(crmAccount.Name + ", " + crmAccount.AccountId.Value.ToString());
}
你好像忘了i
。
不要在主循环中使用索引0
和1
,因为这会一次又一次地合并前两个实体。你应该使用你的迭代器变量i
。
不要用绝对索引初始化联系人:
Contact contact = contactList[0];
Contact _subContact = contactList[1];
应该使用迭代器变量。
Contact contact = contactList[i];
Contact _subContact = contactList[i+1];
<
更新:可能em> IndexOutOfBoundsException
注意可能因为contactList[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++)