Wednesday, February 18, 2015

Filter by field and Filter by selection on a display method in AX 2009

This achievement is not my invention as followed from another blog. This was very good and worked for me. :)  
Requirement:Add options "Filter By Field", "Filter By Selection" and "Remove Filter" in a display field "Name".

Steps:
1. Make "Name" control as Auto declaration to YES
2. Override context method in the same control and paste the following code: This form is having data from Employee details.
public void context()
{
    int                     selectedMenu;
    formrun                 SearchformRun;
    Args                    arg;
    Name                    strtext;
    querybuilddataSource    querybuilddataSource;
    queryrun                qr;
    query                   q;
    PopupMenu menu = new PopupMenu(element.hWnd());
    int a = menu.insertItem('Filter By Field');
    int b = menu.insertItem('Filter By Selection');
    int c = menu.insertItem('Remove Filter');
    ;
    q   = EmplTable_ds.query();
    querybuilddataSource = q.dataSourceTable(tablenum(EmplTable));
    querybuilddataSource = querybuilddataSource.addDataSource(TableNum(DirPartyTable));
    querybuilddataSource.addLink(FieldNum(EmplTable,PartyId),FieldNum(DirPartyTable,PartyId));

    selectedMenu = menu.draw();
    switch(selectedMenu)
    {
        case -1: //Filter by field
            break;
        case a:
                arg = new args('SysformSearch');
                SearchformRun = new formrun(arg);
                SearchformRun.run();
                SearchformRun.wait();
                //Reading User entered value for filter process
                strtext = SearchformRun.design().controlName('FindEdit').valueStr();
                if(strtext)
                {
                //Creating a query for filter

                    querybuilddataSource.addRange(FieldNum(DirPartyTable,Name)).value(strtext);
                    EmplTable_ds.query(Q);
                    EmplTable_ds.executeQuery();
                }
                break;

        case b:                                      // Filter By Selection

                querybuilddataSource.addRange(FieldNum(DirPartyTable,Name)).value(smylEmplName.valueStr());
                    EmplTable_ds.query(Q);
                    EmplTable_ds.executeQuery();
                break;

        case c :                                      // Remove Filter
                q   = new Query();
                querybuilddataSource = q.addDataSource(tablenum(EmplTable));
                querybuilddataSource.clearLinks();
                querybuilddataSource.clearRanges();
                EmplTable_ds.query(Q);
                EmplTable_ds.removeFilter();
                break;

        Default:
                break;
    }
}

Wednesday, March 12, 2014

Import excel sheet in AX 2009 using RunbaseBatch class.

Requirement: Import excel sheet in AX 2009 using RunbaseBatch class.
Brief Steps:
1.       Create a table or you can use existing if possible.
2.       Create a class which performs all operations.
3.       .xls file pattern.
Elaborated Steps:
1.       Create a table with 5 fields:
Create a table in AOT with name “TestImportTable” having required field which you want to print in your report.
Field
Type(EDT, if available)
EmplId
String (EmplId)
Name
String(Name)
WorkingDate
Date(TransDate)
WorkingHrs
Real
ProjectNo
String

2.
       Create a Class which performs all operations.

class testImport extends RunbaseBatch
{
        DialogField                 dialogFileName;
        FileNameOpen            fileName;
        FileIOPermission          permission;
       
        TestImportTable            importTable;
       
        #File
        #avifiles
        
       #define.CurrentVersion(1)
       #define.Version1(1)
       #localmacro.CurrentList
            fileName
       #endmacro
}
public Object dialog()
{
        DialogRunbase       dialog = super();
        ;
        dialogFileName = dialog.addField(typeid(FileNameOpen));
        return dialog;
}
public Object dialog()
{
        DialogRunbase       dialog = super();
        ;
        dialogFileName = dialog.addField(typeid(FileNameOpen));
        return dialog;
}
public boolean getFromDialog()
{
        boolean ret;
       
        ret = super();
        fileName =  dialogFileName.value();
        return ret;
}
// BP Deviation documented
void importFromXSLFile()
{
        Sysexcelapplication   excelapp=sysexcelapplication::construct();
        Sysexcelworksheet         excelworksheet;
        Sysexcelrange                 excelrange;
        Sysexcelcells                  excelcells;       
        SysOperationProgress    simpleProgress;
        int                                    j =1;
        ;
        excelapp.workbooks().open(fileName);
        excelworksheet = excelapp.worksheets().itemFromNum(1);
        excelcells =excelworksheet.cells();       
        simpleProgress  = SysOperationProgress::newGeneral(#AviUpdate,"Import is in progress",100);
        startLengthyOperation();
        do
       {
               importTable .EmplId =  (excelcells.item(j,1).value().bStr());
               importTable .Name =  (excelcells.item(j,1).value().bStr());
               importTable .WorkingDate =  (excelcells.item(j,1).value().Date());
               importTable .WorkingHrs =  (excelcells.item(j,1).value().Double());
               importTable .ProjectNo =  (excelcells.item(j,1).value().bStr());      
       }
       // exclude first line of excel
        while(excelcells.item(j+1 ,1).value().bStr());
        endLengthyOperation();
}
public container pack()
{
    return [#CurrentVersion,#CurrentList];
 }
public boolean unpack(container packedClass)
{
        Version version = RunBase::getVersion(packedClass);
        ;
        switch (version)
    {
                case #CurrentVersion:
        [version,#CurrentList] = packedClass;
        break;
        default:
        return false;
   }
   return true;
}
public void run()
{
    #OCCRetryCount
    ;
    if (! this.validate())
          throw error("Import has been cancel");
        try
       {
                this.importFromXLSFile(); //Import records of xls file
       
        }
        catch(Exception::Deadlock)
        {
            retry;
        }
        catch(Exception::Error)
        {
                        info ('Import cancelled');
        }
        catch (Exception::UpdateConflict)
        {
                        if (appl.ttsLevel() == 0)
            {
                             if (xSession::currentRetryCount() >= #RetryNum)
                 {
                        throw Exception::UpdateConflictNotRecovered;
                 }
                 else
                 {
                        retry;
                 }
             }
             else
             {
                    throw Exception::UpdateConflict;
             }
        }       
}
public boolean validate()
{
    boolean     ret = true;
    Container   checkType;
    if (! filename)
    {
                ret = checkFailed("Please specify file");
    }
    checkType =  Docu::splitFilename(filename);
    if(Conpeek(checkType,2) != 'xls' || Conpeek(checkType,2) != 'XLS')
    {
          ret = checkFailed("file format is incorrect");
    }     
return ret;
}
static void main(Args args)
{
    TestImport             importClass;
    ;
        importClass = new TestImport ();
    if(TestImport.prompt());
            TestImport.run();
}


     


     


























































































































































3.       .xls file pattern.
You can make desire pattern or for this code you can use below one.