Wednesday, December 9, 2015

X++ code to change the table field properties

static void Esh_Test(Args _args)
{
 str myProp,myProp1;

 #AOT


 NumberSequenceList   numberSequenceList; //Table Name

 TreeNode            numSeqFldsRoot = treeNode::findNode(@"\Data Dictionary\Tables\NumberSequenceList\Fields\Status"); //Field Path

 TreeNode           numSeqFldsRoot1 = treeNode::findNode(@"\Data Dictionary\Tables\NumberSequenceList\Fields\TransId"); //Field Path

 TreeNode           table = treeNode::findNode(@"\Data Dictionary\Tables\NumberSequenceList"); //Table Path

;


  numSeqFldsRoot.AOTsetProperties("PROPERTIES\n AllowEdit #" + 'No' + "\n ENDPROPERTIES\n"); // We use AOTsetProperties method to  set or manipulate the field properties.


  numSeqFldsRoot1.AOTsetProperties("PROPERTIES\n AllowEdit #" + 'No' + "\n ENDPROPERTIES\n");



  myProp = numSeqFldsRoot1.AOTgetProperties();
  myProp1 = numSeqFldsRoot.AOTgetProperties();

  numSeqFldsRoot.AOTsave();
  numSeqFldsRoot1.AOTsave();
  table.AOTsave(); // Saving the table.

  info(myProp);
  info(myProp1);
   
 
}

Wednesday, December 2, 2015

Multi-Select Lookup Control and grid filteration Using X++

1.Create a Form

2.Go to Design node and then select a StringEdit Control

3.Name the control as Multilookup - Set auto declaration to Yes to access the control using X++

4.Add a button in order to get values on button click

5.Now open the Class Declaration node of the form and write the following code into it

   SysLookupMultiSelectCtrl msCtrl;

6.Override the init method of the form and write the following code in it


Query query = new Query();

QueryBuildDataSource qbds;

super();

//creating query to show customer account and customer name

//the query must contain only those fields that should be visible on the lookup

qbds = query.addDataSource(tableNum(CustTable));

qbds.fields().dynamic(YesNo::No);

qbds.fields().addField(fieldNum(CustTable,AccountNum));

qbds = qbds.addDataSource(tableNum(DirPartyTable));

qbds.fields().dynamic(YesNo::No);

qbds.fields().addField(fieldNum(DirPartyTable,Name));

qbds.relations(true);

//assigning control and query to the class

msCtrl = SysLookupMultiSelectCtrl::constructWithQuery(element, MultiLookup, query);


Note :- if you are using the AOT query you can using the below code

msCtrl = SysLookupMultiSelectCtrl::construct(element, MultiLookup, queryStr(queryname));


7.Now  override the clicked method of the Button

void clicked()
{
    CustTrans           custtrans1;
    CustTable           custtable;
    DirPartyTable       dir;
    List                strlist   = new List(Types::String);
    ListIterator        iterator;
    container           packedList, c,v;
    int i;


    ;

    strlist=strSplit(CustTrans_AccountNum.valueStr(),";");
    iterator = new ListIterator(strlist);
    while(iterator.more()) // Additionally, to move to the next item in a list, you must explicitly call the more and next methods if you are using a list iterator.

    {
       //filtering the grid based on the selected customers and Trans Date.
        while select custtrans1
        where custtrans1.AccountNum ==  iterator.value()               
         && custtrans1.TransDate >= Proj_CustTrans_kkr_FromDate.dateValue()
         && custtrans1.TransDate <= Proj_CustTrans_kkr_ToDate.dateValue()

        {
            Proj_CustTrans_kkr.CustAccount = custtrans1.AccountNum;
            Proj_CustTrans_kkr.Name = DirPartyTable::findRec(CustTable::find(custtrans1.AccountNum).Party).Name;
            Proj_CustTrans_kkr.AmountCur = custtrans1.AmountCur;
            Proj_CustTrans_kkr.TransDate = custtrans1.TransDate;
            Proj_CustTrans_kkr.insert();
        }
        iterator.next(); // Additionally, to move to the next item in a list, you must explicitly call the more and next methods if you are using a list iterator.
    }

    Proj_CustTrans_kkr_ds.research();
    Proj_CustTrans_kkr_ds.refresh();
    CustTrans_ds.research();
    CustTrans_ds.refresh();

    super();


}


Tuesday, December 1, 2015

Importing Unit of Measures of AX 2009 to 2012 using EXCEL.

