编写在更新字段时触发的CRM插件

时间:2015-09-24 22:28:52

标签: plugins dynamics-crm crm dynamics-crm-2013

我们在内部部署CRM 2013。我正在编写一个插件,当Quote实体上的字段更新时会触发。

所以我在“更新”消息上注册了我的插件。然后事件是'后期操作'。 (我试过预操作但仍然没有运气)

基本上,目标是更新字段时,创建一个新实体'ContractShell',然后在Quote和新创建的'ContractShell'之间创建关系。

但是我的问题是当字段更新时,我的插件似乎永远不会触发。我只是简单地在我的代码中放入一个InvalidPluginExecutionException,但由于某种原因它永远不会触发....任何想法?感谢。

以下是我的插件步骤的屏幕截图:

Plugin step

这是我的代码:

var trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

        // The InputParameters collection contains all the data passed in the message request.
        var targetEntity = context.GetParameterCollection<Entity>(context.InputParameters, "Target");

        if (targetEntity == null)
            throw new InvalidPluginExecutionException(OperationStatus.Failed, "Target Entity cannot be null");

        if (!context.OutputParameters.Contains("id"))
            return;

        Guid QuoteId = (Guid)targetEntity.Attributes["quoteid"];

        var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        var service = serviceFactory.CreateOrganizationService(context.UserId);

        var contractShellEntity = new Entity();
        contractShellEntity = new Entity("new_);

        //assign the portfolio
        if (targetEntity.Attributes.Contains(Schema.Quote.Portfolio))
        {
            var quotePortfolio = (EntityReference)targetEntity.Attributes[Schema.Quote.Portfolio];
            contractShellEntity[Schema.new_ContractShell.PortfolioName] = new EntityReference(quotePortfolio.LogicalName, quotePortfolio.Id);
        }

        var contractShellId = service.Create(contractShellEntity);
        throw new InvalidPluginExecutionException(OperationStatus.Failed, "I created New Contract Shell"); 

        //Creating relationship between Contract Shell and the newly created Accounts
        var quoteContractReferenceCollection = new EntityReferenceCollection();
        var quoteContractRelatedEntity = new EntityReference
        {
            Id = contractShellId,
            LogicalName = contractShellEntity.LogicalName
        };
        quoteContractReferenceCollection.Add(quoteContractRelatedEntity);
        var quoteContractReferenceCollectionRelRelationship = new Relationship
        {
            SchemaName = Schema.new_ContractShell.ContractQuoteRelationship
        };
        service.Associate("quote", QuoteId, quoteContractReferenceCollectionRelRelationship, quoteContractReferenceCollection);

1 个答案:

答案 0 :(得分:0)

您不仅需要注册插件,还需要注册SDKMessageProcessingStep。此外,您必须在插件中实现Execute方法才能注册它,因此您要么丢失代码段中的代码,要么代码就是问题。
此外,您的InvalidPluginExecutionException在多次检查后嵌套。如果您不知道如何注册插件,那么您很可能没有任何输出参数,因此您的代码实际上会在您遇到异常之前返回。