2013-02-23 39 views
1

我試圖從列表中的電子郵件中獲取所有電子郵件(到,從,cc),並通過列表並檢查聯繫人,如果聯繫人存在在CRM中,電子郵件實體上的一個字段將被標記爲true。當我檢查電子郵件的「發件人」,「發件人」和「抄送」字段時,它將返回0方,但在那裏沒有錯誤。同樣在最後,當我調用service.Update(entity)時,它會返回一個錯誤。 An unexpected error occurred.CRM 2011插件不會更新並從ActivityParty獲取電子郵件

public void Execute(IServiceProvider serviceProvider) 
{ 
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider 
    .GetService(typeof(IPluginExecutionContext)); 
    IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider 
    .GetService(typeof(IOrganizationServiceFactory)); 
    IOrganizationService service = factory 
    .CreateOrganizationService(context.UserId); 

    try 
    { 
    Email entity; 
    if (context.MessageName == "Create") 
    { 
    if (context.PostEntityImages.Contains("PostImage") 
     && context.PostEntityImages["PostImage"] is Entity) 
     entity = (Email)context.PostEntityImages["PostImage"].ToEntity<Email>(); 
    else 
     throw new Exception("No PostEntityImages..."); 
    } 
    else 
    throw new Exception("EmailPortalVisibilityPlugin Plugin invalid"); 

    if(entity.LogicalName != "email") 
    throw new Exception("EmailPortalVisibilityPlugin invalid"); 

    bool contactExists = false; 
    List<string> emails = new List<string>(); 
    emails.AddRange(ParseAddressUsed(entity.To, trace)); 
    emails.AddRange(ParseAddressUsed(entity.From, trace)); 
    emails.AddRange(ParseAddressUsed(entity.Cc, trace)); 
    foreach (String em in emails) 
    { 
    contactExists = LookupContact(em, service, trace); 
    if (contactExists) 
     break; 
    } 
    UpdateToggleState(entity, contactExists, service, trace); 
    } 
    catch (Exception ex) 
    { 
    throw new InvalidPluginExecutionException("Execute '" + ex.Message + "'"); 
    } 
} 

public List<string> ParseAddressUsed(
    IEnumerable<ActivityParty> entity, ITracingService trace) 
{ 
    try 
    { 
    List<string> addressStrings = new List<string>(); 
    foreach (ActivityParty party in entity) 
     addressStrings.Add(party.PartyId.Id.ToString()); 
    return addressStrings; 
    } 
    catch (FaultException<OrganizationServiceFault> exceptionServiceCall) 
    { 
    throw new Exception("ParseAddressUsed FaultException"); 
    } 
    catch (Exception ex) 
    { 
    throw new Exception("ParseAddressUsed Exception"); 
    } 
} 

public bool LookupContact(
    String emailAddress, IOrganizationService service, ITracingService trace) 
{ 
    try 
    { 
    QueryByAttribute queryByAttribute = new QueryByAttribute("contact"); 
    queryByAttribute.ColumnSet = new ColumnSet("contactId"); 
    queryByAttribute.Attributes.Add("emailaddress1"); 
    queryByAttribute.Values.Add(emailAddress); 
    EntityCollection retrieved = service.RetrieveMultiple(queryByAttribute); 

    return (retrieved.Entities.Count > 0); 
    } 
    catch (FaultException<OrganizationServiceFault> exceptionServiceCall) 
    { 
    throw new Exception("LookupContact Exception"); 
    } 
    catch (Exception ex) 
    { 
    throw new Exception("LookupContact Exception"); 
    } 
} 

public void UpdateToggleState(
    Email entity, bool toggleState, IOrganizationService service, ITracingService trace) 
{ 
    try 
    { 
    Entity email = new Entity("email"); 
    email.Id = entity.Id; 
    email.Attributes.Add("new_clientfacing", toggleState); 
    service.Update(email); 
    } 
    catch (FaultException<OrganizationServiceFault> exceptionServiceCall) 
    { 
    throw new Exception("UpdateToggleState Exception"); 
    } 
    catch (Exception ex) 
    { 
    throw new Exception("UpdateToggleState Exception"); 
    } 
} 

回答

0

嘗試設置的功能ParseAddressUsedEntityCollection,而不是IEnumerable<ActivityParty>第一個參數類型,並做必要的改變。

而且在功能UpdateToggleState最後的更新,也沒有必要建立一個新的電子郵件實體(Entity email = new Entity("email");),當你已經擁有的實體變量。您可以只設置new_clientfacing屬性並更新已檢索的實體。

+0

對不起,但我是新的CRM 2011插件。我將第一個參數更改爲「EntityCollection」,現在我在ParseAddressUsed函數中出現錯誤。如何將entity.From轉換爲EntityCollection? 另外我嘗試更新** UpdateToggleState **函數中的實體變量使用我原來聲明的變量。但是,這仍然給了我同樣的錯誤。 – anis0315 2013-02-23 19:27:10

+0

你需要調用** ParseAddressUsed **函數,像這樣'ParseAddressUsed((EntityCollection)entity.From,trace)' – amartine 2013-02-23 20:42:10

+0

當我這樣做,我得到一個intellisense錯誤說:'不能轉換'System.Collection.Generic。 IEnumerable '到'Microsoft.Xrm.Sdk.EntityCollection'' – anis0315 2013-02-24 23:45:36

0

在你的方法ParseAddressUsed要添加PartyId GUID字符串列表,您在emailaddress1過濾器作爲參數,用它LookupContact,這可能是爲什麼你不檢索任何記錄的原因。

請嘗試更改addressStrings.Add(party.PartyId.Id.ToString())addressStrings.Add(party.AddressUsed)而不是看看是否有效。

乾杯,dimamura

相關問題