Wednesday, November 22, 2017

Simple Dialog box to browse the file using X++

static void Simple_Dialog(Args _args)
{   
      dialog              dialog;   
      dialogGroup    dialogGroup;   
      dialogField      dialogField;
      Filename         filename;     

     dialog            = new Dialog("Simple Dialog");  //Header 
     dialogGroup  = dialog.addGroup("Customer");  // Group Name 
     dialogField    = dialog.addField(extendedTypeStr(FilenameOpen));    //FilenameOpen is an EDT
   
     if (dialog.run())   
    {
     
      filename = dialogField.value();
   
           if(filename)
            {
               print dialogField.value();
               pause; 
             }
            else
             {
               info("file does not exists")
     }
}


///The dialog.run() method returns true if OK is clicked, and false if Cancel is clicked. 

Errors :

Error code : The method typid has not been declared.

Sol :-  In AX 2012 typeid should be replaced to extendedTypeStr.

Simple Excel Import to the table using X++ job

static void Esh_ReadExcel(Args _args)
{

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

int row=1;
Str Emp_Id,Emp_Name,Emp_Profile;
FileName filename;
Esh_ExcelUpload esh_ExcelUpload; //table buffer
;
application = SysExcelApplication::construct();
workbooks = application.workbooks();

//specify the file path that you want to read
filename = "D:\\ExcelUpload.xlsx";

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
{
    //read data from Excel
    row++;
    Emp_Id = cells.item(row, 1).value().bStr();
    Emp_Name = cells.item(row, 2).value().bStr();
    Emp_Profile = cells.item(row, 3).value().bStr();
    info(strfmt('%1 - %2', Emp_Id, Emp_Name,Emp_Profile));
    type = cells.item(row+1, 1).value().variantType();
 
    //insert data into table
    esh_ExcelUpload.clear();
    esh_ExcelUpload.Emp_Id      = Emp_Id;
    esh_ExcelUpload.Emp_Name    = Emp_Name;
    esh_ExcelUpload.Emp_Profile = Emp_Profile;
    esh_ExcelUpload.insert();
}
 
while (type != COMVariantType::VT_EMPTY);
application.quit();
}

How to dynamically change the Field properties based on Enum Value using X++

Write the below code in Class declaration :

public class FormRun extends ObjectRun
{
    #AOT
    TreeNode treeMenuItem,treeForm,table;
 }


-------------------------------------------------------------------------------------------------------------------
Write the below code in combo box SelectionChange Method :

public int selectionChange()
{
    int ret;

    ret = super();

    if(this.selection()== Account_Type::Customer)
    {
     treeForm = treeNode::findNode(@"\Data Dictionary\Tables\Esh_Test\Fields\AccountNum\");
     treeForm.AOTsetProperties("Properties\n ExtendedDataType #" + 'CustAccount' + "\n ENDPROPERTIES\n");
     table = treeNode::findNode(@"\Data Dictionary\Tables\Esh_Test");
     treeForm.AOTSave();   
     table.AOTsave();          // To save the table
     Esh_Test_ds.refresh(); // To refresh the changes made in form data source
    }
    else if(this.selection()== Account_Type::Vendor)
    {
     treeForm = treeNode::findNode(@"\Data Dictionary\Tables\Esh_Test\Fields\AccountNum\");
     treeForm.AOTsetProperties("Properties\n ExtendedDataType #" + 'VendAccount' + "\n ENDPROPERTIES\n");
     table = treeNode::findNode(@"\Data Dictionary\Tables\Esh_Test");
     treeForm.AOTSave();   
     table.AOTsave();          // To save the table
     Esh_Test_ds.refresh(); // To refresh the changes made in form data source
    }
 
    return ret;
}

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

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