locked
How to sum the currency fields in ms crm 2011 RRS feed

  • Question

  • Hi,

    I am trying to sum the 2 currency fields and post the result to total currency field in ms crm 2011 plugin but getting following cast exception error while executing the following line.

    projafp["bap_estimatedrevenue"] = (((Money)(projAFPRecord[0]["bap_estimatedrevenue"]))).Value + ((afpPostImage.Attributes.Contains("bap_estimatedrevenue") ? ((Money)(afpPostImage["bap_estimatedrevenue"])) : new Money(0))).Value

    service.Update(projafp);

    Error:-
    System.InvalidCastException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #E5F8C802

    Please help how to resolve this issue. Its very urgent.

    Thanks


    Yadav

    Saturday, June 14, 2014 7:47 PM

Answers

  • Hi Yadav,

    Try wrap the whole calculation in a "new Money()".

    ~ Atomic Coder

    Saturday, June 14, 2014 8:57 PM
  • Hi,

    As you mentioned I tried with wrap the whole calculation in "new Money()", now I am not getting any error but the total value is not updating in target currency field. What could be the reason? I need to do any other casting here? please let me know.

    projafp.Attributes["bap_estimatedrevenue"] = new Money((((Money)(projAFPRecord[0]["bap_estimatedrevenue"]))).Value + ((afpPostImage.Attributes.Contains("bap_estimatedrevenue") ? ((Money)(afpPostImage["bap_estimatedrevenue"])) : new Money(0))).Value);

    Thanks


    Yadav

    Saturday, June 14, 2014 10:06 PM

All replies

  • Hi Yadav,

    Try wrap the whole calculation in a "new Money()".

    ~ Atomic Coder

    Saturday, June 14, 2014 8:57 PM
  • Hi,

    As you mentioned I tried with wrap the whole calculation in "new Money()", now I am not getting any error but the total value is not updating in target currency field. What could be the reason? I need to do any other casting here? please let me know.

    projafp.Attributes["bap_estimatedrevenue"] = new Money((((Money)(projAFPRecord[0]["bap_estimatedrevenue"]))).Value + ((afpPostImage.Attributes.Contains("bap_estimatedrevenue") ? ((Money)(afpPostImage["bap_estimatedrevenue"])) : new Money(0))).Value);

    Thanks


    Yadav

    Saturday, June 14, 2014 10:06 PM
  • the problem is with the true condition of the if, you do the cast to Money, but don't access to the Value property. I will rewrite in this way:

    var first = (Money)projAFPRecord[0]["bap_estimatedrevenue"];
    var second = afpPostImage.Contains("bap_estimatedrevenue") ? (Money)afpPostImage["bap_estimatedrevenue"] : new Money(0);
    projafp["bap_estimatedrevenue"] = new Money(first.Value + second.Value);


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Saturday, June 14, 2014 10:58 PM