CRM 2011自定义工作流程

时间:2013-02-20 01:13:01

标签: plugins workflow dynamics-crm-2011 crm workflow-activity

  

我开始工作了,我在这里修改了工作代码。希望它有所帮助!!

我正在创建一个工作流程,其中包含一个插件,用于从电子邮件记录的“FROM”字段中检索联系人实体。我正在尝试查看联系人实体中是否存在该电子邮件。输入是“FROM”电子邮件,输出将返回Contacts实体。我无法使这段代码正常工作,我收到了不同的错误或没有错误,但我知道它不起作用。请帮忙!提前谢谢!

using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using System;

namespace GetSenderPlugin
{
  public class GetSenderPlugin : CodeActivity
  {
    protected override void Execute(CodeActivityContext executionContext)
    {
        //Create the tracing service
        ITracingService trace = executionContext.GetExtension<ITracingService>();
        trace.Trace("*****Tracing Initiated*****");

        //Create the IWorkflowContext and the IOrganizationService for communication with CRM
        IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
        IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
        trace.Trace("*****IOrganizationService created*****");

        trace.Trace("*****Entity logical Name: " + Email.Get<EntityReference>(executionContext).LogicalName + "*****");
        trace.Trace("*****Entity ID: " + Email.Get<EntityReference>(executionContext).Id + "*****");

        if (Email != null && string.Compare(Email.Get<EntityReference>(executionContext).LogicalName, "email", false) == 0)
        {
            EntityReference retrieveEmail = new EntityReference();
            retrieveEmail.Id = Email.Get<EntityReference>(executionContext).Id;
            retrieveEmail.LogicalName = Email.Get<EntityReference>(executionContext).LogicalName;
            retrieveEmail.Name = Email.Get<EntityReference>(executionContext).Name;

            string[] strArrays = new string[1];
            strArrays[0] = "from";

            ColumnSet columnSet = new ColumnSet();
            columnSet.AddColumn(strArrays[0]);

            RetrieveRequest retrieveRequest = new RetrieveRequest();
            retrieveRequest.Target = retrieveEmail;
            retrieveRequest.ColumnSet = columnSet;
            trace.Trace("*****Retrieve Request declared*****");

            RetrieveResponse retrieveResponse = (RetrieveResponse)service.Execute(retrieveRequest);
            trace.Trace("*****Retrieve Response executed*****");

            Email businessEntity = (Email)retrieveResponse.Entity;
            trace.Trace("*****businessEnitity retrieved*****");

            //ActivityParty activitypartyArray = (ActivityParty)businessEntity.From.FirstOrDefault;
            foreach (ActivityParty activityParty in businessEntity.From)
            {
                trace.Trace("*****Activity Party Name: " + activityParty.PartyId.LogicalName + "*****");
                trace.Trace("*****Activity Party Id: " + activityParty.PartyId.Id + "*****");
                if (activityParty != null && activityParty != null && activityParty.PartyId != null)
                {
                    string str = activityParty.PartyId.LogicalName;
                    if (str.CompareTo("contact") != 0)
                    {
                        trace.Trace("*****Not Contact*****");
                        if (str.CompareTo("account") != 0)
                        {
                            trace.Trace("*****Not Account*****");
                            if (str.CompareTo("lead") != 0)
                            {
                                trace.Trace("*****Not Lead*****");
                                if (str.CompareTo("systemuser") != 0)
                                {
                                    trace.Trace("*****Not System User*****");
                                    if (str.CompareTo("queue") == 0)
                                    {
                                        Queue.Set(executionContext, activityParty.PartyId);
                                        Contact.Set(executionContext, new EntityReference("contact", Guid.NewGuid()));
                                        User.Set(executionContext, new EntityReference("systemuser", Guid.NewGuid()));
                                        Lead.Set(executionContext, new EntityReference("lead", Guid.NewGuid()));
                                        Account.Set(executionContext, new EntityReference("account", Guid.NewGuid()));
                                    }
                                    else
                                    {
                                        trace.Trace("*****User not found*****");
                                        Queue.Set(executionContext, new EntityReference("queue", Guid.NewGuid()));
                                        Contact.Set(executionContext, new EntityReference("contact", Guid.NewGuid()));
                                        User.Set(executionContext, new EntityReference("systemuser", Guid.NewGuid()));
                                        Lead.Set(executionContext, new EntityReference("lead", Guid.NewGuid()));
                                        Account.Set(executionContext, new EntityReference("account", Guid.NewGuid()));
                                    }
                                }
                                else
                                {
                                    User.Set(executionContext, activityParty.PartyId);
                                    Contact.Set(executionContext, new EntityReference("contact", Guid.NewGuid()));
                                    Queue.Set(executionContext, new EntityReference("queue", Guid.NewGuid()));
                                    Lead.Set(executionContext, new EntityReference("lead", Guid.NewGuid()));
                                    Account.Set(executionContext, new EntityReference("account", Guid.NewGuid()));
                                }
                            }
                            else
                            {
                                Lead.Set(executionContext, activityParty.PartyId);
                                Contact.Set(executionContext, new EntityReference("contact", Guid.NewGuid()));
                                Queue.Set(executionContext, new EntityReference("queue", Guid.NewGuid()));
                                User.Set(executionContext, new EntityReference("systemuser", Guid.NewGuid()));
                                Account.Set(executionContext, new EntityReference("account", Guid.NewGuid()));
                            }
                        }
                        else
                        {
                            Account.Set(executionContext, activityParty.PartyId);
                            Contact.Set(executionContext, new EntityReference("contact", Guid.NewGuid()));
                            Queue.Set(executionContext, new EntityReference("queue", Guid.NewGuid()));
                            User.Set(executionContext, new EntityReference("systemuser", Guid.NewGuid()));
                            Lead.Set(executionContext, new EntityReference("lead", Guid.NewGuid()));
                        }
                    }
                    else
                    {
                        trace.Trace("*****Contact assigned*****");
                        Contact.Set(executionContext, activityParty.PartyId);
                        Queue.Set(executionContext, new EntityReference("queue", Guid.NewGuid()));
                        User.Set(executionContext, new EntityReference("systemuser", Guid.NewGuid()));
                        Lead.Set(executionContext, new EntityReference("lead", Guid.NewGuid()));
                        Account.Set(executionContext, new EntityReference("account", Guid.NewGuid()));
                    }
                }
                break;
            }
        }
        else
        {
            trace.Trace("*****Email is null*****");
            Queue.Set(executionContext, new EntityReference("queue", Guid.NewGuid()));
            Contact.Set(executionContext, new EntityReference("contact", Guid.NewGuid()));
            User.Set(executionContext, new EntityReference("systemuser", Guid.NewGuid()));
            Lead.Set(executionContext, new EntityReference("lead", Guid.NewGuid()));
            Account.Set(executionContext, new EntityReference("account", Guid.NewGuid()));
        }
    }

    #region Properties
    [Input("E-mail")]
    [ReferenceTarget("email")]
    public InArgument<EntityReference> Email { get; set; }

    [Output("Account")]
    [ReferenceTarget("account")]
    public OutArgument<EntityReference> Account { get; set; }

    [Output("Contact")]
    [ReferenceTarget("contact")]
    public OutArgument<EntityReference> Contact { get; set; }

    [Output("Lead")]
    [ReferenceTarget("lead")]
    public OutArgument<EntityReference> Lead { get; set; }

    [Output("Queue")]
    [ReferenceTarget("queue")]
    public OutArgument<EntityReference> Queue { get; set; }

    [Output("User")]
    [ReferenceTarget("systemuser")]
    public OutArgument<EntityReference> User { get; set; }
    #endregion        
  }
}

因此,我们将内部CRM系统从4.0升级到2011年,这是一个工作流程中的插件。我没有原始的源代码,所以我真的不知道代码在做什么。但是在导出解决方案后,我反编译了原始的.dll文件。我试图重写CRM 2011的代码,这就是我所拥有的。在我测试了工作流程之后,我得到错误说:“预期的非空Guid。”代码转到循环后的最后一行然后给我错误。这是跟踪:

Workflow paused due to error: Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Expected non-empty Guid.Detail: 
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
  <ErrorCode>-2147220989</ErrorCode>
  <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
  <Message>Expected non-empty Guid.</Message>
  <Timestamp>2013-02-21T23:46:37.0376093Z</Timestamp>
  <InnerFault>
   <ErrorCode>-2147220970</ErrorCode>
    <ErrorDetails xmlns:d3p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
    <Message>System.ArgumentException: Expected non-empty Guid.
Parameter name: id</Message>
    <Timestamp>2013-02-21T23:46:37.0376093Z</Timestamp>
    <InnerFault i:nil="true" />
    <TraceText i:nil="true" />
 </InnerFault>
 <TraceText>[Microsoft.Xrm.Sdk.Workflow: Microsoft.Xrm.Sdk.Workflow.Activities.RetrieveEntity]
