0% found this document useful (0 votes)
65 views

Ax2012 Enus Deviv 06 01 Lab Code

This code sample contains the code for a class called ImportBeginningBalances that is used to import beginning balance data from an Excel file into Dynamics 365 Business Central. The class opens the Excel file, reads data from the specified worksheet and rows, and inserts the data as journal lines. It contains methods for initializing the Excel application, reading data cell by cell, validating input, and running the import process.

Uploaded by

aimi99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Ax2012 Enus Deviv 06 01 Lab Code

This code sample contains the code for a class called ImportBeginningBalances that is used to import beginning balance data from an Excel file into Dynamics 365 Business Central. The class opens the Excel file, reads data from the specified worksheet and rows, and inserts the data as journal lines. It contains methods for initializing the Excel application, reading data cell by cell, validating input, and running the import process.

Uploaded by

aimi99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

This file contains code samples to be used for Lab 6.

1: Import Beginning Balance


s.
________________________________________________________________________________
_________
class ImportBeginningBalances extends RunBaseBatch
{
// Packed variables
FilenameOpen
fileNameOpen;
TutorialJournalNameId
inventJournalNameId;
// Excel objects
SysExcelApplication
SysExcelWorkbooks
SysExcelWorksheets
SysExcelWorksheet
SysExcelCells
// Dialog fields
DialogField
DialogField

oSysExcelApplication;
oSysExcelWorkbooks;
oSysExcelWorksheets;
oSysExcelWorksheet;
oSysExcelCells;
dlgFileNameOpen;
dlgInventJournalNameId;

#define.CurrentVersion(1)
#define.Version1(1)
#localmacro.CurrentList
fileNameOpen,
inventJournalNameId
#endmacro
}
________________________________________________________________________________
_________
server static ImportBeginningBalances construct()
{
return new ImportBeginningBalances();
}
________________________________________________________________________________
_________
static ClassDescription description()
{
return "Import Beginning Balances";
}
________________________________________________________________________________
_________
static void main(Args args)
{
ImportBeginningBalances ImportBeginningBalances;
;
ImportBeginningBalances = ImportBeginningBalances::construct();
if (ImportBeginningBalances.prompt())
ImportBeginningBalances.run();
}

