Create Purchase Order via X++

void clicked()
{
    PurchTable  purchTable;
    PurchLine   purchLine;
    InventDim   inventDim;
    NumberSeq   numberSeq;

    //add by fanddy
    AxPurchTable    axPurchTable;
    AxPurchLine     axPurchLine;
    PurchFormLetter purchformLetter;
    //END

   //create PO Header
   purchTable.clear();
   purchTable.initFromVendTable(VendTable::find(TECPOGroupHeader.VendAccount));
   purchTable.DefaultDimension = InventTable::find(POGroupLine.ItemId).DefaultDimension;

   axPurchTable = axPurchTable::newPurchTable(purchTable);
   axPurchTable.parmPurchId(NumberSeq::newGetNum(PurchParameters::numRefPurchId()).num());
   axPurchTable.parmPurchaseType(PurchaseType::Purch);
   axPurchTable.parmDocumentStatus(DocumentStatus::PurchaseOrder);
   axPurchTable.parmAccountingDate(systemDateGet());
   axPurchTable.parmDeliveryDate(systemDateGet());
   axPurchTable.parmPurchStatus(PurchStatus::Backorder);
   axPurchTable.save();

   info(strFmt('Create PO %1', Purchtable.PurchId));

   //create PO Line
   purchLine.clear();
   purchLine.initValue();
   purchLine.initFromPurchTable(purchTable);
   purchLine.initFromInventTable(InventTable::find(POGroupLine2.ItemId));
   axPurchLine = AxPurchLine::newPurchLine(purchLine);
   axpurchLine.parmItemId('YOUR ITEMID');
   axPurchLine.parmPurchQty(YOUR QTY);
   axPurchLine.parmPurchPrice(YOUR PRICE);
   axPurchLine.parmInventDimId('YOUR INVENTDIMID');
   axPurchLine.save();

}

Auto Allocate Charges before auto Confirm Purchase Order

static void AutoAllocateCharges_beforeConfirmPO(Args _args)
{
    PurchTable purchTable;
    PurchFormLetter purchFormLetter;
    Args args = new Args();
    MarkupAllocation markupAllocation;

    select firstOnly purchTable where purchTable.PurchId=='1016-PO-14-000191';
    args.record(purchTable);
    //auto allocate charges
    markupAllocation = MarkupAllocation::newMarkupAllocation(args.record());
    markupAllocation.run(args.parmObject());
    
    purchFormLetter = PurchFormLetter::construct(DocumentStatus::PurchaseOrder);
    purchFormLetter.update(purchTable, purchTable.PurchId);
}

Modify Controller Class for Query Based Report SSRS

public void prePromptModifyContract()
{
    //add a range in the report query  
      SrsReportHelper::addParameterValueRangeToQuery(this.getFirstQuery(),tableNum(SSRSReportDemo),fieldNum(SSRSReportDemo, RecId),SysQuery::value(this.parmArgs().record().RecId));
}

source : https://community.dynamics.com/ax/b/dynamics101trainingcenterax/archive/2014/01/30/using-controller-class-in-developing-ssrs-reports-in-microsoft-dynamics-ax-2012.aspx

get product-category Descendants example

public FreeText getDecendants(ItemId _itemid)
{
    str Decendants;
    EcoResProductCategory ecoResProductCategory;
    EcoResCategory        ecoResCategory;

    EcoResCategory        ecoResCategoryAscendants;

    select firstOnly ecoResProductCategory
    where
        ecoResProductCategory.Product==EcoResProduct::findByProductNumber(_itemid).RecId;

    select firstOnly ecoResCategory where ecoResCategory.RecId==ecoResProductCategory.Category;
    Decendants = ecoResCategory.Name;
    while select * from ecoResCategoryAscendants
            order by ecoResCategoryAscendants.NestedSetRight
                where   ecoResCategoryAscendants.CategoryHierarchy == EcoResCategory.CategoryHierarchy
                        && ecoResCategoryAscendants.NestedSetLeft  < EcoResCategory.NestedSetLeft
                        && ecoResCategoryAscendants.NestedSetRight > EcoResCategory.NestedSetRight
    {
       Decendants = strFmt("%1-%2",ecoResCategoryAscendants.Name,Decendants);
    }


    return Decendants;
}

call Manager Authentication Form for AX Retail POS

add reference library InteractionDefault.dll.

use this code below :

using Microsoft.Dynamics.Retail.Pos.Interaction

ManagerAccessForm formManager = new ManagerAccessForm();
var cekDialog = formManager.ShowDialog();
if (cekDialog == DialogResult.OK)
{
   // your logic
}

and dont forget to set ManagerAccessForm() to public on InteractionDefault project.

public ManagerAccessForm()
{
   InitializeComponent();
}

