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)

Plugin not triggered on Update of field

(0) ShareShare
ReportReport
Posted on by

Hello guys,

My C# plugin is in Quotes. It's on update of "totallineitemamount" and a field called "new_vat". It basically calculates the Total amount of the Quote Products multiplied by the new_vat, and stores it in a field called "Total Amount + VAT".

Problem: The Total Amount of the Quotes ( "totallineitemamount" ) updates from the CRM itself, everytime when a Quote Product is created, but this doesn't change the field that I'm calculating - "Total Amount + VAT". The value in this field stays the same, until I update the new_vat field - And stays the same if I change the totallineitemamount field. Just like the plugin is never being triggered on that occasion.

I'm starting to feel that since the field "totallineitemamount" is updated by a system workflow that takes the values of the Quote Products, my plugin doesn't take this as a step that triggers the calculation.

How can I make this plugin to update itself, when the "totallineitemamount" /Total Amount of Quote Products/ change?

Thanks in Advance, Georgi!

*This post is locked for comments

I have the same question (0)
  • Luc Valentin Profile Picture
    220 on at

    Hello Georgi,

    Can you indicate if your plugin fires on Pre Event or on Post Event.

    I've done a lot of this kind of plugins and, usually, the behavior is better when you trigger on Post Event.

    Also, make sure that the fields "totallineitemamount" and "new_vat" are both checked in the Filtering Attributes.

    Luc Valentin

  • Community Member Profile Picture
    on at

    Hello Luc,

    Yes, I do these kinds of plugins recently a lot too. It is on post-update, and I have both of the fields checked in the Filtering Attributes. Still doesn't calculate when the "totallineitemamount" field changes :/

    Thank you, Georgi B

  • Luc Valentin Profile Picture
    220 on at

    With what tool did you create the plugin ? Developer Toolkit for Visual Studio ?

    Did you change the name of the Post Image ?

    One "bug" I already get is that the name of the Post Image was changed by the Developer Toolkit.

    For example, I've set the name to "PostImage", and after modifying the plugin step, the name was reset to "postImage" (with a little P).

    As the C# contained a check and made a return if the image does not exist, nothing was happening.

    If you can post the first few lines of you C# code, it might help to find what's wrong. :-)

    Luc

  • Community Member Profile Picture
    on at

    I'm using Developer Toolkit for Visual Studio 2013 - yes! I added the steps for the plugins from Plugin Registration tool though (Long Story, but you can still see the step and the fields that activate the plugin).

    Here is my plugin: 

    IPluginExecutionContext context = localContext.PluginExecutionContext;
                IOrganizationService service = localContext.OrganizationService;
                Guid quoteProductID = (Guid)((Entity)context.InputParameters["Target"]).Id;
                ColumnSet set = new ColumnSet();
                set.AllColumns = true;
                var quote = service.Retrieve("quote", quoteProductID, set);
    
    
                if (context.Depth > 1)
                {
                    return;
                }
                else
                {
                    //First I get the base values that I need for the calculations
    
                    var totalamount = (Money)quote["totallineitemamount"];
                    var discount = (Money)quote["totaldiscountamount"];
                    var prepaymentValue = (OptionSetValue)quote["new_prepayment"];
    
                    var VAT = (OptionSetValue)quote["new_vat"];
                    var tax = totalamount.Value * VAT.Value / 100;
    
                    quote["new_totalammountincludingvat"] = new Money((totalamount.Value) + tax);
                    quote["new_prepayedamount"] = new Money((prepaymentValue.Value * totalamount.Value) / 100);
    
                    ConditionExpression condition = new ConditionExpression();
                    condition.AttributeName = "quoteid";
                    condition.Operator = ConditionOperator.Equal;
                    condition.Values.Add(quoteProductID);
    
                    FilterExpression filter = new FilterExpression();
                    filter.AddCondition(condition);
    
                    QueryExpression query = new QueryExpression();
                    query.EntityName = "quotedetail";
                    query.ColumnSet = new ColumnSet(true);
                    query.Criteria = filter;
    
                    EntityCollection quotedetails = service.RetrieveMultiple(query);
    
                    foreach (var detail in quotedetails.Entities)
                    {
                        var quantity = (decimal)detail["quantity"];
                        var priceperunit = (Money)detail["priceperunit"];
                        var teamleader = (OptionSetValue)detail["new_tldiscount"];
    
                        //Then I calculate the manual discount and baseamount, for the further calculations
                        //detail.Attributes["manualdiscountamount"] = new Money((priceperunit.Value * teamleader.Value / 100) * quantity);
                        var manualdiscountamount = (Money)detail.Attributes["manualdiscountamount"];
                        //detail.Attributes["baseamount"] = new Money(priceperunit.Value * quantity);
                        var baseamount = (Money)detail["baseamount"];
    
                        //finally I calculate the tax
                        detail["new_vat"] = new OptionSetValue(VAT.Value);
                        var taxDetail = (baseamount.Value - manualdiscountamount.Value) * VAT.Value / 100;
                        detail.Attributes["tax"] = new Money(taxDetail); //tax
    
                        service.Update(detail);
                    }
    
                    service.Update(quote);
    

    And here is the actual snippet that is not calculated when needed:

    var totalamount = (Money)quote["totallineitemamount"];
                    var discount = (Money)quote["totaldiscountamount"];
                    var prepaymentValue = (OptionSetValue)quote["new_prepayment"];
    
                    var VAT = (OptionSetValue)quote["new_vat"];
                    var tax = totalamount.Value * VAT.Value / 100;
    
                    quote["new_totalammountincludingvat"] = new Money((totalamount.Value) + tax);
    
      service.Update(quote);

    I hope you'll see what I'm missing, Thank you : ]
     

  • Verified answer
    Luc Valentin Profile Picture
    220 on at

    I don't anything obvious that prevent your plugin from doing its job.

    Here is what I suggest.

    Start by identifying if the problem comes from the C# code or from the CRM Platform.

    To do that, you can place a "throw exception" at the beginning of your C# code.

    Insert this line as the first instruction :

    throw new InvalidPluginExecutionException("QuoteReference plugin does not contain QuoteImage image.");

    This should pops up a "Business Process Error" when updating one of your Filtering Attributes.

    If this popup happens, it means that the issues comes from your code.

    Maybe the line 9 (context.Depth > 1) ... as this line makes a return, it may make nothing without informing you.

    I also advise to use the Tracing Service, you can instantiate it with this line :

    ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

    Than you log traces with :

    tracingService.Trace("Your custom message");

    To see the trace, you need to stop the plugin with error by throwing an exception, you can do it with the "throw new InvalidPluginExecutionException" code.

    If this doesn't give any result, consider checking the CRM Platform, see if all Windows Services related to CRM are running, reboot the server, ... do an iisreset in the console.

    Luc

  • Aileen Gusni Profile Picture
    44,524 on at

    Hi Georgi,

    Try to debug:

    var totalamount = (Money)quote["totallineitemamount"];

    What is its value?

    Or just check quote.Contains("totallineitemamount")

    I just worry because you update, then update it again...

    Because I use this in my plugin, it works. But, I use TotalAmount, not totallinelitemamount

    So example of my plugin:

    if (TargetEntity.TotalAmount != null)

                       {

                           totalAmountNetSellingPrice = TargetEntity.TotalAmount != null ? TargetEntity.TotalAmount.Value : 0;

                       }

    And I can get the total amount easily.

    Basically your purpose is the user key in the VAT in the Quote header then you want to update each quote product Tax? Is it correct or you can elaborate more?

    Thanks.

  • Community Member Profile Picture
    on at

    Well, I use the Plugin Profiler for debuging of plugins. The plugin is just not firing when the "totallineitemamount" is updated. I believe it is a problem of the platform.

    And about the "return" in line 9 - > it is used to escape infinite loops, so it is not connected to the plugin logic.

    I believe that the problem is in CRM. What I'll do from here is try to reload the calculations via Javascript on Load event.

    Wish me luck and Thank you for your suggestions Luc : ]

    Georgi B

  • Community Member Profile Picture
    on at

    Hello Aileen,

    Thank you for your response. I update twice, but the first thing that I update are the quote products /Which doesn't concern the Quote itself/ and after that I retrieve the values of the quote products and update the Quote, so I don't think this would be the problem here.

    Thank you, Georgi B

  • Verified answer
    Aileen Gusni Profile Picture
    44,524 on at

    Georgi,

    So the plugin didn't triggered at all?

    Try to check by add the throw new InvalidPluginExecutionException("test")

    in the initial line, if it was shown, so the plugin is triggered.

    And try to change it to pre create

    Thanks.

  • Community Member Profile Picture
    on at

    Thank you Aileen and Luc,

    I tried to make a javascript that takes the value of the field that wasn't updating, and set it again (in order to create something like trigger that recalculates everything onload), but it also didn't work.

    Then I tried to throw this custom Exception, and as it turned out - the plugin is indeed triggered. So I followed Luc's advice on changing the line 9 check for context.Depth. I gave it little more Depth (5) and now it works correctly.

    Good job!

    Thank you guys,

    Georgi B.

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