0
votes

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

1
Is lookup new_productname filled when the plugin is triggered in the pre-operation create stage?Henk van Boeijen

1 Answers

1
votes

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.