[RetrieveEntity]
    *****Tracing Initiated*****
    *****IOrganizationService created*****
    *****Entity logical Name: email*****
    *****Entity ID: c49e4c7c-8724-de11-86ce-000c290f83d7*****
    *****Retrieve Request declared*****
    *****Retrieve Response executed*****
    *****businessEnitity retrieved*****
    *****Activity Party Name: contact*****
    *****Activity Party Id: 79ed3a33-8eb9-dc11-8edd-00c09f226ebb*****
    *****Activity Party not null*****
    *****Contact assigned*****
    *****foreach ended*****</TraceText>
</OrganizationServiceFault>
   at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Retrieve(String entityName, Guid id, ColumnSet columnSet, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
   at Microsoft.Crm.Extensibility.InprocessServiceProxy.RetrieveCore(String entityName, Guid id, ColumnSet columnSet)
   at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Retrieve(String entityName, Guid id, ColumnSet columnSet)
   at Microsoft.Crm.Workflow.Services.RetrieveActivityService.<>c__DisplayClass1.<RetrieveInternal>b__0(IOrganizationService sdkService)
   at Microsoft.Crm.Workflow.Services.ActivityServiceBase.ExecuteInTransactedContext(ActivityDelegate activityDelegate)
   at Microsoft.Crm.Workflow.Services.RetrieveActivityService.ExecuteInternal(ActivityContext executionContext, RetrieveEntity retrieveEntity)
   at Microsoft.Crm.Workflow.Services.RetrieveActivityService.Execute(ActivityContext executionContext, RetrieveEntity retrieveEntity)
   at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
   at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

2 个答案:

答案 0 :(得分:3)

  

我收到了不同的错误或没有错误,但我知道它不起作用

首先,我建议您列出问题中的错误。回答你并避免-1s会容易得多。 :)其次,在工作流程中使用跟踪服务。

ITracingService tracingService = context.GetExtension<ITracingService>();
tracingService.Trace("I'm tracing something....");

如果找不到指定电子邮件的联系会怎样?你必须处理这个案子。可能它在这里失败了。发布错误日志,检查一下。

答案 1 :(得分:2)

如果我要给出一个嘻哈的答案,我猜这是问题是你在某些情况下没有得到匹配并过滤掉所有内容。然后,你在某处得到 null (或尝试引用一个 guid,或者尝试选择一个尚未输入的属性,这样,虽然已定义,但它是不服务)。

你得到了什么确切的错误?你什么时候得到它们什么时候不能得到它们?

此外,我注意到的代码存在一些问题(不是解决方案,但仍有待改进)。

名字中的错字。我想你想要 ContactReference

[Output("output")]
[ReferenceTarget("contact")]
public OutArgument<EntityReference> ContactRefernce { get; set; }

我以不同的方式设计查询。由于您知道要匹配的地址,因此您可能希望精确地过滤该地址。此外,您似乎只需要基于电子邮件的匹配guid所以您只需要获取它。

private Guid MatchSenderWithExistingContact(
  IOrganizationService service, String fromAddress)
{
  QueryExpression query = new QueryExpression
  {
    EntityName = "contact",
    ColumnSet = new ColumnSet("emailaddress1"),
    Criteria = new FilterExpression
    {
      Filters =
      {
        new FilterExpression
        {
          Conditions =
          {
            new ConditionExpression(
              "emailaddress1", ConditionOperator.Equal, fromAddress)
          }
        }
      }
    }
  };

  EntityCollection retrieveMultipleRequest = service.RetrieveMultiple(query);
  IEnumerable<Entity> entities = retrieveMultipleRequest.Entities;
  return entities.FirstOrDefault().Id;
}

您可能希望将查询声明放在逐步语法上。我更喜欢这种方式,因为它在创建高级查询时更方便。但是,当您迭代并使用 break 时,该部分肯定会更好,如上所示。

快乐的编码!

您可能有理由使用跟踪。通常,我将跟踪对象声明为插件类中的成员变量,然后在每次操作时写入它。这样,至少我知道哪里粪便击中AC并且可以在此之前记录变量的值。

private ITracingService trace;

public void Execute(IServiceProvider serviceProvider)
{
  trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
  _trace.Trace("Tracing successful!");
  throw new Exception("Intentional! Nice.");
}

请记住,除非发生未捕获的异常,否则不会显示跟踪。事实上,我偶尔会为了查看变量中的内容而有意破坏执行。所以请记住,如果你有一个全局 try-catch ,你需要手动重新抛出异常。

此方法的优点是您可以跟踪内部部署和联机。它适用于除异步之外的所有插件注册。也有解决办法,但这不是你问题的主题,我已经离题了。