web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Fire-up Post Create plugin for Order Product

(0) ShareShare
ReportReport
Posted on by

Hello,

In CRM online, in Product and Order Product entities i a have added a custom field. I complete only in Product records. When i create an order from a quantity i need to copy this value into newly created Order Products. Also, when i add an existing product to an order, i want to copy that value from product into the corresponding field in Order Product.

I have created a plug-in to autocomplete that value in order product in post-create, but it doesn't work, this event does not fire.

Is there another way to do this?

*This post is locked for comments

I have the same question (0)
  • Mahendar Pal Profile Picture
    45,095 on at

    Can you share your registration screenshot and code, so that someone can check it ??

  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi,

    I'd suggest changing it to a Pre-Create plugin, that way you can set the value on the way to the database (in a post plugin you would have to save the record again and it's wasteful). If you register the plugin according to the steps here you should be good to go: msdn.microsoft.com/.../gg328263.aspx

    Hope this helps! I'd appreciate if you'd mark this as Answering your question.

    Thanks,

     Aiden

  • Community Member Profile Picture
    on at

    Hi Aiden,

    I've tried with Pre-Create, but it does not work.

  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Pre-Create is still the way to go. Can you share your code and plugin configuration details?

    Thanks,

     Aiden

  • Community Member Profile Picture
    on at

    This is the code:

    public void Execute(IServiceProvider serviceProvider)

           {

               Microsoft.Xrm.Sdk.IPluginExecutionContext context;

               IOrganizationServiceFactory factory;

               IOrganizationService service;

               context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

               factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

               service = factory.CreateOrganizationService(context.UserId);

               Entity entity = (Entity)context.InputParameters["Target"];

               if (entity.LogicalName != "salesorderdetail")

                   return;      

               decimal prod_vat = 0;

               //get order product record:

               QueryExpression op_query = new QueryExpression();

               op_query.EntityName = "salesorderdetail";

               op_query.ColumnSet = new ColumnSet { AllColumns = true };

               Entity op = service.Retrieve("salesorderdetail", entity.Id, op_query.ColumnSet);

               //get product guid:

               Guid prod_guid = Guid.Empty;

               if (op.Contains("productid"))

               {

                   if (op.Attributes["productid"] != null)

                   {                    

                       EntityReference productLookup = (EntityReference)op.Attributes["productid"];

                       prod_guid = productLookup.Id;                  

                   }

               }            

               QueryExpression product_query = new QueryExpression();

               product_query.EntityName = "product";

               product_query.ColumnSet = new ColumnSet(new string[] { "productid", "sftl_vatpercent" });

               Entity product = service.Retrieve("product", prod_guid, product_query.ColumnSet);

    //get product vat percent

               if (product.Contains("sftl_vatpercent"))

               {

                   if (product.Attributes["sftl_vatpercent"] != null)

                   {

                       prod_vat = (decimal)product.Attributes["sftl_vatpercent"];

                   }

               }          

               if (prod_vat > 0)

               {

                   op.Attributes["sftl_vatpercent"] = prod_vat;                

                  service.Update(op);

               }

           }

  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    The code generally looks fine (though if it's in the PreCallout, you will need to remove the Update since you set the value on the entity on the way to the database).

    Can you add logging to the begging of the method to make sure you're getting into the method with the data you expect?

    Thanks,

     Aiden

  • Community Member Profile Picture
    on at

    I have removed the line with update. If i try to add a product to order i get this error : "The request record was not found or you do not have sufficient permission to view it".

  • Community Member Profile Picture
    on at

    In my plugin registration tool step i have the following data:

    - Message : Create

    - Primary Entity : salesorderdetail

    - Execution mode : synchronous

    - Event pipeline stage of execution :

             - Pre-validation -> result: order product record is created, custom field is null

             - Pre-operation -> result: order product is not created + error message "The request record was not found or you do not have sufficient permission to view it"

             - Post-operation -> result: order product record is created, custom field is null

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans