Thursday 19 April 2018

Get First day and Last day of month in Ax

1.The First day Of Month:

static void FirstOfMonth(Args _args)
{
   TransDate TransDate=today();
   TransDate FirstOfMth;
   ;
 
   FirstOfMth=mkdate(1,mthofyr(TransDate),year(TransDate));
 
   info(date2str(FirstOfMth,123,2,2,2,2,4));
}

static void FirstOfMonth(Args _args)
{
   TransDate TransDate=today();
   TransDate FirstOfMth;
   ;
   FirstOfMth=DateStartMth(TransDate);
 
   info(date2str(FirstOfMth,123,2,2,2,2,4));
}


2.The Last day Of Month:

static void LastOfMonth(Args _args)
{
   TransDate TransDate=today();
   TransDate LastOfMth;
   ;
 
   LastOfMth=endmth(TransDate);
 
   info(date2str(LastOfMth,123,2,2,2,2,4));
}

Form templates in Ax 2012 with examples

Form Templates in AX 2012


A primary focus in Dynamics AX 2012 was to increase a developer's productivity by automating common tasks. As part of this initiative, a new feature was introduced in Dynamics AX 2012 in the AOT. This is the creation of new forms using form templates. 

If you right click on the form node, you can see an extra option there - New Form from template

As seen in the image above, you have an option to create seven different form based on the pre-defined templates, these are
  • ListPage
  • DetailsFormMaster
  • DetailsFormTransaction
  • SimpleListDetails
  • SimpleList
  • TableOfContents
  • Dialog
  • DropDialog

Form: redesigned forms – a brand new look in Axapta 2012. 
form is a window that you open from the Microsoft Dynamics AX client workspace that shows information and enables you to perform actions. 
For example, the following figure shows the form you use to view and enter information about a customer. Notice how the form has tabs, fields, and buttons that you can use to view, update, and perform actions with a customer record. 

Form Templates:

To create a form that implements a design pattern, you use a form template.
The template generates a new form that has the basic structure and components specified by the design pattern.
The template reduces the number of steps that you have to complete to create the new form.

List page: Use the template to create a list page you can use to find, analyze, and performs actions on master data.


Example: CustTableListPage To open this form: Under Accounts Receivable, click Common > Customers > All Customers.

DetailsFormMaster(Detail form): Use the template to create a Details form to view, edit, and act on master data.


Example: CustTable. To open this form: Under Accounts Receivable, click Common > Customers > All Customers,and then double-click an entry in the list.

DetailsFormTransaction (Details form with lines): Use the template to create details form with lines to view, edit, and act on master data that has line items.


Example: SalesTable. To open this form: Under Accounts Receivable, click Common > Sales Orders > All Sales Orders, and then double-click an entry in the list.

Dialog: Use the template to create a dialog window that provides a response to a question.




Example: DirPartyQuickCreateForm .To open this form: Under Accounts Receivable, click Common > Customers > All Customers. On the Action pane, in the New group, click Customer.

DropDialog: Use the template to create a drop dialog form to perform an action with data.

Example: HcmWorkerNewWorker. To open this form: Under Human Resources, click Common > Workers > Workers. On the Action pane, in the New group, click Hire New Worker.

Simple List: View, enter, and update records that appear as a list of records in a grid.


Example: CustGroup. To open this form: Under Accounts Receivable, click Setup > Customers > Customer Groups.

SimpleListDetails: Use the template to create a simple list and details form to view, edit, and act on dependent and reference data.




Example: CustPosting To open this form: Under Accounts Receivable, click Setup > Customer Posting Profiles.

TableOfContents: Use the template to create a table of contents form to view and edit configuration or setup data.


Example: CustParameters To open this form: Under Accounts Receivable, click Setup > Accounts Receivable Parameters.

Selected Record (or) multi select records fetch the data in Ax7


FormRun                              formRun = sender.formRun();
FormDataSource                  whsWorkTable_ds = formRun.dataSource(formDataSourceStr(WHSWorkTableListPage,WHSWorkTable)) as FormDataSource; //Form
       
 WHSWorkTable                     rec;     


 for (rec = whsWorkTable_ds.getFirst(true) ? whsWorkTable_ds.getFirst(true) :                      whsWorkTable_ds.cursor();
        rec;
        rec = whsWorkTable_ds.getNext())
        {

         }

Delete Mutliple related tables using Exist Join

 delete_from  inventitemlocation
                                    where  inventitemlocation.inventDimId == inventDim.inventDimId
                                        exists join inventitemlocationcountingstatus
                                              where inventitemlocationcountingstatus.InventDimId == inventitemlocation.inventDimId
                                                 && inventitemlocationcountingstatus.ItemId      == inventitemlocation.ItemId;

MutliSelect Lookup and using flag

class NEC_ItemBlockedDialog extends RunBase
{
    DialogRunbase                   dialog;
    DialogGroup                     dialogGrp;

    FormBuildStringControl          fbsCtrlPromotion;
    FormStringControl               fsCtrlPromotion;
    SysLookupMultiSelectCtrl        msCtrlPromotion;
    container                       msWhcon;
    InventLocationId                msWhStr,warehouse;
    WHSWorkTable                    wHSWorkTable;
    int                             i;
   