UNIT_Excel (Importing Units of 2009)
==============================
static void Unit_Excel(Args _args)
{
    Dialog dialog;
    DialogField dialogfield;
    Filename filename;

    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;

    COMVariantType type;
    int row;
    UnitOfMeasure unitofmeasure,UnitFM;

    ;

    dialog = new dialog("FileOpen");
    dialogfield = dialog.addField(extendedTypeStr(FilenameOpen), 'File Name');
    dialog.run();

    if(dialog.run())
    {
     filename =(dialogfield.value());
    }

    row=1;
    application = SysExcelApplication::construct();
    workbooks = application.workbooks();

    try
    {
        workbooks.open(filename);
    }
    catch (Exception::Error)
    {
        throw error("File cannot be opened.");
    }

     workbook = workbooks.item(1);
    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(1);
    cells = worksheet.cells();
    do
    {
    row++;
    unitofmeasure.DecimalPrecision=any2int(cells.item(row,1).value().double());
    unitofmeasure.Symbol=cells.item(row,2).value().bStr();
    unitofmeasure.SystemOfUnits=any2int(cells.item(row,3).value().double());
    unitofmeasure.UnitOfMeasureClass=any2int(cells.item(row,4).value().double());
    UnitFM=unitofmeasure::findBySymbol(unitofmeasure.Symbol,true);
        if(UnitFM)
    {
        ttsBegin;
        UnitFM.selectForUpdate(true);
        UnitFM.update();
        ttsCommit;
    }
    else
        unitofmeasure.insert();

    info(strfmt('%1 ', unitofmeasure.RecId ));
    type = cells.item(row+1, 1).value().variantType();

    }
    while (type != COMVariantType::VT_EMPTY);
    application.quit();

}

-===================================================================
UOM_Excel ( Importing Unit of Measures of 2009)
====================================================================

static void UOM_Excel(Args _args)
{
    Dialog dialog;
    DialogField dialogfield;
    Filename filename;

    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;

    COMVariantType type;
    int row,cnt=0;
    UnitOfMeasureConversion unitOfMeasureConversion, unitofmeasure;
    ;

    dialog = new dialog("FileOpen");
    dialogfield = dialog.addField(extendedTypeStr(FilenameOpen), 'File Name');
    dialog.run();

    if(dialog.run())
    {
     filename =(dialogfield.value());
    }

    row=1;
    application = SysExcelApplication::construct();
    workbooks = application.workbooks();

    try
    {
        workbooks.open(filename);
    }
    catch (Exception::Error)
    {
        throw error("File cannot be opened.");
    }

    workbook    = workbooks.item(1);
    worksheets  = workbook.worksheets();
    worksheet   = worksheets.itemFromNum(1);
    cells       = worksheet.cells();
    do
    {
        row++;
        unitOfMeasureConversion.Product             = EcoResProduct::findByProductNumber(cells.item(row, 1).value().bStr()).Recid;
        unitOfMeasureConversion.ToUnitOfMeasure     = UnitOfMeasure::findBySymbol(cells.item(row, 4).value().bStr()).RecId;

        unitOfMeasureConversion.Factor              = cells.item(row, 3).value().double();

        unitOfMeasureConversion.Numerator           = 1;
        unitOfMeasureConversion.Denominator         = 1;
        unitOfMeasureConversion.FromUnitOfMeasure   = UnitOfMeasure::findBySymbol(cells.item(row, 2).value().bStr()).RecId;
        unitOfMeasureConversion.Rounding            = UnitofMeasureconversionrounding::Nearest;//cells.item(row, 5).value().bStr();
        unitofmeasure = UnitOfMeasureConversion::findByConversion(unitOfMeasureConversion.FromUnitOfMeasure, unitOfMeasureConversion.ToUnitOfMeasure,unitOfMeasureConversion.Product);
        if( unitOfMeasureConversion.ToUnitOfMeasure!=0 &&  unitOfMeasureConversion.FromUnitOfMeasure!=0)
         {
          if(unitofmeasure)
          {
            ttsBegin;
            unitofmeasure.selectForUpdate(true);
            unitofmeasure.update();
            ttsCommit;
          }
          else
             {
            unitOfMeasureConversion.insert();
                 cnt++;
             }
          info(strfmt(' Conversions imported for the products=%1 ', EcoResProduct::findByProductNumber(cells.item(row, 1).value().bStr()).DisplayProductNumber ));

         }
        else
        {
            info(strfmt('Conversions are not imported for products=%1 ',  EcoResProduct::findByProductNumber(cells.item(row, 1).value().bStr()).DisplayProductNumber ));

        }
         type = cells.item(row+1, 1).value().variantType();
    }
    while (type != COMVariantType::VT_EMPTY);
    info(strFmt('No Of Conversions imported are=%1',cnt));
    application.quit();


}

