Contact Me

Total Pageviews

Wednesday 3 September 2014

Creating a Report Using Temporary Table and Class in Axapta

Hi guys,

Today I am going to create a Report which uses a Temporary table as a datasource and will generate that report through a Class using Args.

Objective: Create a class which displays a dialog box with Customer Account field ,after selecting the account number in the dialog the corresponding transactions for that account are displayed in a Report.

Steps:

1)  Create a Temporary table "TempCustTrans" and add two fields to it : "AccountNum" and "AmountMST" as shown below:



2)Create a class "GenerateCustTrans"  and add following methods to it:

class GenerateCustTrans extends Runbase
{
    DialogRunbase                       dialogRunbase;
    CustTrans                               custTransList;
    TmpCustTrans                        tmpCustTransList;
    Boolean                                   printReport;
    dialogField                              custAccount;
    CustAccount                           accountNum;
}


Override the dialog method of the Runbase class and add Customer account as Dialog field.

protected Object dialog(DialogRunbase dialog, boolean forceOnClient)
{
    ;
    dialogRunbase = super();

    dialogRunbase.caption("Select Account Number");
    custAccount          = dialogRunbase.addField(typeid(CustAccount), "Account Number");
    return dialogRunbase;
}


public boolean getFromDialog()
{
    ;
    accountNum          = custAccount.value();
    return super();
}


Override pack/Unpack methods of the class

public container pack()
{
    return connull();
}


public boolean unpack(container packedClass)
{
    return true;
}


The main() method of the class would be:

public static void main(Args args)
{
    GenerateCustTrans               generatecusttrans;
    CustTrans                       custtranslist;

    ;

    generatecusttrans = new  GenerateCustTrans();
    if (generatecusttrans.prompt())
    {
      generatecusttrans.run(); // call the run method
    }

}


and the run() method is :

public void run()
{

    ;

    try
    {
        ttsbegin;
        this.generateCustTrans(accountNum); //Fill the Temporary Table and pass the 

                                                                        selected dialog value as a parameter
        ttscommit;
        if(printReport)
            this.displayReport();  //Call the Report from the class
        else
            info('Report is empty');
    }
    catch (Exception::Deadlock)
    {
        retry;
    }
    catch (Exception::Error)
    {
        ttsabort;
        throw error("Operation failed");
    }



void generateCustTrans(CustAccount  _accountNum)
{
 // Fill the temporary table from "CustTransList" table for the selected account number.
  while select custTransList where custTransList.AccountNum == _accountNum
  {
    tmpCustTransList.clear();
    tmpCustTransList.AccountNum      =  custtranslist.AccountNum;
    tmpCustTransList.AmountMST       =  custtranslist.AmountMST;
    tmpCustTransList.insert();
    printReport = true;
  }

}

and finally now call the report from this class :

void displayReport()
{
    MenuFunction                   markedReport;
    Args                                   reportArgs;
    ;

    markedReport    =   new MenuFunction(identifierstr('CustomerTransactions'), MenuItemType::Output));  
    reportArgs      =   new args();
    reportArgs.caller(this);
    reportArgs.record( tmpcustTransList);
    markedReport.run(reportArgs);
}



3) The Final step is to create a report "CustomerTransactions" with some methods as following:

public class ReportRun extends ObjectRun
{
  CustTrans     custTrans;
  TmpCustTrans  tmpcustTrans;
}


// Override the fetch() method and select select data from the temporary table which was filled by the class

public boolean fetch()
{
    boolean             retCode = false;
    ;
    while select * from tmpcustTrans
    {
       element.send(tmpcustTrans);
       retCode = true;
    }
    return retCode;
}


// If report called directly throw error else initialise from the Args Passed.
 public void init()
{
    ;

    element.initFromArgs(element.args());

    if(!element.args().record())
    {
     throw error(strfmt("Report is called with incorrect Parameters"));
    }

    super();
}


void initFromArgs(Args args)
{

    if (args && args.dataset())
    {
        tmpcustTrans = args.record(); // Get the temporary table record
    }
}


Also, create a Output Menu Item for the "CustomerTransactions" report.

Right click on the class and run it.Select a Account number and click "OK" on the dialog.
Report will be generated for the selected Account number.

Happy Daxing :) 













 

No comments:

Post a Comment