    public Object dialog()
    {
        FormBuildControl            setupGroupControl;
        ;

        dialog = super();
         
        dialog.alwaysOnTop(true);
        dialog.windowType(FormWindowType::Workspace);
        dialogGrp = dialog.addGroup('Item Block Dialog');
        setupGroupControl = dialog.formBuildDesign().control(dialogGrp.formBuildGroup().id());
        fbsCtrlPromotion = setupGroupControl.addControl(FormControlType::String, identifierstr(InventLocationId));
        fbsCtrlPromotion.label('Ware House');
        dialog.allowUpdateOnSelectCtrl(true);
        this.dialogSelectCtrl();
         
        return dialog;
    }

    public void dialogPostRun(DialogRunbase _dialog)
    {
        FormRun formRun;
        super(dialog);
         
        formRun = _dialog.dialogForm().formRun();
        if (formRun)
        {
            fsCtrlPromotion = formRun.design().control(fbsCtrlPromotion.id());
            msCtrlPromotion = SysLookupMultiSelectCtrl::constructWithQuery(formRun, fsCtrlPromotion, this.buildQuery());
        }
    }

    public boolean getFromDialog()
    {
        boolean         ret;
        #Characters
       
        ret = super();
        if (msCtrlPromotion)
        {
            msWhcon = msCtrlPromotion.getSelectedFieldValues(); // get actual value of the selected rows
        }
     
        msWhStr    = con2StrUnlimited(msWhcon,#comma);
        this.itemblocking(msWhStr);
        return ret;
    }

    public static void main(Args _args)
    {
        NEC_ItemBlockedDialog           ItemBlock;

        ItemBlock = new NEC_ItemBlockedDialog();
        if(ItemBlock.prompt())
        {
            ItemBlock.run();
        }
    }

    public query buildQuery()
    {
        Query                           q  = new Query();
        QueryBuildDataSource            qbds;
     
        ;
       
        qbds    = q.addDataSource(tablenum(WHSWorkTable));
        qbds.addSelectionField(fieldNum(WHSWorkTable, InventLocationId));
        qbds.addSortField(fieldNum(WHSWorkTable,InventLocationId));
        qbds.orderMode(OrderMode::GroupBy);
        while select wHSWorkTable
        {
            qbds.addRange(fieldNum(WHSWorkTable,InventLocationId)).value(wHSWorkTable.InventLocationId);
           
        }
        return q;
    }

    public void itemblocking(InventLocationId    _inventLocationId)
    {
        container                           con,c;
        WHSWorkLine                         wHSWorkLine;
        InventTable                         inventTable;
        InventDim                           inventDim;



        InventItemLocation                  inventItemLocation;
        InventItemLocationCountingStatus    inventItemLocationCountingStatus;
        int                                 value;
        str                                 locationid,itemnum,text;
        boolean                             blocked;
        boolean                             unBlocked;
        boolean                             statusClose;

        con = str2con(_inventLocationId);
        for(i=1;i<=conLen(con);i++)
        {
            blocked     = false;
            unBlocked   = false;
            statusClose = false;
            value       = 0;
            warehouse   = conPeek(con,i);

            while select  wHSWorkTable 
                where wHSWorkTable.InventLocationId == warehouse
                && wHSWorkTable.WorkStatus == WHSWorkStatus::Open
                && wHSWorkTable.WorkTransType == WHSWorkTransType::CycleCount
                join wHSWorkLine
                    where wHSWorkLine.WorkId == wHSWorkTable.WorkId
                    join inventDim
                    where inventDim.inventDimId == wHSWorkLine.InventDimId
            {
                if(inventDim.inventDimId)
                {
                    value = 1;
                    select inventItemLocation
                            where  inventItemLocation.inventDimId == inventDim.inventDimId
                                join inventItemLocationCountingStatus
                                      where inventItemLocationCountingStatus.InventDimId == inventItemLocation.inventDimId
                                         && inventItemLocationCountingStatus.ItemId      == inventItemLocation.ItemId;
                                           
                     if(!inventItemLocation.inventDimId)
                    {
                        inventItemLocation.CountGroupId = 'Default';
                        inventItemLocation.inventDimId  = inventDim.inventDimId;
                        inventItemLocation.ItemId       = wHSWorkLine.ItemId;
                        inventItemLocation.insert();

                        inventItemLocationCountingStatus.CountingStarted = NoYes::Yes;
                        inventItemLocationCountingStatus.InventDimId     = inventDim.inventDimId;
                        inventItemLocationCountingStatus.ItemId          = wHSWorkLine.ItemId;
                        inventItemLocationCountingStatus.insert();

                        blocked = true;
                    }
                    else
                    {
                        unBlocked = true;
                    }
                }
            }
            if(!inventDim.inventDimId && value == 0)
            {
                 statusClose = true;
            }
            if (blocked)
            {
                info(strFmt("Items are blocked for selected Warehouse: %1", warehouse));
            }
            if (unBlocked)
            {
                warning(strFmt(" Items are already blocked for selected Warehouse: %1", warehouse));
            }
            if (statusClose)
            {
                warning(strFmt("No items for selected Warehouse: %1 with status 'Open' and type 'Cycle counting'", warehouse));
            }
        }
    }

}