March 19, 2015

X++ code to create Excel file in AX 2009

static void CreateExcelDocument (Args _args)
 {
    SysExcelApplication xlsApplication;
    SysExcelWorkBooks xlsWorkBookCollection;
    SysExcelWorkBook xlsWorkBook;
    SysExcelWorkSheets xlsWorkSheetCollection;
    SysExcelWorkSheet xlsWorkSheet;
    SysExcelRange xlsRange;
    CustTable custTable;
    int row = 1;
    str fileName;
    ;

    // Name of the Excel document.
    fileName = "C:\\test.xslx";

    // Excel open and initialize.
    xlsApplication = SysExcelApplication:: construct ();
    xlsApplication.visible (true);

    // Create an Excel Worksheet produce.
    xlsWorkBookCollection = xlsApplication.workbooks();
    xlsWorkBook = xlsWorkBookCollection.add();
    xlsWorkSheetCollection = xlsWorkBook.worksheets();
    xlsWorkSheet = xlsWorkSheetCollection.itemFromNum (1);

    // Write to the worksheet cells headings.
    xlsWorkSheet.cells (). item (row, 1). value ('Account Number');
    xlsWorkSheet.cells (). item (row,2). value ('name');

    row ++;

    // Excel Worksheet with data fill / (Excel cells fill).
    while select custTable
    {
    xlsWorkSheet.cells (). item (row, 1). value (custTable.AccountNum);
    xlsWorkSheet.cells (). item (row, 2). value (custTable.Name);
    row ++;
    }

    // Check whether the document already exists.
    if (WINAPI:: fileExists (fileName))
    {
    Winapi:: DeleteFile (fileName);
    }

    // Save Excel document.
    xlsWorkbook.saveAs(fileName);

    // Close Excel.
    xlsApplication.quit ();
    xlsApplication.finalize ();
 }

X++ code to get AssestId in AX 2009

static void assetIdCheck(Args _args)
{
     LedgerTable     ledgerTable;
     LedgerTrans     ledgerTrans;
     AssetTrans      assetTrans;
     ;
     select AssetId from assetTrans
     join ledgerTrans join ledgerTable
     where assetTrans.Voucher == ledgerTrans.Voucher && ledgertrans.AccountNum == '110110';

     info (assetTrans.AssetId);
     info (ledgerTrans.AccountNum);

}


X++ code for Creating and invoicing Sales order in AX 2012

static void SOAutoConfirmAndInvoice(Args _args)
{
SalesTable salesTable;
SalesLine salesLine;
CustTable custTable= CustTable::find("US-101");
AxSalesTable axsalesTable;
AxSalesLine axSalesLine;
SalesFormLetter salesFormLetter;
;

//Create Sales order

salesTable.initFromCustTable();

axsalesTable = AxSalesTable::newSalesTable(salesTable);
axsalesTable.parmCustAccount("US-004");
axsalesTable.parmSalesType(SalesType::Sales);

axsalesTable.parmDocumentStatus(DocumentStatus::Confirmation);

axsalesTable.parmDeliveryDate(str2Date("03/18/2015",213));
axsalesTable.parmSalesStatus(SalesStatus::Backorder);

axsalesTable.doSave();

salesLine.initFromSalesTable(salesTable);

axSalesLine = AxSalesLine::newSalesLine(salesLine);
axSalesLine.parmItemId("T0001");
axSalesLine.parmInventDimId("000458");
axSalesLine.parmCurrencyCode("USD");
axSalesLine.parmSalesQty(2);
axSalesLine.parmSalesPrice(10.00);
axSalesLine.doSave();



//SO confirmation
salesTable = axSalesTable.salesTable(salesTable);
salesFormLetter = SalesFormLetter::construct(DocumentStatus::Confirmation);
salesFormLetter.update(salesTable);

// SO invoicing
salesFormLetter = salesFormLetter::construct(DocumentStatus::Invoice);
salesFormLetter.update(salesTable);
info(strFmt("Sales order %1 invoiced",salesTable.SalesId));
}


X++ code for creating and invoicing Purchase order in AX 2012

