2012-05-15 53 views
2

我希望有人能夠幫助我。我找到了一篇文章,其中提供了一小段代碼,用於將添加多個實體(成員)添加到市場營銷列表。到現在爲止還挺好。我遇到了這個問題。我有一個自定義的查詢字段,可以在營銷成員列表中獲取另一個營銷列表(包含聯繫人,帳戶或潛在客戶)。現在我需要將這些成員遷移(添加)到我的新營銷列表中。我的代碼有:將會員添加到CRM 2011插件中的營銷列表

1. AddListMembersListRequest request = new AddListMembersListRequest(); 
    2. request.ListId = Origmarketing_List_Id.Id; 
    3. request.MemberIds = new Guid[1]; 
    4. request.MemberIds[0] = guid; 
    5. AddListMembersListResponse resp = (AddListMembersListResponse)service.Execute(request); 

2號線是我的ID從得到的EntityReference(查找場得到另一個營銷列表),現在是三,四線我設置的東西我真的困惑,但我仍然確信我會在這裏,因爲我把它設置到listmemberid。在這個例子中,我只有一個原因,我想試試它的工作原理。第4行bdw中的guid獲得正確的值,它在我的代碼的頂部聲明(並且我已經在另一個域上輸出它來檢查它是否抓取正確的值)。也可以有人請說明當你想添加多個實體時你會怎麼做?謝謝。我正在註冊我的插件在預操作(創建)。而插件本身不會引發任何錯誤,但它似乎並沒有在我的新列表中添加任何成員。如果somone能幫助我,我真的很感激。非常感謝你提前。

回答

4

首先,將事件更改爲後期操作,因爲您還沒有創建實體的GUID,事實上,您也沒有實體本身,這就是爲什麼它被稱爲預操作。 要添加多個實體試圖在代碼中傳遞的GUID陣列狀波紋管:

// Setup the CrmConnection and OrganizationService instances 
    CrmConnectionInstance = new CrmConnection(ConfigurationConstants.CrmConnectionName); 
OrgServiceInstance = new OrganizationService(CrmConnectionInstance); 
    // Create the marketing list 
    Guid NewMarketingListId = Guid.Empty; 
    Microsoft.Xrm.Sdk.Entity CurrentList = new Microsoft.Xrm.Sdk.Entity(MarketingListConstants.MarketingListEntityName); 
    CurrentList[MarketingListConstants.MarketingListTypeAttribute] = false; 
    CurrentList[MarketingListConstants.ListNameAttribute] = "NameOfList"; 
    // For contacts, a value of 2 should be used. 
    CurrentList[MarketingListConstants.CreatedFromCodeAttribute] = new OptionSetValue(2); 
    // Actually create the list 
    NewMarketingListId = OrgServiceInstance.Create(CurrentList); 
    // Use the AddListMembersListRequest to add the members to the list 
    List<Guid> MemberListIds = new List<Guid>(); 
    // Now you'll need to add the Guids for each member to the list 
    // I'm leaving that part out as adding values to a list is very basic. 
    AddListMembersListRequest AddMemberRequest = new AddListMembersListRequest(); 
    AddMemberRequest.ListId = NewMarketingListId; 
    AddMemberRequest.MemberIds = memberIds.ToArray(); 
    // Use AddListMembersListReponse to get information about the request execution 
    AddListMembersListResponse AddMemberResponse = OrgServiceInstance.Execute(AddMemberRequest) as AddListMembersListResponse; 
+0

嗨格里戈裏,非常感謝你爲你的解決方案爲我工作得很好:-)。只是爲了讓你知道我犯了一個非常愚蠢的錯誤,我抓住listmemberid Guid而不是entityid Guid,這就是爲什麼我的記錄從未插入,但現在它的工作正常。再次感謝,非常感謝您的努力。 –

相關問題