Tuesday, October 1, 2013

Chart Of Accounts import from CSV file through X++ programming

public class ChartOfAccountsMasterImport extends RunBase
{
    CommaIo                     csvFile;
    Filename                    filename;
    DialogField                 dialogFilename,dialogChartOfAccountsName;
    str                         ChartOfAccountsName;
    #define.CurrentVersion(2)
    #localmacro.CurrentList
    filename,
    insertIncorrectRecords,
    #endmacro
    #localmacro.ListVersion1
    filename,
    insertIncorrectRecords
    #endmacro
    #File
}

public Object dialog()
{
    DialogRunbase       dialog = super();
    ;
    dialogFilename  = dialog.addField(extendedTypeStr(FilenameOpen));
    dialogFilename.value(filename);
    dialogChartOfAccountsName = dialog.addField(extendedTypeStr("Name"), "Chart of account name");
    dialog.filenameLookupFilter(["All files", #AllFiles]);
    return dialog;
}

public boolean getFromDialog()
{
    filename                = dialogFilename.value();
    ChartOfAccountsName     = diALOGChartOfAccountsName.value();
    return true;
}
void run()
{
    ChartOfAccountsService                      chartOfAccountsService;
    MainAccountContract                         mainAccountContract;
    CommaTextIo                                 file;
    container                                   rec;
    Name                                        ledgerChartOfAccountsName;
    MainAccountNum                              mainAccountId;
    DimensionLedgerAccountType                  dimensionledgerAccountType;
    MainAccountCategory                         MainAccountCategory;
    MainAccount                                 MainAccount;
    DimensionAttribute                          mainAccountDimAttribute;
    DimensionAttributeValue                     dimensionAttributeValue;
    DimensionAttributeValueTotallingCriteria    totalCriteria;
    AccountCategory                             _accountCategory;
    str                                         strOfAccounts, fromA, toA,MainAccountName;
    int                                         sep;
    container                                   readCon;
    counter                                     icount,inserted;
    ;

    csvFile = new CommaIO(filename, 'r');
    try
    {
        if (csvFile)
        {
            ttsbegin;
            readCon = csvFile.read();
            while (csvFile.status() == IO_Status::OK)
            {
                readCon = csvFile.read();
                if(readCon)
                {
                    mainAccountId = conPeek(readCon, 1);//strlrTrim(
                    mainAccountName = conPeek(readCon, 2);
                    dimensionledgerAccountType = conPeek(readCon, 3);
                    _accountCategory = conPeek(readCon,4);
                    if(!mainAccountId)
                    continue;
                    if (!mainAccount::findByMainAccountId( mainAccountId , false, LedgerChartOfAccounts::findByName(ledgerChartOfAccountsName).RecId))
                    {
                        mainAccountContract = new MainAccountContract();
                        mainAccountContract.parmMainAccountId( mainAccountId );
                        mainAccountContract.parmName(mainAccountName);
                        mainAccountContract.parmLedgerChartOfAccounts(ChartOfAccountsName);
                        mainAccountContract.parmType(enum2int(dimensionledgerAccountType::Expense));
                        select firstonly mainAccountCategory where mainAccountCategory.AccountCategory == _accountCategory;
                        MainAccountContract.parmAccountCategoryRef(mainAccountCategory.AccountCategoryRef);
                        chartOfAccountsService = new ChartOfAccountsService();
                        chartOfAccountsService.createMainAccount(mainAccountContract);
                    }
                }
            }
            ttsCommit;
        }
        icount--;//Remove header recount from total record count
    }
    catch(Exception::Error)
    {
        info(strFmt("%1 %2",Exception::Error,icount));
    }
}

static void main(Args  args)
{
    ChartOfAccountsMasterImport        ChartOfAccountsMasterImport;
    ;
    ChartOfAccountsMasterImport =  new ChartOfAccountsMasterImport();
    if(ChartOfAccountsMasterImport.prompt())
    {
        ChartOfAccountsMasterImport.run();
    }
}

No comments:

Post a Comment