Fetch the customer details using X++ in AX 2012

Hi Technical,

Today, in this post we are going to share a X++ code to fetch the customer details as customer number, customer name, primary address, country, sales channel, sales rep., region, territories, agent, sales manager in AX 2012.

When you run this X++ code in AX 2012, an excel will generate with all the data.




Output:


To fetch this data create an AX job in AOT and copy paste the below code.

// Project Name: PKA_Adhoc_CustomerDetaills
// Created By: Chirag Gupta
// Created Date: 29-Jan-2020
// Summary: Fetch the customer details as customer number, customer name, primary address,
// country, sales channel, sales rep., region, territories, agent, sales manager.
static void PKA_GetCustomerData(Args _args)
{
    CustTable                                   custTable;
    smmBusRelChainGroup                         smmBusRelChainGroup;
    smmBusRelSegmentGroup                       smmBusRelSegmentGroup;
    smmBusRelSubSegmentGroup                    smmBusRelSubSegmentGroup;
    GOD_AgentMaster_RSA                         agentMaster_RSA;
    Name                                        salesRep, salesManager;
    LogisticsAddressCountryRegionTranslation    countryRegionTranslation;
    UserInfo                                    userInfo;
    LogisticsAddressCountryRegion               countryRegion;
    LogisticsPostalAddress                      postalAddress;
    SysExcelApplication                         xlsApplication;
    SysExcelWorkBooks                           xlsWorkBookCollection;
    SysExcelWorkBook                            xlsWorkBook;
    SysExcelWorkSheets                          xlsWorkSheetCollection;
    SysExcelWorkSheet                           xlsWorkSheet;
    SysExcelRange                               xlsRange;
    int                                         row;

    xlsApplication           = SysExcelApplication::construct();
    xlsWorkBookCollection    = xlsApplication.workbooks();
    xlsWorkBook              = xlsWorkBookCollection.add();
    xlsWorkSheetCollection   = xlsWorkBook.worksheets();
    xlsWorkSheet             = xlsWorkSheetCollection.itemFromNum(1);
    row                      = 1;

    xlsWorkSheet.cells().item(row, 1).value("Customer Number");
    xlsWorkSheet.cells().item(row, 2).value("Customer Name");
    xlsWorkSheet.cells().item(row, 3).value("Primary Address");
    xlsWorkSheet.cells().item(row, 4).value("Country");
    xlsWorkSheet.cells().item(row, 5).value("Sales Channel");
    xlsWorkSheet.cells().item(row, 6).value("Sales Rep.");
    xlsWorkSheet.cells().item(row, 7).value("Region");
    xlsWorkSheet.cells().item(row, 8).value("Territories");
    xlsWorkSheet.cells().item(row, 9).value("Agent");
    xlsWorkSheet.cells().item(row, 10).value("Sales Manager");

    while select firstOnly100 custTable where custTable.AccountNum == 'C04015'
    {
        row++;
        smmBusRelChainGroup.Description     = smmBusRelChainGroup::find(custTable.CompanyChainId).Description;
        smmBusRelSegmentGroup.Description   = smmBusRelSegmentGroup::find(custTable.SegmentId).Description;
        agentMaster_RSA.Description         = GOD_AgentMaster_RSA::find(custTable.GOD_Agent).Description;
        salesRep                            = HcmWorker::worker2Name(custTable.MainContactWorker);
        salesManager                        = HcmWorker::worker2Name(custTable.PKA_MainContactWorker);
        postalAddress                       = custTable.postalAddress();
        countryRegion                       = LogisticsAddressCountryRegion::find(postalAddress.CountryRegionId);

        select firstonly Language from userInfo where userInfo.Id == curUserId()
            join countryRegionTranslation
                where countryRegionTranslation.CountryRegionId == countryRegion.CountryRegionId &&
                        countryRegionTranslation.LanguageId    == userInfo.Language;

        select firstOnly smmBusRelSubSegmentGroup
            where smmBusRelSubSegmentGroup.SegmentId == custTable.SegmentId
            && smmBusRelSubSegmentGroup.SubsegmentId == custTable.SubsegmentId;

        smmBusRelSubSegmentGroup.SubSegmentDescription = smmBusRelSubSegmentGroup.SubSegmentDescription;

        //info(strFmt("%1 - %2 - %3 - %4 - %5 - %6 - %7 - %8 - %9 - %10",
                    //custTable.AccountNum,
                    //custTable.name(),
                    //postalAddress.Address,
                    //countryRegionTranslation.ShortName,
                    //smmBusRelChainGroup.Description,
                    //salesRep,
                    //smmBusRelSegmentGroup.Description,
                    //smmBusRelSubSegmentGroup.SubSegmentDescription,
                    //agentMaster_RSA.Description,
                    //salesManager
                    //)
            //);

        xlsWorkSheet.cells().item(row, 1).value(custTable.AccountNum);
        xlsWorkSheet.cells().item(row, 2).value(custTable.name());
        xlsWorkSheet.cells().item(row, 3).value(postalAddress.Address);
        xlsWorkSheet.cells().item(row, 4).value(countryRegionTranslation.ShortName);
        xlsWorkSheet.cells().item(row, 5).value(smmBusRelChainGroup.Description);
        xlsWorkSheet.cells().item(row, 6).value(salesRep);
        xlsWorkSheet.cells().item(row, 7).value(smmBusRelSegmentGroup.Description);
        xlsWorkSheet.cells().item(row, 8).value(smmBusRelSubSegmentGroup.SubSegmentDescription);
        xlsWorkSheet.cells().item(row, 9).value(agentMaster_RSA.Description);
        xlsWorkSheet.cells().item(row, 10).value(salesManager);

        info(strFmt("%1 record(s) generated in excel file.", row - 1));
    }
    xlsApplication.visible(true);
}

We hope this solution helped you to fetch customer details using X++ in AX 2012. Please comment your valuable feedback for this article...

Author:
Chirag Gupta
Microsoft Dynamics D365/AX 2012
Senior Technical Consultant at IBM Bangalore

Date:
3-Apr-2020

Happy Learning !!

Comments

Popular posts from this blog

[Solved] : Error: String or binary data would be truncated. Cannot execute a data definition language command on Vendor (VendLedgerReconciliationTmp)

AX 2012 - Command prompt compilation / AxBuild.exe for Parallel Compile on AOS of X++ to P-code / Parallel Compilation

AX 2012 - How to send SSRS report in email as an attachment using X++

AX 2012 - How to stop AOS when services got stuck at stopping status

AX 2012 - How to make a form field mandatory

Error: There was no endpoint listening at net.tcp://your_server_name:8201/Dynamics AX/Services…

Fetch vendor address using X++ in AX 2012

AX 2012 - How to print current date and time on SSRS Report

[Error] - Error in unit of amount in currency KES. Must be rounded to 0.01 | The unit of 37.563.5343100000000000 KES being posted to account 32710001-- is outside the current penny rounding threshold | Posting has been canceled

[Solved] : AX 2012 - No connection could be made because the target machine actively refused it 192.168.10.249:8201