using “update_record” instead of “while select forupdate”

description on MSDN :
“This means that certain tasks may have improved performance by using the power of the SQL server.”

replace code below for a better perfomance.

MyTable myTableBuffer;
;
while select forUpdate mytableBuffer
{
mytableBuffer.field1 = field * 1.10;
}

with


MyTable myTableBuffer;
;
update_recordset myTableBuffer
setting field1 = field1 * 1.10;

source : http://msdn.microsoft.com/en-us/library/aa674382.aspx

How to print barcode Label , SSRS can’t print on Landscape Printer AX 2012r2

Hi there ,
after the report being save to PDF on local drive, i call adobe to print barcode,
i have to did that, because SSRS can’t print on landscape printer.

here is my example code

void clicked()
{
    //AIFD_PrintBarcode
    AI_PrintBarcodeController  aiBarcode = new AI_PrintBarcodeController();
    AI_PrintBarcodeContract    aiContract = new AI_PrintBarcodeContract();
    ProdJournalProd            prodJournalProdLoc;
    SRSPrintDestinationSettings  printSettings = new SRSPrintDestinationSettings();
    PrintJobSettings    printJobSettings = new PrintJobSettings();
    Dialog              dialog = new Dialog();
    DialogField         dialogFileName;
    str                 adobeExe;
    str                 adobeParm;

    str                 fileName;
    int                 fileCount;

    //AIFD
    InteropPermission   permission;
    str errorMessage;
    //END
    real WeightInreal;
    permission = new InteropPermission(InteropKind::ClrInterop);
    permission.assert();

    super();
    try
    {
        prodJournalProdLoc = ProdJournalProd_ds.getFirst(true);

        while (prodJournalProdLoc)
        {
            fileCount++;
            aiBarcode.parmReportName(ssrsReportStr(AI_PrintBarcoderReport,Report));
            aiBarcode.parmShowDialog(false);
            aiContract.parmRecordId(prodJournalProdLoc.RecId);
            aiContract.parmDocumentTitle(&quot;x&quot;);

            //printSettings = aiBarcode.parmReportContract().parmPrintSettings();
            printSettings.printMediumType(SRSPrintMediumType::File);
            //printSettings.landscape(false);
            //printSettings.printerPageSettings('KRRLabelBarcode');
            //info(strFmt(&quot;%1&quot;,printSettings.printerPageSettings()));

            fileName = strFmt(&quot;D:\\barcode%1.pdf&quot;,fileCount);

            printSettings.fileFormat(SRSReportFileFormat::PDF);
            printSettings.fileName(fileName);
            printSettings.printerName(&quot;TOSHIBA B-SA4TP TS&quot;);
            printSettings.landscape(false);
            printSettings.overwriteFile(true);
            aiBarcode.parmReportContract().parmRdpContract(aiContract);
            aiBarcode.parmReportContract().parmPrintSettings(printSettings);

            aiBarcode.startOperation();

            WeightInReal = sleep(500);
            //printJobSettings.printerSettings('SysPrintForm');
            printJobSettings.setTarget(PrintMedium::Printer);
            printJobSettings.preferredTarget(PrintMedium::Printer);
            adobeExe = WinAPI::findExecutable(fileName);

            adobeParm = strFmt(' /t &quot;%1&quot; &quot;%2&quot; &quot;%3&quot; &quot;%4&quot;',
                               fileName,
                               &quot;TOSHIBA B-SA4TP TS&quot;,
                               printJobSettings.printerDriverName(),
                               printJobSettings.printerPortName());

            winAPI::shellExecute(adobeExe,  adobeParm);

            WeightInReal = sleep(500);

            prodJournalProdLoc = ProdJournalProd_ds.getNext();
         }
     CodeAccessPermission::revertAssert();
    }
    catch(Exception::CLRError)
    {
        errorMessage = AifUtil::getClrErrorMessage();

        CodeAccessPermission::revertAssert();

        throw error(errorMessage);
    }
    //END
}

Custom lookup example code x++ AX 2012

Example of custom lookup for itemid on PurchLine , form : PurchTable

public void lookup()
{
 SysTableLookup sysTableLookup = SysTableLookup::newParameters(tablenum(InventTable), this);
 Query query = new Query();
 QueryBuildDataSource queryBuildDataSource, queryBuildDataSource2;
 QueryBuildRange queryBuildRange, queryBuildRange2;
 QueryBuildLink queryBuildLink;
 BuyBackScheme BuyBackScheme;
 str FilterCategory;
 //super();

 queryBuildDataSource = query.addDataSource(tablenum(InventTable));
 //add by Fanddy
 if(InventBuyerGroup::find(PurchTable.ItemBuyerGroupId).InventOwnership == InventOwnership::MD)
 {
 queryBuildDataSource = queryBuildDataSource.addDataSource(tableNum(PdsApprovedVendorList));
 queryBuildDataSource.joinMode(JoinMode::InnerJoin);
 queryBuildDataSource.relations(true);

 queryBuildDataSource.addRange(fieldNum(PdsApprovedVendorList,PdsApprovedVendor)).value(PurchTable.OrderAccount);
 queryBuildDataSource.addRange(fieldNum(PdsApprovedVendorList,ValidFrom)).value(strFmt(“&lt;= %1″,DateTimeUtil::date(DateTimeUtil::removeTimeZoneOffset(PurchTable.createdDateTime,TimeZone::GMTPLUS0700_BANGKOK_HANOI_JAKARTA))));
 queryBuildDataSource.addRange(fieldNum(PdsApprovedVendorList,ValidTo)).value(strFmt(“&gt;= %1″,DateTimeUtil::date(DateTimeUtil::removeTimeZoneOffset(PurchTable.createdDateTime,TimeZone::GMTPLUS0700_BANGKOK_HANOI_JAKARTA))));

 }
 //END by fandd

 sysTableLookup.addLookupfield(fieldnum(InventTable, itemId));
 sysTableLookup.addLookupMethod(‘itemName’);
 sysTableLookup.addLookupMethod(‘itemGroupId’);
 sysTableLookup.addLookupfield(fieldnum(InventTable, NameAlias));
 sysTableLookup.addLookupMethod(tableMethodStr(InventTable,pdsDefaultOrderType));
 sysTableLookup.addLookupfield(fieldnum(InventTable, RetailType));
 sysTableLookup.parmQuery(query);
 sysTableLookup.performFormLookup();

}

happy daxing !

 

Create BOM Journal example via x++ AX 2012

 

static void createMovJournal(Args _args)
{
  InventJournalTable  journalTable;
  InventJournalTrans  journalTrans;
  JournalId           journalId;
  CustomReportFinishedConvertItem    convertItem;
  InventJournalCheckPost journalCheckPost = new InventJournalCheckPost();
  ;

  // Init JournalTable

  journalTable.clear();
  journalTable.initValue();
  journalTable.initFromInventJournalName(InventJournalName::find(InventParameters::find().BomJournalNameId));
  journalId = NumberSeq::newGetNum(InventParameters::numRefInventJournalId()).num();
  journalTable.JournalId = journalId;
  journalTable.insert();

  journalTrans.clear();
  journalTrans.initFromInventJournalTable(journalTable);
  journalTrans.initFromInventTable(InventTable::find(convertItem.ItemIdChild));
  journalTrans.InventDimId = convertItem.InventDimId;
  journalTrans.Qty = convertItem.Qty;
  journalTrans.PriceUnit        = InventTableModule::find(journalTrans.ItemId, ModuleInventPurchSales::Invent).pcsPrice();
  journalTrans.CostAmount       = journalTrans.Qty * journalTrans.PriceUnit;
  journalTrans.InventTransId = NumberSeq::newGetNum(InventParameters::numRefInventTransId()).num();
  journalTrans.TransDate = today();
  journalTrans.Voucher = NumberSeq::newGetNumFromId(journalTable.VoucherNumberSequenceTable).num();
  journalTrans.BOMLine = NoYes::No;
  journalTrans.insert();

  inventTransId    = journalTrans.InventTransId;

  journalTrans.clear();
  journalTrans.initFromInventJournalTable(journalTable);
  journalTrans.initFromInventTable(InventTable::find(convertItem.ItemId));
  journalTrans.InventDimId = convertItem.InventDimIdRAF;
  journalTrans.Qty = -1*convertItem.QtyConversion;
  journalTrans.PriceUnit        = InventTableModule::find(journalTrans.ItemId, ModuleInventPurchSales::Invent).pcsPrice();
  journalTrans.CostAmount       = journalTrans.Qty * journalTrans.PriceUnit;
  journalTrans.InventTransId = NumberSeq::newGetNum(InventParameters::numRefInventTransId()).num();
  journalTrans.TransDate  = today();
  journalTrans.Voucher = NumberSeq::newGetNumFromId(journalTable.VoucherNumberSequenceTable).num();
  journalTrans.BOMLine = NoYes::Yes;
  journalTrans.InventTransIdFather =  inventTransId;
  journalTrans.insert();
  //end edited by fanddy


  // Call the static method to post the journal
  if(InventJournalCheckPost::newPostJournal(journalTable).validate())
  InventJournalCheckPost::newPostJournal(journalTable).run();

}

source :

1.http://www.andesoft.net/creating-bom-journal-using-x-code-ax-2009-ax-2012/

2.http://learnax.blogspot.com/2010/01/x-code-to-create-and-post-inventory.html