Friday, 27 May 2016

Split Ledger Account through x++

Some times when developing we encounter some strings of which we only need a segment of the string, let’s say for example the Ledger Account.

static void ledgeraccount(Args _args)
{
    GeneralJournalAccountEntry      gjae; //Table name
    container                                  con;
    List                                         list = new List(types::string);
    ListIterator                              iterator;
    str                                          ch;
    ;
    select gjae  where gjae.RecId == 5637151397;
    ch = strFmt("%1",gjae.LedgerAccount);//510500-002-022-007-Audio

//we will initialize the list using the standard method strSplit of Dynamics AX, we will use as the separator the “-” character without quotes.

    list = Global::strSplit(Ch, '-');

//Now we must start the iterator for our list.
//The iterator will loop through the lines of our list and them to a container which in our example will be “con”.

    iterator = new ListIterator(List);
    while(iterator.more())
    {
        con+=iterator.value();
        iterator.next();
    }
    info(conPeek(con,1));//510500
    info(conPeek(con,2));//002
    info(conPeek(con,3));//022
    info(conPeek(con,4));//007
    info(conPeek(con,5));//Audio

//Now we have a populated container and,  using the “conPeek” standard function we can retrieve the elements from the container by the index.
}

No comments:

Post a Comment