[英]Merge Contacts in Dynamics CRM using C#
因此,我正在編寫一個控制台應用程序,該應用程序在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);
}
}
}
根據要求,我在下面包括了ContactsBL的內容。
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];
您應該使用iterator變量。
Contact contact = contactList[i];
Contact _subContact = contactList[i+1];
更新:可能的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++)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.