--------------------------------------------------------------------------------------------------------------------------

Below is the method to check the Text format


static void UOM_Excel(Args _args)
{
    Dialog                         dialog;
    DialogField                 dialogfield;
    Filename                    filename;

    SysExcelApplication  application;
    SysExcelWorkbooks  workbooks;
    SysExcelWorkbook    workbook;
    SysExcelWorksheets  worksheets;
    SysExcelWorksheet   worksheet;
    SysExcelCells              cells;
 
    EcoResProduct          ecoResProduct;
    InventTable                inventtable;
    ItemId                        itemid;
 
    COMVariantType      type;
    int                              row,cnt=0;
    UnitOfMeasureConversion unitOfMeasureConversion, unitofmeasure;
    ;

    dialog = new dialog("FileOpen");
    dialogfield = dialog.addField(extendedTypeStr(FilenameOpen), 'File Name');
    dialog.run();

    if(dialog.run())
    {
     filename =(dialogfield.value());
    }

    row=1;
    application = SysExcelApplication::construct();
    workbooks = application.workbooks();

    try
    {
        workbooks.open(filename);
    }
    catch (Exception::Error)
    {
        throw error("File cannot be opened.");
    }

    workbook    = workbooks.item(1);
    worksheets  = workbook.worksheets();
    worksheet   = worksheets.itemFromNum(1);
    cells       = worksheet.cells();
    do
    {
        row++;
     
     
        switch(cells.item(row, 1).value().variantType())
        {
        case COMVariantType::VT_BSTR:
            itemid = strFmt("%1", cells.item(row, 1).value().bStr());
            break;
        case COMVariantType::VT_DECIMAL, COMVariantType::VT_R4, COMVariantType::VT_R8:
            itemid = strFmt("%1", any2int(cells.item(row, 1).value().double()));
            break;
        case COMVariantType::VT_I1, COMVariantType::VT_I2, COMVariantType::VT_I4:
            itemid = strFmt("%1", cells.item(row, 1).value().int());
            break;
        case COMVariantType::VT_UI1, COMVariantType::VT_UI2, COMVariantType::VT_UI4:
            itemid = strFmt("%1", cells.item(row, 1).value().uLong());
            break;
         case COMVariantType::VT_EMPTY:
            itemid = '';
            break;
        default:
            throw error(strfmt('Unhandled variant type (%1).', cells.item(row, 1).value().variantType()));
        }

     
     
     
        if(itemid)
        {
         
        inventtable = InventTable::find(itemid);  
         
        unitOfMeasureConversion.Product             = inventtable.Product;
        unitOfMeasureConversion.ToUnitOfMeasure     = UnitOfMeasure::findBySymbol(cells.item(row, 4).value().bStr()).RecId;

        unitOfMeasureConversion.Factor              = cells.item(row, 3).value().double();

        unitOfMeasureConversion.Numerator           = 1;
        unitOfMeasureConversion.Denominator         = 1;
        unitOfMeasureConversion.FromUnitOfMeasure   = UnitOfMeasure::findBySymbol(cells.item(row, 2).value().bStr()).RecId;
        unitOfMeasureConversion.Rounding            = UnitofMeasureconversionrounding::Nearest;        unitofmeasure = UnitOfMeasureConversion::findByConversion(unitOfMeasureConversion.FromUnitOfMeasure, unitOfMeasureConversion.ToUnitOfMeasure,unitOfMeasureConversion.Product);
     
        if( unitOfMeasureConversion.ToUnitOfMeasure!=0 &&  unitOfMeasureConversion.FromUnitOfMeasure!=0)
         {
          if(unitofmeasure)
          {
            ttsBegin;
            unitofmeasure.selectForUpdate(true);
            unitofmeasure.update();
            ttsCommit;
          }
          else
             {
            unitOfMeasureConversion.insert();
                 cnt++;
             }
          info(strfmt(' Conversions imported for the products=%1 ', inventtable.ItemId));

         }
        else
        {
            info(strfmt('Conversions are not imported for products=%1 ',  inventtable.ItemId));

        }
        }
        else
        {
            info(strfmt('Item not found: %1',cells.item(row, 1).value().bStr()));
        }
         type = cells.item(row+1, 1).value().variantType();
    }
    while (type != COMVariantType::VT_EMPTY);
    info(strFmt('No Of Conversions imported are=%1',cnt));
    application.quit();


}


How to enable the dimension fields based on the Item selected on the form.

[Form] public class KMTShipFromWarehouses extends FormRun {     InventDimCtrl_Frm_EditDimensions        inventDimFormSetup;     /// ...