CRM 2011插件不会更新并从ActivityParty获取电子邮件
问题描述:
我试图从列表中的电子邮件中获取所有电子邮件(到,从,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");
}
}
答
尝试设置的功能ParseAddressUsed
到EntityCollection
,而不是IEnumerable<ActivityParty>
第一个参数类型,并做必要的改变。
而且在功能UpdateToggleState
最后的更新,也没有必要建立一个新的电子邮件实体(Entity email = new Entity("email");
),当你已经拥有的实体变量。您可以只设置new_clientfacing
属性并更新已检索的实体。
答
在你的方法ParseAddressUsed
要添加PartyId GUID字符串列表,您在emailaddress1过滤器作为参数,用它LookupContact
,这可能是为什么你不检索任何记录的原因。
请尝试更改addressStrings.Add(party.PartyId.Id.ToString())
为addressStrings.Add(party.AddressUsed)
而不是看看是否有效。
干杯,dimamura
对不起,但我是新的CRM 2011插件。我将第一个参数更改为“EntityCollection”,现在我在ParseAddressUsed函数中出现错误。如何将entity.From转换为EntityCollection? 另外我尝试更新** UpdateToggleState **函数中的实体变量使用我原来声明的变量。但是,这仍然给了我同样的错误。 – anis0315 2013-02-23 19:27:10
你需要调用** ParseAddressUsed **函数,像这样'ParseAddressUsed((EntityCollection)entity.From,trace)' – amartine 2013-02-23 20:42:10
当我这样做,我得到一个intellisense错误说:'不能转换'System.Collection.Generic。 IEnumerable'到'Microsoft.Xrm.Sdk.EntityCollection'' –
anis0315
2013-02-24 23:45:36