Friday, March 8, 2013

X++ switch signs for use in matching credits to debits

In certain applications, you may need to switch the sign of an amount variable. For example, $170.00 would be -$170.00 or -$180.00 would be $180.00.  Going from negative to positive is easy with the abs([VALUE]) function but sometimes you don't know if the variable coming in is positive or negative. This would not work for pos to neg.

I used this in a three way auto-match modification so that I could match up debits/credits to each other where the starting amount could be either. Nothing too special but figured the code could potentially help out someone. Just don't use the hardcoded recId or infoLog obviously.

static void daxSwitchSigns(Args _args)
{
    CustTrans   custTrans = CustTrans::find(5637215455);
    Amount      custTransAmount = custTrans.AmountMST > 0 ? -custTrans.AmountMST : abs(custTrans.AmountMST);  
    
    info (strFmt("%1 - Switched signs: %2", CustTrans.AmountMST, custTransAmount));                     
}
Figure 1 - The code to switch signs


Figure 2 - The Output from the code in Figure 1

4 comments:

  1. Hi all,
    how to print debit and credit value separately in ax 2009 report

    ReplyDelete
  2. Arun,
    If I understand what you are asking correctly, you need to have two columns in a MorphX report (non-SSRS):one for debit and one for credit. The transaction has this data in the same field but we need to split it out into two columns.

    There are multiple ways to do this and which is best would be a matter of which is best in which scenario. Without knowing more details, I would say have two fields presenting the same data except on one field, have an X++ condition on the report override to only show the value if its is in a certain range (e.g. <0) while the other being the >=0. I'd leave the other column not being used as a blank value (requiring a string field) rather than a zero as that could get annoying.

    Alternatively, depending on what you need to do, you may need to have two data sources, each pulling only a certain set of records also using similar criteria as described above, but at the data source level.

    Hope that helps!

    ReplyDelete
  3. static void SwitchSigns(Args _args)
    {
    Amount origAmount = -590.39;
    Amount newAmount;

    newAmount = origAmount * -1;

    info (strFmt("%1 - Switched signs: %2", origAmount, newAmount));

    origAmount = 243.55;

    newAmount = origAmount * -1;

    info (strFmt("%1 - Switched signs: %2", origAmount, newAmount));
    }

    ReplyDelete
    Replies
    1. Just multiply by -1 for the same result.

      Delete