Wednesday 31 October 2018

Populate a temporary table on a form

  • Create a class to populate the temporarytable
 
public temporaryTable populateTemporaryTable()
{
   
 temporaryTable temporaryTable;
   
 int I;
   
 for(i = 0;i <= 5; i++)
 {
  //
  // Set the data in the table
  //
  temporaryTable.Field1 = "InterestungString";
  temporaryTable.Field2 = i;
  temporaryTable.insert();
 }
 return temporaryTable;
}
 
  • In the form add the temporary table as a datasource
  • In the form class declaration add a buffer based on the temporary table
 
public class FormRun extends ObjectRun
{
 //
 // Global temporaryTable
 //
 temporaryTable temporaryTable;
}
  • Populate the table in the form (usually in the init method)
 
public void init()
{
 BuildTempTable BuildTempTable = new BuildTempTable();
   
 super();
 //
 // Fill the buffer with data
 //
 temporaryTable = BuildTempTable.populateTemporaryTable();
 //
 // link the form datasource with the buffer
 //
 temporaryTable.linkPhysicalTableInstance(temporaryTable);
}

Wednesday 10 October 2018

List Security Roles in AX2012 to Excel



static void SecurityCreateExcelDocument(Args _args)
{
SysExcelApplication      xlsApplication;
SysExcelWorkBooks    xlsWorkBookCollection;
SysExcelWorkBook     xlsWorkBook;
SysExcelWorkSheets   xlsWorkSheetCollection;
SysExcelWorkSheet    xlsWorkSheet;
SysExcelRange            xlsRange;
int                                row = 1;
str                               fileName;
SecurityUserRole            _SecurityUserRole;
SecurityRoleTaskGrant       _SecurityRoleTaskGrant;
SecurityTask                _SecurityTask;
SecurityRole                _SecurityRole;
UserInfo                    _UserInfo;

fileName = "D:\\Security.xlsx";
xlsApplication           = SysExcelApplication::construct();
xlsWorkBookCollection    = xlsApplication.workbooks();
xlsWorkBook              = xlsWorkBookCollection.add();
xlsWorkSheetCollection   = xlsWorkBook.worksheets();
xlsWorkSheet             = xlsWorkSheetCollection.itemFromNum(1);
xlsWorkSheet.cells().item(row,1).value("User Role");
xlsWorkSheet.cells().item(row,2).value("User Id");
xlsWorkSheet.cells().item(row,3).value("Legal Entity");
row++;
    while select _UserInfo
    {
        while select _SecurityUserRole where _SecurityUserRole.User==_UserInfo.id
        {
                while select _SecurityRole where _SecurityRole.RecId==_SecurityUserRole.SecurityRole
                {
                xlsWorkSheet.cells().item(row,1).value(_SecurityRole.Name);
                xlsWorkSheet.cells().item(row,2).value(_UserInfo.id);
                xlsWorkSheet.cells().item(row,3).value(_UserInfo.company);
                row++;
                }
        }
    }
   
if(WinApi::fileExists(fileName))

{
    WinApi::deleteFile(fileName);

}

xlsWorkbook.saveAs(fileName);
xlsApplication.visible(true);
}