简体   繁体   English

在创建实体时编辑CRM实体。 CRM Dynamics插件

[英]Edit a CRM Entity upon entity creation. CRM Dynamics Plugins

I have a CRM custom plugin that is registered (via CRM Plugin Registration Tool) on the event Create Job. 我有一个CRM自定义插件,在活动创建作业上注册(通过CRM插件注册工具)。 'Create' being the message and 'job' being the Primary Entity. “创建”是主要实体的消息和“工作”。

Upon creation of a new job, I want to take that entity and automatically assign it a project number. 创建新作业后,我想获取该实体并自动为其分配项目编号。 I always set the 'Event Pipeline Stage of Execution' to be Post-Operation. 我总是将'事件管道执行阶段'设置为后操作。 I have tried both Execution Modes (Asynchronous and Synchronous). 我尝试了两种执行模式(异步和同步)。

Asynchronous always throws me an error along the lines of "Entity job with ID '' does not exist" 异步总是抛出一个错误, "Entity job with ID '' does not exist"

Synchronous never throws an error but none of the code within my tool is being performed. 同步永远不会抛出错误,但我的工具中没有任何代码正在执行。

    public void Execute(IServiceProvider serviceProvider)
    {
        var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        var orgService = factory.CreateOrganizationService(null);
        var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));        
        Entity ent = (Entity)context.InputParameters["Target"];

        IOrganizationService service = factory.CreateOrganizationService(null);
                    if (ent.LogicalName == "cmc_job")
        {
            try
            {                   
                ent["cmc_jobnumber"] = "0000001";
                ent["cmc_name"] += " - DEMO";
                service.Update(ent);                    
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
            }

    }

I have also tried service.Create(entity) as well but I tend to run into errors with that as well. 我也尝试过service.Create(entity) ,但我也倾向于遇到错误。 These errors often relate to having duplicate records. 这些错误通常与重复记录有关。 Also I have made sure to deactivate any existing processes that relate to creating jobs. 此外,我确保停用与创建作业相关的任何现有流程。

How can I properly update an entity field immediately after creating the entity? 如何在创建实体后立即正确更新实体字段? Which practice is best? 哪种做法最好?

Side Note : The reason I am deciding to use a CRM Custom Plugin and not a custom Processes is because I need to query out the largest existing project number and then adding 1 to it. 旁注 :我决定使用CRM自定义插件而不是自定义进程的原因是因为我需要查询最大的现有项目编号,然后再添加1。

For an auto-numbering plugin, the best practice is to register on Pre-Operation (Synchronous). 对于自动编号插件,最佳做法是在预操作(同步)上注册。 This way the auto-numbered fields will be set within the same database transaction that creates the record (avoiding unnecessary transactions and messy audit history). 这样,自动编号字段将在创建记录的同一数据库事务中设置(避免不必要的事务和杂乱的审计历史记录)。

When writing pre-operation plugins, you should not call service.Update(), but simply set the values on the target (as you currently are), and they will be persisted along with the other attributes of the target. 在编写预操作插件时,不应该调用service.Update(),而只需在目标上设置值(就像当前一样),它们将与目标的其他属性一起保留。 Comment out your service.Update() line and your plugin should work on pre-operation. 注释掉你的service.Update()行和你的插件应该在pre-operation上工作。

Asynchronous always throws me an error along the lines of "Entity job with ID '' does not exist 异步总是抛出一个错误,就像“具有ID的实体作业”不存在一样

Synchronous never throws an error but none of the code within my tool is being performed 同步永远不会抛出错误,但我的工具中没有任何代码正在执行

This happens because during create an ID is not assigned to records until they are persisted to the database. 发生这种情况是因为在创建期间,ID不会分配给记录,直到它们持久保存到数据库。 You are taking the Target (which has no ID) and then attempting to perform a service.Update() which expects an entity with an ID. 您正在获取Target(没有ID),然后尝试执行service.Update(),该服务需要具有ID的实体。 Both sync and async calls will throw an error, but the async error happens in the background and you don't see it. 同步和异步调用都会引发错误,但异步错误会在后台发生,而您却看不到它。

You have to recheck with asynchronous step registration in PRT. 您必须在PRT中重新检查异步步骤注册。

Whenever you are accessing the just created record Id in post message pipeline, sync plugin will fail as the DB transaction is not yet committed. 每当您在发布消息管道中访问刚创建的记录ID时,同步插件将失败,因为尚未提交数据库事务。

But asynchronous plugin will succeed as the DB transaction is committed & record by Id is accessible. 但异步插件将成功,因为提交数据库事务并且可以访问Id的记录。

Anyway the best practice is set the needed attribute value in pre-operation itself to avoid another explicit update service call. 无论如何,最佳实践是在预操作本身中设置所需的属性值,以避免另一个显式更新服务调用。 You can find lot of similar accepted answers with the same suggestion in SO itself, also in Dynamics community & internet blogs. 您可以在SO本身中找到许多类似的已接受答案 ,也可以在Dynamics社区和互联网博客中找到相同的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM