【问题标题】:Dynamics CRM custom workflow before entity deletion删除实体之前的 Dynamics CRM 自定义工作流程
【发布时间】:2016-05-04 15:17:08
【问题描述】:

我正在尝试在 Dynamics CRM 上创建自定义工作流程。当另一个实体被删除时,我需要删除一些实体。

我创建了我的class library,并使用以下代码检索了已删除实体的Guid

    protected override void Execute(CodeActivityContext executionContext)
    {
        ITracingService tracingService = executionContext.GetExtension<ITracingService>();

        IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
        IOrganizationServiceFactory serviceFactory =
            executionContext.GetExtension<IOrganizationServiceFactory>();
        IOrganizationService service =
            serviceFactory.CreateOrganizationService(context.UserId);

        mService = service;
        mExecutionContext = executionContext;

        Guid myTipologyTypeDeleted = context.PrimaryEntityId;
        bool isReading = context.PrimaryEntityName.Equals(new_tipologialettura_richiesta.EntityLogicalName);
        bool isMaintenance = context.PrimaryEntityName.Equals(new_tipologiamanutenzionerichiesta.EntityLogicalName);

        bool myResult = AddOnIntervention(isReading, isMaintenance, myTipologyTypeDeleted);


        // Retrieve the summands and perform addition
        result.Set(executionContext, myResult);
    }

这里一切正常,我得到了Guid,我得到了type(阅读或维护)。

我的问题是当我尝试使用此代码检索实体时(相同的代码在另一个从创建记录开始的工作流中运行良好,但记录删除给我错误)。

            Entity myReadingEntity = mService.Retrieve(new_tipologialettura_richiesta.EntityLogicalName, myTipologyTypeDeleted, new ColumnSet(true));

这里我得到一个异常,说没有找到类型为 MyType 且 ID 为 myId 的记录。

我检查了记录,它仍然存在于数据库中,所以它没有被删除。我做错了什么?

谢谢

【问题讨论】:

  • 1) 如果您尝试删除正在删除的记录的子记录,您是否考虑过更改关系属性以自动删除记录? 2) 检索失败的调用...您是否尝试检索当前正在删除的记录?您是否需要检索它以便检查其上的值? 3)你说你检查了数据库的记录并且它仍然存在,你什么时候做那个检查?如果您在工作流完成运行后检查但工作流失败,则平台可能已回滚删除操作。
  • @Polshgiant 感谢您的回复!我在这里回复:1)它不是孩子,只与另一个工作流的操作有关。 2) 我在这个实体中有两个 entityRef,它指的是我在操作中需要的两个实体,我怎样才能在不失败的情况下检索整个记录? 3)可能是的,正如你所说,这是一个回滚
  • 我希望平台可以让您检索当前正在删除的记录,但如果没有,那么您可以让您的自定义 wf 程序集接受更多输入参数,然后在 wf 设计器中您可以传入值从被删除的记录中。这有意义吗?
  • 此工作流何时运行?前置、后置或异步。另外,为什么这是在工作流而不是插件中完成的?

标签: c# workflow dynamics-crm-2013


【解决方案1】:

我认为在这里编写您的自定义逻辑最好是Plugin,您应该编写一个在

上运行的插件
Message: Delete
Stage:    POST

Post Delete Operation 上注册插件后,您应该添加一个pre-image,该pre-image 将在Post Delete with all the attributes 上可用。最好的做法是在图像中推送所需的数据,而不是发出检索。

取自 MSDN: Registering for pre or post images to access entity attribute values results in improved plug-in performance as compared to obtaining entity attributes in plug-in code through RetrieveRequest or RetrieveMultipleRequest requests.

在您的插件中更改代码行:

Entity myReadingEntity = mService.Retrieve(new_tipologialettura_richiesta.EntityLogicalName, myTipologyTypeDeleted, new ColumnSet(true));

if (context.PreEntityImages.Contains("YourImageName"))
{
  Entity myReadingEntity = context.PreEntityImages["YourImageName"]
}

【讨论】:

    猜你喜欢
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多