adsenseheader

Thursday, January 30, 2020

Export to Excel in Dynamics AX 365 for operations

Hi Guys,

The below code shall take the input parameters for filtering the query and export the data to excel.

using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using OfficeOpenXml.Table;

class MOETExport
{
    VendAccount accountNum;
    TransDate fromDate, todate;
    NoYesId resetMOET;

    public VendAccount parmAccountNum(VendAccount _accountNum = accountNum)
    {
        accountNum = _accountNum;

        return accountNum;
    }

    public NoYesId parmResetMOET(NoYesId _resetMOET = resetMOET)
    {
        resetMOET = _resetMOET;

        return resetMOET;
    }

    public TransDate parmFromDate(TransDate _fromDate = fromDate)
    {
        fromDate = _fromDate;

        return fromDate;
    }

    public TransDate parmToDate(TransDate _todate = todate)
    {
        todate = _todate;

        return todate;
    }

public void run()
    {
#define.Standard('Standard')
        #define.Student('Student')
        #define.STD('STD')
        #define.STU('STU')


PurchLine purchLine;
        Query query = new Query();
        QueryBuildDataSource qbdsPurchTable;
        QueryBuildDataSource qbdsTransDetails;
        QueryBuildDataSource qbds;
        QueryRun qr;
        TransDetails transDetails;
        DictRelation            dictRelation;
        MemoryStream memoryStream = new MemoryStream();

        try
        {
            accountNum = this.parmAccountNum();
            fromDate = this.parmFromDate() ? this.parmFromDate() : dateNull();
            todate = this.parmToDate() ? this.parmToDate() : dateMax();
            resetMOET = this.parmResetMOET();

            if (fromDate > todate)
            {
                throw Error("@SYS120590");
            }

            qbdsPurchTable = query.addDataSource(tableNum(PurchTable));
            qbds = qbdsPurchTable.addDataSource(tableNum(PurchLine));
            qbds.relations(true);

            qbdsTransDetails = qbds.addDataSource(tableNum(TransDetails));
            dictRelation = new DictRelation(tableNum(TransDetails));
            dictRelation.loadTableRelation(tableNum(PurchLine));
            qbdsTransDetails.addRelation(dictRelation);

            if (accountNum)
            {
                qbdsPurchTable.addRange(fieldNum(PurchTable, OrderAccount)).value(accountNum);
            }

            if (fromDate && todate)
            {
                qbdsPurchTable.addRange(fieldNum(PurchTable, CreatedDateTime)).value(queryRange(fromDate,todate));
            }

            if (resetMOET)
            {
                qbdsTransDetails.addRange(fieldNum(TransDetails, MOETExported)).value(queryValue(NoYes::Yes));

                qr = new QueryRun(query);
 
                while (qr.next())
                {
                    purchLine = qr.get(tableNum(PurchLine));

                    transDetails = TransDetails::findfromPurchLine(purchLine.RecId,true);

                    ttsbegin;
                    transDetails.MOETExported = NoYes::No;
                    transDetails.doUpdate();
                    ttscommit;
                }

                qbdsTransDetails.clearRanges();
            }

            qbdsTransDetails.addRange(fieldNum(TransDetails, MOETExported)).value(queryValue(NoYes::No));

            qr = new QueryRun(query);

            using (var package = new ExcelPackage(memoryStream))
            {
                var currentRow = 1;
                var worksheets = package.get_Workbook().get_Worksheets();
                var MOETWorksheet = worksheets.Add("ExportMOET");
                var cells = MOETWorksheet.get_Cells();
                OfficeOpenXml.ExcelRange cell = cells.get_Item(currentRow, 1);

                System.String value = fieldPName(PurchLine,DeliveryName);
                cell.set_Value(value);
                cell = null;

                value = fieldPName(PurchLine,PurchId);
                cell = cells.get_Item(currentRow, 2);
                cell.set_Value(value);

                value = "UsageDate";
                cell = cells.get_Item(currentRow, 3);
                cell.set_Value(value);

                value = "UsageMonth";
                cell = cells.get_Item(currentRow, 4);
                cell.set_Value(value);

                value = "UsageYear";
                cell = cells.get_Item(currentRow, 5);
                cell.set_Value(value);

                value = fieldPName(TransDetails,AgreementNumber);
                cell = cells.get_Item(currentRow, 6);
                cell.set_Value(value);

                value = "@SYS9367";
                cell = cells.get_Item(currentRow, 7);
                cell.set_Value(value);

                value = fieldPName(TransDetails,ProductSKU);
                cell = cells.get_Item(currentRow, 8);
                cell.set_Value(value);

                value = fieldPName(purchLine,QtyOrdered);
                cell = cells.get_Item(currentRow, 9);
                cell.set_Value(value);

                value = "@SYS319915";
                cell = cells.get_Item(currentRow, 10);
                cell.set_Value(value);

                value = fieldPName(TransDetails,BillingOption);
                cell = cells.get_Item(currentRow, 11);
                cell.set_Value(value);

                value = "OfferingType";
                cell = cells.get_Item(currentRow, 12);
                cell.set_Value(value);

                value = fieldPName(TransDetails,UsageCountry);
                cell = cells.get_Item(currentRow, 13);
                cell.set_Value(value);

                value = fieldPName(PurchLine,CustPurchaseOrderFormNum);
                cell = cells.get_Item(currentRow, 14);
                cell.set_Value(value);
 
                while (qr.next())
                {
                    currentRow ++;
                    NameAlias offeringVal;

                    purchLine = qr.get(tableNum(PurchLine));

                    transDetails = TransDetails::findfromPurchLine(purchLine.RecId,true);

                    if (!transDetails.MOETExported)
                    {
                        if (transDetails.Offering == #Standard)
                        {
                            offeringVal = #STD;
                        }

                        else if (transDetails.Offering == #Student)
                        {
                            offeringVal = #STU;
                        }

                        else
                        {
                            offeringVal = transDetails.Offering;
                        }

                        cell = null;
                        cell = cells.get_Item(currentRow, 1);
                        cell.set_Value(purchLine.DeliveryName);
                         
cell = null;
                        cell = cells.get_Item(currentRow, 2);
                        cell.set_Value(purchLine.PurchId);

                        cell = null;
                        cell = cells.get_Item(currentRow, 3);
                        cell.set_Value(DateTimeUtil::day(transDetails.UsageDate));

                        cell = null;
                        cell = cells.get_Item(currentRow, 4);
                        cell.set_Value(DateTimeUtil::month(transDetails.UsageDate));

                        cell = null;
                        cell = cells.get_Item(currentRow, 5);
                        cell.set_Value(DateTimeUtil::year(transDetails.UsageDate));

                        cell = null;
                        cell = cells.get_Item(currentRow, 6);
                        cell.set_Value(transDetails.AgreementNumber);

                        cell = null;
                        cell = cells.get_Item(currentRow, 7);
                        cell.set_Value(PurchLine.LineNumber);

                        cell = null;
                        cell = cells.get_Item(currentRow, 8);
                        cell.set_Value(transDetails.ProductSKU);

                        cell = null;
                        cell = cells.get_Item(currentRow, 9);
                        cell.set_Value(purchLine.QtyOrdered);

                        cell = null;
                        cell = cells.get_Item(currentRow, 10);
                        cell.set_Value(purchLine.itemName());

                        cell = null;
                        cell = cells.get_Item(currentRow, 11);
                        cell.set_Value(transDetails.BillingOption);

                        cell = null;
                        cell = cells.get_Item(currentRow, 12);
                        cell.set_Value(offeringVal);

                        cell = null;
                        cell = cells.get_Item(currentRow, 13);
                        cell.set_Value(transDetails.usageCountry);

                        cell = null;
                        cell = cells.get_Item(currentRow, 14);
cell.set_Value(purchLine.CustPurchaseOrderFormNum);
                    }
                        ttsbegin;
                        transDetails.MOETExported = NoYes::Yes;
                        transDetails.doUpdate();
                        ttscommit;
                }
                package.Save();
                file::SendFileToUser(memoryStream, 'MOETExport.xlsx');
            }

}
        catch (Exception::Error)
        {
            throw Exception::Error;
        }
    }

public static void main(Args _args)
    {
        Dialog dialog = new Dialog("@SCM:PurchPurchaseOrderLineEntityExport");
        DialogGroup dialogGroup;
        DialogField dialogAccountId;
        DialogField dialogFromDate;
        DialogField dialogToDate;
        DialogField dialogResetMOET;
        MOETExport moetExport = new MOETExport();

        dialogGroup = dialog.addGroup("@SYS8079");
        dialogAccountId = dialog.addField(extendedTypeStr(VendAccount));

        dialogGroup = dialog.addGroup("@SYS41297");
        dialogGroup.columns(2);
        dialogFromDate = dialog.addField(extendedTypeStr(FromDate));
        dialogToDate   = dialog.addField(extendedTypeStr(ToDate));

        dialogGroup = dialog.addGroup("ResetMOET");
        dialogGroup.columns(1);
        dialogResetMOET = dialog.addField(extendedTypeStr(NoYesId));
        dialogResetMOET.helpText("ResetMOETHelpTxt");

        if (dialog.run())
        {
            moetExport.parmAccountNum(dialogAccountId.value());
            moetExport.parmFromDate(dialogFromDate.value());
            moetExport.parmToDate(dialogToDate.value());
            moetExport.parmResetMOET(dialogResetMOET.value());
            moetExport.run();
        }
    }

}

Regards,
Pradeep