Skip to main content

Creating BOM journal using code AX 2012

courtesy:
http://www.andesoft.net/creating-bom-journal-using-x-code-ax-2009-ax-2012/
https://community.dynamics.com/ax/f/33/t/96262.aspx
http://learnax.blogspot.com/2010/01/x-code-to-create-and-post-inventory.html

static void createBomJournal_working(Args _args)
{
    InventJournalTable      journalTable;
    InventJournalTrans      journalTrans;
    InventJournalTableData  journalTableData;
    InventJournalTransData  journalTransData;
    InventTable             inventTable;
    InventDim               inventDim;
    Counter                 cnt;
    InventJournalCheckPost  journalCheckPost = new InventJournalCheckPost();

    InventTransId           inventTransId, inventTransIdFather;
    InventBatch             inventBatch;
    ;

    journalTableData = JournalTableData::newTable(journalTable);
    journalTransData = journalTableData.journalStatic().newJournalTransData(journalTrans,journalTableData);

    // Init JournalTable
    journalTable.clear();

    journalTable.JournalId = journalTableData.nextJournalId();
    journalTable.JournalType = InventJournalType::BOM;
    journalTable.JournalNameId = 'BomJrnl';//journalTableData.journalStatic().standardJournalNameId(journalTable.JournalType);

    journalTableData.initFromJournalName(journalTableData.journalStatic().findJournalName(journalTable.JournalNameId));

    // Init JournalTrans

    for(cnt=1; cnt<3; cnt++)
    {
        if (cnt == 1)
        {
            select firstonly inventTable
                where inventTable.ItemId == 'itemName';
        }
        else if (cnt == 2)
        {
            select firstonly inventTable
                where inventTable.ItemId == 'itemName2';
        }

        journalTrans.clear();
        journalTransData.initFromJournalTable();

        journalTrans.TransDate = systemdateget() + 1 div 2;
        journalTrans.ItemId = inventTable.ItemId;

        if (cnt == 1)
        {
            journalTrans.Qty = 26;

            inventDim.clear();
            inventDim.InventColorId = 'color';
            inventDim.InventStyleId = 'style';
            inventDim.InventSizeId = 'size';
            inventDim.InventSiteId = 'site';
            inventDim.InventLocationId = 'whs';
            inventDim.wMSLocationId = InventLocation::find(inventDim.inventLocationId).WMSLocationIdDefaultReceipt;
            if(!inventDim.inventBatchId)
            {
                select firstOnly inventBatch where inventBatch.itemId == journalTrans.ItemId;
                inventDim.inventBatchId = inventBatch.inventBatchId;
            }
        }
        else if (cnt == 2)
        {
            journalTrans.Qty = qty * -1;

            inventDim.clear();
            inventDim.InventColorId = 'clr';
            inventDim.InventStyleId = 'sty';
            inventDim.InventSizeId = 'sz';
            inventDim.InventSiteId = 'st';
            inventDim.InventLocationId = 'lct';
            inventDim.wMSLocationId = 'loc';
//InventLocation::find(inventDim.inventLocationId).WMSLocationIdDefaultReceipt;
            if(!inventDim.inventBatchId)
            {
                select firstOnly inventBatch where inventBatch.itemId == journalTrans.ItemId;
                inventDim.inventBatchId = inventBatch.inventBatchId;
            }
        }

        inventDim = InventDim::findOrCreate(inventDim);
        journalTrans.InventDimId = inventDim.InventDimId;

        if (cnt == 2)
        {
            journalTrans.BOMLine = true;//NoYes::Yes;
            journalTrans.LineNum = -1;
            journalTrans.InventTransIdFather = inventTransIdFather;
        }

        journalTrans.setCostPrice(journalTrans.InventDimId);

        journalTransData.create();
        inventTransId = journalTrans.InventTransId;

        if (cnt == 1)
        {
            inventTransIdFather = inventTransId;
        }

        //this.pickItem(salesLine.SalesId,salesLine.InventTransId,tmp.QtySelected,inventDim.inventBatchId,inventDim.wMSLocationId);
        //journalTrans.clear();
    }

    journalTable.insert();

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


Comments

Popular posts from this blog

Get Position and Department of an employee | AX 2012

A simple job to get position/designation and department of an employee using employee id: static void WorkerPositionDepartment(Args _args) {     HcmWorker                       hcmWorker;     HcmPositionWorkerAssignment     hcmPositionWorkerAssignment;     OMOperatingUnit                 oMOperatingUnit;     HcmPositionDetail               hcmPositionDetail;     HcmJob                          hcmJob;     HcmPosition             ...

Run SSRS report using x++ code | AX 2012

This post describes how to run an SSRS report through x++ code and passing report parameters as well. Its a simple code and comments are added for further ease in code understanding! public static void runSSRSReport() {     SrsReportRunController controller;             controller = new SrsReportRunController();     controller.parmLoadFromSysLastValue(false);      // write report name and its design in quotes that you want to run     controller.parmReportName("ReportName.DesignName");      // set additional, optional properties like setting landscape to true     controller.parmReportContract().parmPrintSettings().landscape(true);          // set print destination screen/printer/file etc.     controller.parmReportContract().parmPrintSettings().printMediumType (SRSPrintMediumType::S...

How to pass value from form to class, AX 2012

Courtesy : DUG Dynamics User Group It's very simple to pass value from a form to a class by defining a method on form and using args.caller() in the class. 1. Take a String control in the form design like  string Customer Name. 2. Go to String control properties and set "Auto declaration" property to "Yes". 3. Go to form methods -> new method -> write return type method like;           public str customerName()      {            return CustomerName.Text();      } 4. Go to main() method of the class and type following:     public static void main(Args args)     {            str custName;            ;                if(formHasMethod(a...