________________________________________________________________________________
_________
public Object dialog()
{
DialogRunbase dialog = super();
#resAppl
dlgFilenameOpen = dialog.addFieldValue(
extendedTypeStr(FilenameOpen),
FilenameOpen);
dlgInventJournalNameId =
dialog.addFieldValue(
extendedTypeStr(TutorialJournalNameId),
InventJournalNameId);
return dialog;
}
________________________________________________________________________________
_________
public boolean getFromDialog()
{
fileNameOpen = dlgFileNameOpen.value();
inventJournalNameId = dlgInventJournalNameId.value();
return super();
}
________________________________________________________________________________
_________
public boolean validate(Object _calledFrom = null)
{
boolean ret = true;
//Add validation to the class to verify that the two fields on the dialog b
ox are populated with a value.
if (!fileNameOpen)
{
ret = checkFailed("Invalid file name.");
}
if (!inventJournalNameId)
{
ret = checkFailed("Invalid invent journal name Id.");
}
return ret;
}
________________________________________________________________________________
_________
public boolean init()
{
;
// If spreadsheet document doesn't exist, then quit
if (! winAPI::fileExists(fileNameOpen))
{
throw error(strfmt("File '%1' doesn't exist or is currently open.",
fileNameOpen));
}

// Start excel & turn off alerts


oSysExcelApplication = SysExcelApplication::construct();
oSysExcelApplication.displayAlerts(false);
oSysExcelWorkbooks = oSysExcelApplication.workbooks();
// Try to open the file
try
{
oSysExcelWorkbooks.open(fileNameOpen);
}
catch
{
throw error(strfmt("@SYS76826", fileNameOpen));
}
oSysExcelWorksheets = oSysExcelApplication.worksheets();
return true;
}
________________________________________________________________________________
_________
void startProgress()
{
#avifiles
SysOperationProgress oProgress;
oProgress = new SysOperationProgress();
oProgress.setAnimation(#aviupdate);
oProgress.setCaption(ImportBeginningBalances::description());
oProgress.update(true);
}
________________________________________________________________________________
_________
private void readAll( )
{
// Macro for file type (extension)
#define.SP_FILE_TYPE ("xls")
// Macros for input and output sheet
#define.SP_OUTPUT_WORKSHEET ("Data")
// Macros for read
#define.SP_START_ROW (2)
#define.SP_END_ROW (10)
#define.SP_READ_TRANSDATE_COLUMN (1)
#define.SP_READ_ITEMID_COLUMN (2)
#define.SP_READ_COST_COLUMN (3)
int startRow = #SP_START_ROW;
InventDim tInventDim;
TutorialJournalTable journalTable;
TutorialJournalTrans journalTrans;
TutorialJournalTableData journalTableData = JournalTableData::newTable(jour

nalTable);
TutorialJournalTransData journalTransData = journalTableData.journalStatic(
).newJournalTransData(journalTrans,journalTableData);
// Try to open Outputs worksheet
oSysExcelWorksheet = oSysExcelWorksheets.itemFromName(#SP_OUTPUT_WORKSHEET)
;
if (oSysExcelWorksheet == null)
{
throw error(strfmt("No 'Outputs' worksheet in file '%1'.", fileNameOpe
n));
}
// Try reading values from outputs worksheet
try
{
// Init journalTable
journalTable.JournalId = journalTableData.nextJournalId();
journalTable.JournalType = InventJournalType::Movement;
journalTable.JournalNameId = inventJournalNameId;
journalTableData.initFromJournalName(journalTableData.journalStatic().
findJournalName(journalTable.JournalNameId));
oSysExcelCells = oSysExcelWorksheet.cells();
while (startRow <= #SP_END_ROW)
{
journalTrans.clear();
journalTransData.initFromJournalTable();
journalTrans.TransDate = oSysExcelCells.item(StartRow, #SP_READ_T
RANSDATE_COLUMN).value().date();
journalTrans.ExItemId = this.cell2String(StartRow, #SP_READ_ITEMI
D_COLUMN);
journalTrans.ExCostAmount = oSysExcelCells.item(StartRow, #SP_REA
D_COST_COLUMN).value().double();
journalTransData.create();
StartRow ++;
}
journalTable.insert();
}
catch
{
throw error("Reading values from Excel file failed.");
}
}
________________________________________________________________________________
_________
private str cell2String(int _row, int _column)
{
str ret;
if (! oSysExcelCells)

{
ret = '';
}
else
{
switch (oSysExcelCells.item(_row, _column).value().variantType())
{
case COMVariantType::VT_BSTR:
// String type
ret = strltrim(strrtrim(oSysExcelCells.item(_row, _column).value
().bstr()));
break;
case COMVariantType::VT_R8:
// Real type
ret = strltrim(strrtrim(num2str(oSysExcelCells.item(_row, _colum
n).value().double(),0,0,0,0)));
break;
default :
ret = '';
break;
}
}
return ret;
}
________________________________________________________________________________
_________
private void exitExcel()
{
if (oSysExcelApplication)
{
// Although displayAlerts is called in init it appears necessary to
// call it again. Otherwise if the application is not saved a dialog
// will pop up asking if the file should be saved.
oSysExcelApplication.displayAlerts(false);
oSysExcelApplication.quit();
}
}
________________________________________________________________________________
_________
/// <summary>
/// Contains the code that does the actual job of the class.
/// </summary>
public void run()
{
#OCCRetryCount
if (! this.validate())
throw error("");
try
{
ttsbegin;
this.startProgress();
this.init();
this.readAll();
this.exitExcel();

ttscommit;
}
catch (Exception::Deadlock)
{
retry;
}
catch (Exception::UpdateConflict)
{
if (appl.ttsLevel() == 0)
{
if (xSession::currentRetryCount() >= #RetryNum)
{
this.exitExcel();
throw Exception::UpdateConflictNotRecovered;
}
else
{
retry;
}
}
else
{
this.exitExcel();
throw Exception::UpdateConflict;
}
}
catch (Exception::Error)
{
error("Import failed!");
this.exitExcel();
}
}
________________________________________________________________________________
_________

You might also like