AX2012 Import Chart of Accounts from CSV

/ Assumes a file structure with:
// Account number;Account name;Account type (based on the enum DimensionLedgerAccountType)
static void Krishh_ImportChartOfAccounts(Args _args)
{
    ChartOfAccountsService      chartOfAccountsService;
    MainAccountContract         mainAccountContract;
    CommaTextIo                 file;
    container                   rec;
    Name                        ledgerChartOfAccountsName;
    MainAccountNum              mainAccountId;
    DimensionLedgerAccountType  dimensionledgerAccountType;

    MainAccount                                 MainAccount;
    DimensionAttribute                          mainAccountDimAttribute;
    DimensionAttributeValue                     dimensionAttributeValue;
    DimensionAttributeValueTotallingCriteria    totalCriteria;

    str                 strOfAccounts, fromA, toA;
    int                 sep;
    Dialog              d;
    DialogField         df1, df2;
    ;
    d = new Dialog("Import chart of accounts (main accounts)");
    df1 = d.addField(ExtendedTypeStr("FilenameOpen"));
    df2 = d.addField(extendedTypeStr("Name"), "Chart of account name");

    if (d.run())
    {
        file = new CommaTextIo(df1.value(), 'r');
        file.inFieldDelimiter(';');
        ledgerChartOfAccountsName = df2.value();
        ttsBegin;
        while (file.status() == IO_Status::Ok)
        {
            rec = file.read();
             mainAccountId = strlrTrim(conPeek(rec, 1));
            dimensionledgerAccountType = conPeek(rec, 3);

            if (!mainAccount::findByMainAccountId( mainAccountId , false, LedgerChartOfAccounts::findByName(ledgerChartOfAccountsName).RecId))
            {
                mainAccountContract = new MainAccountContract();
                mainAccountContract.parmMainAccountId( mainAccountId );
                mainAccountContract.parmName(conPeek(rec, 2));
               mainAccountContract.parmLedgerChartOfAccounts(ledgerChartOfAccountsName);
                mainAccountContract.parmType(dimensionledgerAccountType);

                chartOfAccountsService = new ChartOfAccountsService();
                chartOfAccountsService.createMainAccount(mainAccountContract);

                if (dimensionledgerAccountType == DimensionledgerAccountType::Total)
                {
                    strOfAccounts = conPeek(rec, 4);

                    sep = strScan(strOfAccounts, '..', 1, strLen(strOfAccounts));

                    if (sep)
                    {
                        fromA = subStr(strOfAccounts, 1, sep - 1);
                        toA   = subStr(strOfAccounts, sep + 2, strLen(strOfAccounts));
                    }

                    select recid from MainAccount where mainAccount.MainAccountId ==  mainAccountId  ;

                    mainAccountDimAttribute.RecId = DimensionAttribute::getMainAccountDimensionAttribute();

                    dimensionAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndEntityInst(mainAccountDimAttribute.RecIdmainAccount.RecId, true, true);

                    totalCriteria.DimensionAttributeValue =dimensionAttributeValue.RecId;
                    totalCriteria.FromValue = fromA;
                    totalCriteria.ToValue   = toA;
                    totalCriteria.insert();
                }
            }
        }

        ttsCommit;
    }

}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.