Sunday, July 19, 2015

Calling report design based on parameter selected without knowing contract class for AX 2012

Requirement: Calling report design based on parameter selected without knowing contract class.
Brief Steps:
1. Create a method in controller class linked with report which will select report design based on selected parameters.
2. Fetch report name in override method ‘preRunModifyContract’ with the help of method wrote in step 1 and frame report name to its contract class.
[Note: Before use any method for selecting report name we have to specify a report name in main method of controller class for default value.]
Elaborated steps:
Step 1: Create a method in controller class linked with report which will select report design based on selected parameters: 
Here, we have to pick report name based on selected inventory dimensions; below code is returning report design name ssrsReportStr(InventJournalTrans, Report_WL) when only ‘location’ and ‘warehouse’ was ticked at runtime and if any other is also ticked or if not match with this combination then, code is returning report design name ssrsReportStr(InventJournalTrans, Report)
private str getReportName()
{
    str reportNameLocal;

    if( this.parmReportContract().parmRdlContract().getValue('InventLocationId') && this.parmReportContract().parmRdlContract().getValue('WMSLocationId')       &&
        !(this.parmReportContract().parmRdlContract().getValue('ConfigId')       || this.parmReportContract().parmRdlContract().getValue('InventSiteId')        ||
        this.parmReportContract().parmRdlContract().getValue('InventSizeId')    || this.parmReportContract().parmRdlContract().getValue('InventColorId')        ||
        this.parmReportContract().parmRdlContract().getValue('InventStyleId')   || this.parmReportContract().parmRdlContract().getValue('InventProfileId_RU')   ||
        this.parmReportContract().parmRdlContract().getValue('InventOwnerId_RU')|| this.parmReportContract().parmRdlContract().getValue('InventBatchId')        ||
        this.parmReportContract().parmRdlContract().getValue('WMSPalletId')     || this.parmReportContract().parmRdlContract().getValue('InventSerialId')       ||
        this.parmReportContract().parmRdlContract().getValue('InventGTDId_RU')))
    {
        reportNameLocal =ssrsReportStr(InventJournalTrans, Report_WL);
    }
    else
    {
         reportNameLocal = ssrsReportStr(InventJournalTrans, Report);
    }
    return reportNameLocal;
}

Step 2: Fetch report name in override method ‘preRunModifyContract’ with the help of method wrote in step 1 and frame report name to its contract class.

/// <summary>
/// Changes the report contract before it runs the report.
/// </summary>
public void preRunModifyContract()
{
    boolean showLog = false;

    showLog = this.parmReportContract().parmRdlContract().getParameter(#ParameterShowLog).getValueTyped();
    this.processReportParameters(this.parmReportContract().parmQueryContracts().lookup(this.getFirstQueryContractKey()),
        showLog);
    // <GEELV>
    if (isInventJournalTrans_LV)
    {
        this.parmReportContract().parmReportName(ssrsReportStr(InventJournalTrans, Report_LV));
    }
    // </GEELV>
    // <GEERU>
    this.parmReportContract().parmRdlContract().getParameter(#parmISOCode).setValueTyped(SysCountryRegionCode::countryInfo());
    // </GEERU>

    this.parmReportContract().parmReportName(this.getReportName()); // Calling method and framing to its contract class
}

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.