1

我正在尝试从列表中的电子邮件中获取所有电子邮件(收件人、发件人、抄送)并浏览列表并检查联系人,如果联系人存在于 CRM 中,则电子邮件实体上的字段将被标记为 true。当我检查电子邮件的 to、from 和 cc 字段时,它返回 0 方,但那里没有错误。最后,当我调用 service.Update(entity) 时,它返回一个错误。An unexpected error occurred.

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");
  }
}
4

2 回答 2

0

在您的方法ParseAddressUsed中,您将 PartyId GUID 添加到字符串列表中,并LookupContact在 emailaddress1 过滤器中使用它作为参数,这可能是您没有检索任何记录的原因。

请尝试更改addressStrings.Add(party.PartyId.Id.ToString())addressStrings.Add(party.AddressUsed),看看是否有效。

干杯,二村

于 2013-02-25T09:35:43.320 回答
0

尝试将函数的第一个参数类型设置ParseAddressUsedEntityCollection而不是IEnumerable<ActivityParty>,并进行必要的更改。

并且对于函数的最终更新,当您已经拥有实体变量时,UpdateToggleState无需创建新的电子邮件实体( )。Entity email = new Entity("email");您可以只设置new_clientfacing属性并更新已检索到的实体。

于 2013-02-23T08:49:40.987 回答