locked
CRM calculation Plugin handling empty money values RRS feed

  • Question

  • Hello,

    i programmed an update-plugin with PreImage on entity "quotedetails" which calculates a sum of

    a.) sales rebate ("rabattvertrieb")and
    b.) yearly payment rebate ("rabattjahresbuchung1").

    My Problem is that user can empty the field sales rebate , so that the money field has no value anymore. If user does so and saves the quotedetail, the plugin throws an error "object not defined".

    How can i avoid that error, if an empty money field appears?

    Code snippets regarding that look like:

    Money rabattvertrieb = new Money();
                    rabattvertrieb.Value = (decimal)0;

                    if (entity.Attributes.Contains("fitsfp_rabattvertrieb"))
                    {
                        rabattvertrieb = (Money)entity.Attributes["fitsfp_rabattvertrieb"];
                    }
                    else
                    {
                        //Prüfen ob das Attribut im Image enthalten ist
                        if (image.Attributes.Contains("fitsfp_rabattvertrieb"))
                        {
                            rabattvertrieb = (Money)image.Attributes["fitsfp_rabattvertrieb"];
                        }
                   
                    }

    ....

    Money summerabatt = new Money(rabattvertrieb.Value + rabattjahresbuchung1.Value);

    ...

    if (entity.Attributes.Contains("fitsfp_rabattvertrieb"))
                        {
                                                   
                            entity.Attributes["fitsfp_rabattvertrieb"] = ((Money)rabattvertrieb);
                        }
                        else
                        {
                            entity.Attributes.Add("fitsfp_rabattvertrieb", (Money)rabattvertrieb);
                        }
                   

    I don't get the clue how to catch that empty values, maybe someone can give me advice.

    Thanks in advance,

    Ulrich


    Microsoft Certified Professional | Microsoft Certified Partner

    Thursday, March 19, 2015 4:37 PM

All replies

  • Hello,

    If your money fields don't have a value, the attributes are not present in the attribute dictionary of the image/entity. Contains checks is there is a value for a specific key. Try ContainsKey instead:

    Money rabatvertrieb = entity.Attributes.ContainsKey("fitsfp_rabattvertrieb") ? (Money) entity["fitsfp_rabattvertrieb"] : new Money(0);
    
    or
    
    Money rabatvertrieb = new Money(0);
    
    if(entity.Attributes.ContainsKey("fitsfp_rabattvertrieb))
    rabatvertrieb  = (Money)entity["fitsfp_rabattvertrieb"];

    Hope this helps and correct me if I am wrong ;)

    Kind Regards

    Friday, March 20, 2015 7:51 AM