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

Plugins 编写在字段更新时触发的CRM插件,plugins,dynamics-crm,crm,dynamics-crm-2013,Plugins,Dynamics Crm,Crm,Dynamics Crm 2013,我们使用的是CRM 2013内部部署。我正在编写一个插件,当Quote实体上的字段被更新时触发 所以我在“更新”消息上注册了我的插件。然后事件是“操作后”。(我试过手术前,但仍然没有成功) 基本上,目标是在更新字段时,创建一个新实体“ContractShell”,然后在Quote和新创建的“ContractShell”之间创建关系 然而,我的问题是,当字段被更新时,我的插件似乎永远不会启动。我只是简单地在我的代码中添加了一个InvalidPlugineExecutionException,但由于

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

所以我在“更新”消息上注册了我的插件。然后事件是“操作后”。(我试过手术前,但仍然没有成功)

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

然而,我的问题是,当字段被更新时,我的插件似乎永远不会启动。我只是简单地在我的代码中添加了一个InvalidPlugineExecutionException,但由于某些原因,它从未触发过。。。。有什么想法吗?谢谢

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

这是我的密码:

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);
var trace=(ITracingService)serviceProvider.GetService(typeof(ITracingService));
//InputParameters集合包含消息请求中传递的所有数据。
var targetEntity=context.GetParameterCollection(context.InputParameters,“Target”);
if(targetEntity==null)
抛出新的InvalidPluginExecutionException(OperationStatus.Failed,“目标实体不能为null”);
如果(!context.OutputParameters.Contains(“id”))
返回;
Guid QuoteId=(Guid)targetEntity.Attributes[“QuoteId”];
var serviceFactory=(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service=serviceFactory.CreateOrganizationService(context.UserId);
var contractShellEntity=新实体();
contractShellEntity=新实体(“新实体”);
//分配投资组合
if(targetEntity.Attributes.Contains(Schema.Quote.Portfolio))
{
var quotePortfolio=(EntityReference)targetEntity.Attributes[Schema.Quote.Portfolio];
contractShellEntity[Schema.new_ContractShell.PortfolioName]=新的EntityReference(quotePortfolio.LogicalName,quotePortfolio.Id);
}
var contractShellId=service.Create(contractShellEntity);
抛出新的InvalidPlugineExecutionException(OperationStatus.Failed,“我创建了新的合同外壳”);
//在合同外壳程序和新创建的帐户之间创建关系
var quoteContractReferenceCollection=new EntityReferenceCollection();
var quoteContractRelatedEntity=新实体引用
{
Id=contractShellId,
LogicalName=contractShellEntity.LogicalName
};
添加(quoteContractRelatedEntity);
var quoteContractReferenceCollectionRelationship=新关系
{
SchemaName=Schema.new_ContractShell.ContractQuoteRelationship
};
服务关联(“quote”、QuoteId、QUOTEContractReferenceCollectionRelationship、QUOTEContractReferenceCollectionRelationship);

您不仅需要注册插件,还需要注册SDKMessageProcessingStep。此外,您必须在插件中实现Execute方法才能注册它,因此您的代码段中可能缺少代码,或者您的代码就是问题所在。

此外,经过多次检查后,InvalidPlugineExecutionException被嵌套。如果您不知道如何注册插件,则很有可能您没有任何输出参数,因此您的代码会在遇到异常之前实际返回。

您可以很好地展示插件注册的外观。根据您的注册方式它注册后,可能有多种原因导致它似乎没有启动这个插件。错误消息是什么?没有错误消息。插件只是从来没有被启动过。我尝试将我的InvalidPluginExecutionException消息更早地放在代码顶部,但也从来没有被启动过……我尝试了“后期操作”和“预操作”ion'但仍然不走运..你确定你正在实现IPlugin接口的Execute方法并且你的插件已经构建好了吗?也有可能你实际上没有更改_settoactive字段(检查审计历史记录)。为了进行故障排除,请将筛选属性更改为all。此外,我通常对目标实体使用PluginExecutionContext.InputParameters[“Target”],尽管您的方法可能仍然适用于该特定行。您的组织中是否有其他插件工作正常?请在调用用户后验证是否已更改为Admin(用户上下文)感谢Joseph,我确实在我的代码中添加了一个服务“create”和“associate”语句。我也在插件中注册了一个步骤,尝试了“Post-operation”和“Pre-operation”,但仍然没有成功。我尝试将我的InvalidPluginExecutionException消息更早地放在代码的顶部,但也从未被触发过…仍然很难理解你是怎么做到的你确认你的插件从未被解雇?如果它是异步注册的,它将永远不会弹出错误消息。请共享你的插件注册的屏幕截图。你还需要验证,如果你是根据特定的过滤属性注册它,那么其中一个属性正在更改。你的插件注册有很多问题n告诉我们这个问题-但我保证这是注册或代码本身的问题。我在插件步骤中使用了同步。我还在步骤中添加了过滤属性“settoactive”。我无法在评论中添加图片,因此我在上面的问题中添加了图片。非常感谢您的输入