简体   繁体   English

Microsoft Dynamics CRM插件:从查找字段检索属性

[英]Microsoft Dynamics CRM Plugin: Retrieve Attribute from Look Up Field

im new in CRM. 我在CRM中是新的。 I have create two entity : Order and Product. 我创建了两个实体:订单和产品。 On order entity there is look up field that fire to product entity. 在订单实体上,有一个查找字段可触发产品实体。 I try to get productquantity from product through look up field and paste it to a field within the order entity. 我尝试通过查找字段从产品中获取产品数量,然后将其粘贴到订单实体中的字段中。 Here is the code i tried: 这是我尝试的代码:

if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
        {
            Entity entity = (Entity)context.InputParameters["Target"];



            if (entity.Attributes.Contains("new_productname"))
            {
                Entity productreference = service.Retrieve("new_callistyproduct", ((EntityReference)entity["new_productname"]).Id, new ColumnSet(true));

                if (productreference.Attributes.Contains("new_productquantity"))
                {
                    if (entity.Attributes.Contains("new_numberofproduct"))


                       entity["new_numberofproduct"] = productreference.GetAttributeValue<Decimal>("new_productquantity");

                   else

                     entity.Attributes.Add("new_numberofproduct", productreference.GetAttributeValue<Decimal>("new_productquantity"));



                }

            }



        }

I want this plugin work whenever i create a new record. 每当我创建新记录时,我都希望此插件正常工作。 So i register this plugin as Pre-create event. 所以我将此插件注册为Pre-create事件。 But, when i try to create a record. 但是,当我尝试创建记录时。 This plugin did't retrieve value from productquantity field. 该插件未从productquantity字段中获取价值。 So, i tried to run this plugin as Pre-Update event. 因此,我尝试将此插件作为Pre-Update事件运行。 On the record i've create before, i change the lookup value from product A to product B. And its work, the plugin retrieve a product quantity value from product B. 根据我之前创建的记录,我将查找值从产品A更改为产品B。插件的工作是从产品B检索产品数量值。

The question is, what should i do if i want this plugin also work for pre-Create event. 问题是,如果我希望此插件也可以用于创建前事件,该怎么办。

Thanks 谢谢

If you want to update a target entity, and have CRM perform the update for you, you'll have to register your plugin on the Pre-Create or Pre-Update. 如果要更新目标实体,并让CRM为您执行更新,则必须在“创建前”或“更新前”上注册插件。 If you want to do the action on a Post event, you'll need to call Update using the IOrganizationService, just updating the Target won't work. 如果要对Post事件执行操作,则需要使用IOrganizationService调用Update,只是无法更新Target。 You'll also want to be sure you don't create an infinite loop, where an update triggers the plugin, which performs another update that triggers the same plugin, which performs another update... etc. etc. 您还需要确保您不会创建无限循环,在该循环中,更新将触发插件,该循环执行另一个触发相同插件的更新,然后执行另一个更新...等等。

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

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