Dynamics 365副网格记录上的重复检测插件创建

发布于 2025-01-19 03:34:04 字数 3237 浏览 6 评论 0原文

希望有人可以帮助我,我已经为此苦苦挣扎了几个小时。

我正在创建一个插件,以在实体 A 记录(案例)的子网格上创建名为未来产品(实体 B)的自定义实体记录。

EntityA 是与自定义 EntityB 具有 1:N 关系的 Case。实体 B 与实体 C(产品类别)也具有 M:1 关系。

总共有 3 个实体:案例、未来产品和最后一个产品类别。

我的插件是在创建 EntityB 时注册的,它具有多对一的案例(实体 A)。

该插件需要检测添加到案例(实体 A)上的子网格中的重复记录,但是这些记录位于实体 B(未来产品)上。

我的挑战是 EntityB 有一个名为产品类别的字段,它是对 EntityC 的查找,我想比较用户添加到 Case 上已有列表的上下文中的值,如果存在具有相同查找名称的匹配项,然后抛出一个新错误,表示它检测到添加了重复记录。

我的代码无法正常工作,并且在添加任何产品类别时出现异常,而不是在找到的重复项上出现异常。

请参阅下面的 C# 插件代码

// The InputParameters collection contains all the data passed in the message request.  
        if (context.InputParameters.Contains("Target") &&
            context.InputParameters["Target"] is Entity)
        {
            // Obtain the target entity from the input parameters.  
            Entity entity = (Entity)context.InputParameters["Target"];

            // Obtain the organization service reference which you will need for  
            // web service calls.  
            IOrganizationServiceFactory serviceFactory =
                (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService organizationService = serviceFactory.CreateOrganizationService(context.UserId);


            try
            {
                if (entity.Contains("mm_application"))
                {
                    tracingService.Trace($"Application Case- {(EntityReference)entity["mm_application"]}");

                    var query = new QueryExpression("mm_futureproductcategories");
                    query.ColumnSet.AddColumns("mm_application", "mm_productcategory");



                    //Get contect Entity ID
                    Entity futureProductRecord = organizationService.Retrieve(entity.LogicalName, entity.Id, new ColumnSet(true));

                    //Get Related Case application lookup
                    EntityReference relatedCase = futureProductRecord.GetAttributeValue<EntityReference>("mm_application");
                    Entity caseRecord = organizationService.Retrieve("incident", relatedCase.Id, new ColumnSet(true));

                    //Get Related Product Category lookup
                    EntityReference relatedProductCategory = futureProductRecord.GetAttributeValue<EntityReference>("mm_productcategory");
                    Entity productCategoryRecord = organizationService.Retrieve("mm_productcategory", relatedProductCategory.Id, new ColumnSet(true));
                    

                    query.Criteria.AddCondition("mm_application", ConditionOperator.Equal, caseRecord.Id);
                    query.Criteria.AddCondition("mm_productcategory", ConditionOperator.Equal, productCategoryRecord.Id);

                    EntityCollection futureProductCatRecords = organizationService.RetrieveMultiple(query);

                    if (futureProductCatRecords.Entities.Count > 0)
                    {
                        throw new InvalidPluginExecutionException($"Duplicate Future Product Category detected.");
                    }


                   
                }
            }

该插件总是会遇到异常,即使添加的未来产品类别尚未位于案例记录的子网格中也是如此。我可能错过了一些东西,如果可以的话请帮忙,我已经为此奋斗了一整天。

hope someone can help me I am been struggling with this for a few hours now.

I am creating a Plugin to Create a custom entity record called Future Products(Entity B) on a Subgrid of an EntityA record(Case).

EntityA is Case that has a 1:N relationship with custom EntityB. Entity B also has an M:1 relationship with Entity C(Product Category).

In total there are 3 entities, Case, Future Products, and the last Product Category.

My plugin is registered on creation of EntityB, which has many to one Case(Entity A).

The plugin needs to detect duplicate records added to the SubGrid which is on Case(Entity A), however, the records sit on Entity B(future products).

My challenge is that EntityB has a field called product category, which is a lookup to EntityC, I want to compare the value in the context user is adding to the list already on Case, and if there is a match with the same lookup name, then throw a new error to say it detected duplicate record being added.

My code is not working, and an exception is hit when adding any Product Category and not on the duplicate found.

See c# plugin code below

// The InputParameters collection contains all the data passed in the message request.  
        if (context.InputParameters.Contains("Target") &&
            context.InputParameters["Target"] is Entity)
        {
            // Obtain the target entity from the input parameters.  
            Entity entity = (Entity)context.InputParameters["Target"];

            // Obtain the organization service reference which you will need for  
            // web service calls.  
            IOrganizationServiceFactory serviceFactory =
                (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService organizationService = serviceFactory.CreateOrganizationService(context.UserId);


            try
            {
                if (entity.Contains("mm_application"))
                {
                    tracingService.Trace(
quot;Application Case- {(EntityReference)entity["mm_application"]}");

                    var query = new QueryExpression("mm_futureproductcategories");
                    query.ColumnSet.AddColumns("mm_application", "mm_productcategory");



                    //Get contect Entity ID
                    Entity futureProductRecord = organizationService.Retrieve(entity.LogicalName, entity.Id, new ColumnSet(true));

                    //Get Related Case application lookup
                    EntityReference relatedCase = futureProductRecord.GetAttributeValue<EntityReference>("mm_application");
                    Entity caseRecord = organizationService.Retrieve("incident", relatedCase.Id, new ColumnSet(true));

                    //Get Related Product Category lookup
                    EntityReference relatedProductCategory = futureProductRecord.GetAttributeValue<EntityReference>("mm_productcategory");
                    Entity productCategoryRecord = organizationService.Retrieve("mm_productcategory", relatedProductCategory.Id, new ColumnSet(true));
                    

                    query.Criteria.AddCondition("mm_application", ConditionOperator.Equal, caseRecord.Id);
                    query.Criteria.AddCondition("mm_productcategory", ConditionOperator.Equal, productCategoryRecord.Id);

                    EntityCollection futureProductCatRecords = organizationService.RetrieveMultiple(query);

                    if (futureProductCatRecords.Entities.Count > 0)
                    {
                        throw new InvalidPluginExecutionException(
quot;Duplicate Future Product Category detected.");
                    }


                   
                }
            }

The plugin always hits the exception, even when Future Products Category added is not already in the subgrid on the case records. I may be missing something, please help if you can I have struggled with this for the whole day now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄暮涼年 2025-01-26 03:34:04

在更多谷歌搜索的帮助下,我最终弄清楚了,所以我上面的代码是不正确的,而且它是在操作后创建而不是操作前运行的。

我通过从插件上下文中已有的实体获取查找值来解决这个问题。请参阅我更新的代码,了解如何检查子网格创建上的重复项。

try
            {
                if (entity.Contains("mm_productcategory"))
                {


                    tracingService.Trace($"Application Case- {(EntityReference)entity["mm_application"]}");
                    // Instantiate QueryExpression query
                    var query = new QueryExpression("mm_futureproductcategories");

                    // Add columns to query.ColumnSet
                    query.ColumnSet.AddColumns("mm_application", "mm_productcategory");

                    // Define filter query.Criteria
                    if (entity.Contains("mm_productcategory"))
                    {
                        tracingService.Trace($"Application Case- {(EntityReference)entity["mm_productcategory"]}");
                        query.Criteria.AddCondition("mm_productcategory", ConditionOperator.Equal, entity.GetAttributeValue<EntityReference>("mm_productcategory").Id);
                    }

                    if (entity.Contains("mm_application"))
                    {
                        query.Criteria.AddCondition("mm_application", ConditionOperator.Equal, entity.GetAttributeValue<EntityReference>("mm_application").Id);
                    }
                    

                    EntityCollection futureProductCatRecords = organizationService.RetrieveMultiple(query);

                    if (futureProductCatRecords.Entities.Count > 0)
                    {
                        throw new InvalidPluginExecutionException($"Duplicate Future Product Category detected.");
                    }



                }
            }

此代码将检查是否有任何重复的未来产品,如果找到则抛出异常。

I ended up figuring it out, with the help of some more googling, So my above code was incorrect, and also it was running on Post Operation Create instead of Pre Operation.

I solved it by getting the lookup values from the entity which is already in the context of the plugin. See my updated code on how to check duplicates on a Subgrid create.

try
            {
                if (entity.Contains("mm_productcategory"))
                {


                    tracingService.Trace(
quot;Application Case- {(EntityReference)entity["mm_application"]}");
                    // Instantiate QueryExpression query
                    var query = new QueryExpression("mm_futureproductcategories");

                    // Add columns to query.ColumnSet
                    query.ColumnSet.AddColumns("mm_application", "mm_productcategory");

                    // Define filter query.Criteria
                    if (entity.Contains("mm_productcategory"))
                    {
                        tracingService.Trace(
quot;Application Case- {(EntityReference)entity["mm_productcategory"]}");
                        query.Criteria.AddCondition("mm_productcategory", ConditionOperator.Equal, entity.GetAttributeValue<EntityReference>("mm_productcategory").Id);
                    }

                    if (entity.Contains("mm_application"))
                    {
                        query.Criteria.AddCondition("mm_application", ConditionOperator.Equal, entity.GetAttributeValue<EntityReference>("mm_application").Id);
                    }
                    

                    EntityCollection futureProductCatRecords = organizationService.RetrieveMultiple(query);

                    if (futureProductCatRecords.Entities.Count > 0)
                    {
                        throw new InvalidPluginExecutionException(
quot;Duplicate Future Product Category detected.");
                    }



                }
            }

This code will check if there are any duplicate Future Products and then throw an exception if one is found.

め七分饶幸 2025-01-26 03:34:04

为此,您需要检查当前记录是否未返回。

您需要添加行:

 query.Criteria.AddCondition(mm_futureproductcategories, ConditionOperator.NotEqual, futureProductRecord.Id);

或者您可以将最终条件更改为:

if (futureProductCarRecords.Entities.Count > 1){}

For this to work, you need to check that the current record isn't returned.

You need to add the line:

 query.Criteria.AddCondition(mm_futureproductcategories, ConditionOperator.NotEqual, futureProductRecord.Id);

Or you could just change your final condition to:

if (futureProductCarRecords.Entities.Count > 1){}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文