static void POAutoConfirmAndInvoice(Args _args)
{
PurchTable purchTable;
PurchLine purchLine;
VendTable vendTable = VendTable::find("US-101");
AxPurchTable axPurchTable;
AxPurchLine axPurchLine;
PurchFormLetter purchFormLetter;
;

//Create Purchase order
purchTable.initFromVendTable(vendTable);

axPurchTable = axPurchTable::newPurchTable(purchTable);
axPurchTable.parmPurchaseType(PurchaseType::Purch);
axPurchTable.parmDocumentStatus(DocumentStatus::PurchaseOrder); 


axPurchTable.parmDeliveryDate(str2date("03/18/2015",213)); 
axPurchTable.parmAccountingDate(str2date("03/18/2015",213));
axPurchTable.parmPurchStatus(PurchStatus::Backorder);

axPurchTable.doSave();


purchLine.initFromPurchTable(purchTable);

axPurchLine = AxPurchLine::newPurchLine(purchLine);
axpurchLine.parmItemId("T0001"); 
axpurchLine.parmInventDimId('000458');
axPurchLine.parmPurchQty(10);
axPurchLine.parmPurchPrice(100);
axPurchLine.doSave();

//PO confirmation 
purchTable = axPurchTable.purchTable();
purchFormLetter = PurchFormLetter::construct(DocumentStatus::PurchaseOrder);
purchFormLetter.update(purchTable, strFmt("Inv_%1", purchTable.PurchId));

// PO invoicing
purchFormLetter = PurchFormLetter::construct(DocumentStatus::Invoice);
purchFormLetter.update(purchTable, strFmt("Inv_%1", purchTable.PurchId));
info(strFmt("purchase order %1 invoiced",purchTable.PurchId));

}


X++ code to find all the reports with datasources innerjoined 1:1

static void FindAllReports(Args _args)
{
   #AOT
   Report                  report;
   TreeNode                treeNode = TreeNode::findNode(#ReportsPath);
   TreeNodeIterator        iterator = treeNode.AOTiterator();
   QueryBuildDataSource    qbds;

   boolean  find1nInnerJoin(QueryBuildDataSource _qbdsParent)
   {
       int i;
       QueryBuildDataSource qbdsChild;
       boolean    ret;
       ;
       for (i = 1; i <= _qbdsParent.childDataSourceCount(); i++)
       {
           qbdsChild = _qbdsParent.childDataSourceNo(i);
           if (qbdsChild)
           {
               if (qbdsChild.joinMode() == JoinMode::InnerJoin &
& qbdsChild.fetchMode() == QueryFetchMode::One2One)//::One2Many)
                   return true;

               if (qbdsChild.childDataSourceCount() > 0 && find1nInnerJoin(qbdsChild))
                   return true;
           }
       }
       return ret;
   }
   ;

   treeNode = iterator.next();
   while (treeNode)
   {
       if (treeNode.sysNodeType() == 202) //Report
       {
           report = treeNode;
           if (report && report.query().dataSourceCount() > 1)
           {
               qbds = report.query().dataSourceNo(1);
               if (find1nInnerJoin(qbds))
                   info(report.name());
           }
       }
       treeNode = iterator.next();
   }
}


January 05, 2015

X++ code to Customize Customer InvoiceId in the format of SI-YYMM#####

Hai,

Class used in this process is 

SalesInvoiceJournalCreateBase 

Method used is 

initJournalHeader()

comment this below line 

custInvoiceJour.InvoiceId= this.getJournalNumber();

and write the following code :

int HyphenPosition;

 

custInvoiceJour.InvoiceDate = this.updateDate();

 HyphenPosition=strFind(this.getJournalNumber(),'-',1,strLen(this.getJournalNumber()));

    custInvoiceJour.InvoiceId = subStr(this.getJournalNumber(),1,HyphenPosition)+
                                subStr(date2str(custInvoiceJour.InvoiceDate,321,2,0,2,0,2),1,4)+
                                subStr(this.getJournalNumber(),HyphenPosition+1,strLen(this.getJournalNumber())-HyphenPosition);

X++ code to get the Phone Number of the Delivery Address in the Purchase Order in AX 2012

static void getphoneNumber(Args _args)
{

PurchTable purchTable;

LogisticsLocation logisticslocation;

LogisticsPostalAddress logisticsPostalAddress;
   

select purchTable where purchTable.PurchId=="000020";

select logisticslocation where logisticslocation.ParentLocation =
LogisticsPostalAddress::findRecId(purchTable.DeliveryPostalAddress).Location;

info(LogisticsElectronicAddress::findByLocation(logisticslocation.RecId).Description);

info(LogisticsElectronicAddress::findByLocation(logisticslocation.RecId).Locator);