Fetch customer address using X++ in AX 2012

Hi Technical,

Today, in this post we are going to share a piece of code by which you can fetch customer address using X++ in AX 2012.

Create a AOT job and copy paste the below code to export customer address in excel.



// Project Name: PKA_WO67963_FetchCustomerVendorAddress
// Created by: Chirag Gupta (ibm.chirag)
// Created date: 18-Dec-19
// Summary: Fetch Customer ID, Name or description, Address, Purpose, Primary
static void PKA_CustomerAddress(Args _args)
{
    SysExcelApplication         xlsApplication;
    SysExcelWorkBooks           xlsWorkBookCollection;
    SysExcelWorkBook            xlsWorkBook;
    SysExcelWorkSheets          xlsWorkSheetCollection;
    SysExcelWorkSheet           xlsWorkSheet;
    SysExcelRange               xlsRange;
    int                         row;

    CustTable                   custTable;
    DirPartyTable               dirPartyTable;
    DirPartyLocation            dirPartyLocation;
    DirPartyLocationRole        dirPartyLocationRole;
    LogisticsLocation           logisticsLocation;
    LogisticsLocationRole       logisticsLocationRole;
    LogisticsPostalAddress      logisticsPostalAddress;
    str                         isPrimary;

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

    row = 1;

    xlsWorkSheet.cells().item(row, 1).value("Custome ID");
    xlsWorkSheet.cells().item(row, 2).value("Name or description");
    xlsWorkSheet.cells().item(row, 3).value("Addresss");
    xlsWorkSheet.cells().item(row, 4).value("Purpose");
    xlsWorkSheet.cells().item(row, 5).value("Primary");

    while select AccountNum from custTable
        join Name from dirPartyTable
            where dirPartyTable.RecId == custTable.Party
        join IsPrimary from dirPartyLocation
            where dirPartyLocation.Party == custTable.Party
        join dirPartyLocationRole
            where dirPartyLocationRole.PartyLocation == dirPartyLocation.RecId
        join Type from logisticsLocationRole
            where logisticsLocationRole.RecId == dirPartyLocationRole.LocationRole
        join logisticsLocation
            where logisticsLocation.RecId == dirPartyLocation.Location
        join logisticsPostalAddress
            where logisticsPostalAddress.Location == logisticsLocation.RecId
    {
        row++;

        xlsWorkSheet.cells().item(row, 1).value(custTable.AccountNum);
        xlsWorkSheet.cells().item(row, 2).value(dirPartyTable.Name);
        xlsWorkSheet.cells().item(row, 3).value(logisticsPostalAddress.Address);
        xlsWorkSheet.cells().item(row, 4).value(enum2str(logisticsLocationRole.Type));
        xlsWorkSheet.cells().item(row, 5).value(enum2str(dirPartyLocation.IsPrimary));
    }

    xlsApplication.visible(true);
}

We hope this article helped you to fetch customer address by using X++. Please comment your valuable feedback for this article...

Author:
Chirag Gupta
Microsoft Dynamics 365 AX Technical Consultant at IBM Bangalore

Date:
29-Jan-2020

Happy Learning !!

Comments

Post a Comment

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