locked
Money field calculation with C# RRS feed

  • Question

  • Hi All,

    I would like to do next with C#:

    first, see if a money field has a value or not.

    second, how to do calculations between money fields and obtain the result in a money field( C = A  - B for example)

    could you help me with this please?

    thanks.

    david.

    Thursday, February 28, 2013 2:09 PM

Answers

  • Hi,

    If you are writing plugin 

    Decimal a;
    Decimal b;
    Decimal c;
    if(entity.Attributes.Contains("money_field1"))
    {
    a=((Money)entity.Attributes["money_field1"]).Value;
    }
    if(entity.Attributes.Contains("money_field2"))
    {
    b=((Money)entity.Attributes["money_field2"]).Value;
    }
    
    c=a-b;



    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !


    • Edited by _Vikram Thursday, February 28, 2013 2:24 PM
    • Proposed as answer by Dibyasingh Tripathy Friday, March 1, 2013 10:51 AM
    • Marked as answer by gardav0 Friday, March 1, 2013 3:20 PM
    Thursday, February 28, 2013 2:24 PM

All replies

  • do you mean that you want to write something in plug in or in some external C# code? check http://social.microsoft.com/Forums/en/crmdevelopment/thread/550ac902-e992-493b-a9dc-4e38952d9986

    regards

    joon

    Thursday, February 28, 2013 2:21 PM
  • yes, you are right, i did not specify the kind of program.

    it's an external program, is not a plugin, is a program just lo load some information into CRM.

    thanks.

    david.

    Thursday, February 28, 2013 2:24 PM
  • Hi,

    If you are writing plugin 

    Decimal a;
    Decimal b;
    Decimal c;
    if(entity.Attributes.Contains("money_field1"))
    {
    a=((Money)entity.Attributes["money_field1"]).Value;
    }
    if(entity.Attributes.Contains("money_field2"))
    {
    b=((Money)entity.Attributes["money_field2"]).Value;
    }
    
    c=a-b;



    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !


    • Edited by _Vikram Thursday, February 28, 2013 2:24 PM
    • Proposed as answer by Dibyasingh Tripathy Friday, March 1, 2013 10:51 AM
    • Marked as answer by gardav0 Friday, March 1, 2013 3:20 PM
    Thursday, February 28, 2013 2:24 PM
  • i guess you can use decimal datatype of SQL. It is similar. In C#, you can use Convert.ToDecimal() for that. For null value, you can either use System.DBNull.Values or simple null(did not check myself).

    Decimal decCurrency = Convert.ToDecimal("YourValueForCurrency");
    crmEntity["crmEntityCurrencyField"] = new Money(decCurrency);

    regards

    joon


    • Edited by Joon84 Thursday, February 28, 2013 2:31 PM
    Thursday, February 28, 2013 2:30 PM
  • for money field  the datatype is in money format

             Money sellprice = (Money)o2mh.Attributes["pz_test"];
                    decimal sellpricevalue = sellprice.Value;


    ms crm

    Thursday, February 28, 2013 3:58 PM