简体   繁体   中英

Microsoft Dynamics CRM Plugin: Retrieve Attribute from Look Up Field

im new in 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. But, when i try to create a record. This plugin did't retrieve value from productquantity field. So, i tried to run this plugin as Pre-Update event. 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.

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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