Getting Segments from Segmented Entry control/Ledger dimension–Dynamics AX 2012 [X++]

Friends,

As you know, In AX 2012, we use the segmented entry control to associate an account and related financial dimensions with the record.

In our earlier posts, we have learnt about the segmented entry controls and in this post, we will see how to extract the segments from the segmented entry control.

Example : segmented control [Screen shot below]

sssegmented control

As you see that the above ledger dimension has the account associated with the segments[dimensions].

Below code will get all the account transactions [sum amount] group by CostCenter Dimension.

In the code, we will extract the segments with the help of DimensionStorage class

static void SR_Extract_SegmentName_values(Args _args)

{

    #define.CostCenter("CostCenter")

    Query                                   query;

    QueryRun                                queryRun;

    QueryBuildDataSource                    qbds,qbdsDate;

    GeneralJournalAccountEntry              generalJournalAccountEntry;

    GeneralJournalEntry                     generalJournalEntry;

    container                               value = conNull();

    OMOperatingUnit                         omou;

 

    // Dimension

    DimensionStorage        dimensionStorage;

 

    // Segment

    int                     segmentCount, segmentIndex;

    DimensionStorageSegment segment;

    str 100                 segmentName, MainAccount, segmentValue;

    container               segments;

    query                   = new Query();

    qbds                    = query.addDataSource(tableNum(GeneralJournalAccountEntry));

 

    // Sum functional and transaction amounts

 

    qbds.addSelectionField(fieldNum(GeneralJournalAccountEntry, AccountingCurrencyAmount), SelectionField::Sum);

    qbds.addSelectionField(fieldNum(GeneralJournalAccountEntry, TransactionCurrencyAmount), SelectionField::Sum);

    qbds.addGroupByField(fieldNum(GeneralJournalAccountEntry, LedgerDimension));

    qbdsDate = qbds.addDataSource(tableNum(GeneralJournalEntry));

    qbdsDate.joinMode(JoinMode::InnerJoin);

    qbdsDate.fetchMode(QueryFetchMode::One2One);

    qbdsDate.addLink(fieldNum(GeneralJournalAccountEntry, GeneralJournalEntry), fieldNum(GeneralJournalEntry, RecId));

    queryRun = new QueryRun(query);

 

    while (queryRun.next())

    {

        generalJournalAccountEntry = queryRun.get(tableNum(generalJournalAccountEntry));

        // Get dimension storage

        dimensionStorage = DimensionStorage::findById(GeneralJournalAccountEntry.LedgerDimension);

        if (dimensionStorage == null)

        {

            throw error("@SYS83964");

        }

        // Add segments for all hierarchies

        segments = conNull();

        // Get segments

        segmentCount = dimensionStorage.segmentCountForHierarchy(1);

        for (segmentIndex = 1; segmentIndex <= segmentCount; segmentIndex++)

        {

            // Get segment

            segment = dimensionStorage.getSegmentForHierarchy(1, segmentIndex);

            if (segment.parmDimensionAttributeValueId() != 0)

            {

                // Get segment name

                segmentName = DimensionAttribute::find(DimensionAttributeValue::find(segment.parmDimensionAttributeValueId        ()).DimensionAttribute).Name;

 

                // Add segment value

                segmentValue = segment.parmDisplayValue();

 

                if (segmentIndex == 1)

                {

                    MainAccount = segmentValue;

                }

 

                if (segmentName == #CostCenter)

                {

                   info("Main account – " +  MainAccount);

                   info("Segment Name – " + segmentName);

                   info("Cost center – "  + segmentValue);

                   info(strFmt(‘Total TransactionCurrency amount – %1’,generalJournalAccountEntry.TransactionCurrencyAmount));

                   info(strFmt(‘Total AccountingCurrency amount – %1’, generalJournalAccountEntry.AccountingCurrencyAmount));

                   info("______________________________________");

                }

            }

        }

    }

}

 

Below is the output

 

Output

[Note: In the above code , I have checked explicitly with the segment name to #CostCenter. There are many ways to find out whether the dimension belongs to Cost center/Department etc. we can use DimensionAttribute table to or OMOperatingUnit table to verify this]

If you want to restrict only linked main accounts as per the legal entity, you can use the below code

[I hope you know how the structure is >> Legal entity >> COA >> main accounts]

// Join to main account to filter by accounts

            qbds = query.dataSourceTable(tableNum(DimensionAttributeValueCombination));

            qbds = qbds.addDataSource(tableNum(MainAccount));

            qbds.fetchMode(QueryFetchMode::One2One);

            qbds.joinMode(JoinMode::InnerJoin);

            qbds.addLink(fieldNum(DimensionAttributeValueCombination, MainAccount), fieldNum(MainAccount, RecId));

 

            // Restrict to current COA and proper account types

            qbds.addRange(fieldNum(MainAccount, LedgerChartOfAccounts)).value(queryValue(LedgerChartOfAccounts::current()));

 

Happy Dax6ng,

Sreenath Reddy

Advertisements

One Response to “Getting Segments from Segmented Entry control/Ledger dimension–Dynamics AX 2012 [X++]”

  1. suganya Says:

    hi,
    thanks for your post.it helped me a lot.
    my doubt is when i try to get ledger segment name and value i am getting only the main account.but in my ledger account structure i have cost center and department also.
    using above code i am getting only main account. if (segment.parmDimensionAttributeValueId() != 0)
    this condition is not satisfied so i am not getting name and value for cost center.
    can u guide me in this.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: