Skip to content

Business Central.Export.All

Runs all other Business Central processes required to maintain a model including:

  • Business Central.Export.Company
  • Business Central.Export.Non Time Data
  • Business Central.Export.generalLedgerEntries

Process Code

vb

script.library(script.variableGet("BusinessCentralIntegrationLibraryPath"));


function pre() {
    // This function is called once before the process is executed.
    // Use this to setup prompts.
    script.log('process pre-execution parameters parsed.');
}

function begin() {
    // This function is called once at the start of the process
    script.log('process execution started.');

    process.execute("Business Central.Export.Company");
    process.execute("Business Central.Export.Non Time Data", { "endpoint": "accounts" });
    process.execute("Business Central.Export.Non Time Data", { "endpoint": "dimensions" });
    process.execute("Business Central.Export.Non Time Data", { "endpoint": "dimensionValues" });

    const tableName = `${schema}_generalledgerentries`;

    let months;
    if (tableExists(ds, schema, tableName)) {
        // Table already exists - just refresh the last 3 months.
        months = getLastNMonths(3);
        console.log(`Table "${tableName}" exists - refreshing last ${months.length} months.`);
    } else {
        // Table doesn't exist yet - do a full historical load based on
        // every month present in the model's Time hierarchy.
        months = getModelMonths();
        console.log(`Table "${tableName}" does not exist - loading ${months.length} months found in model.`);
    }

    for (let i = 0; i < months.length; i++) {
        if (script.IsCancelled()) {
            return;
        }
        process.execute("Business Central.Export.generalLedgerEntries", {
            "loadYear": months[i].loadYear,
            "loadMonth": months[i].loadMonth
        });
    }
}

function data(record) {
    // This function is called once for each line of data on the second cycle
    // Use this to build dimensions and push data into cubes
}

function end() {
    // This function is called once at the end of the process
    script.log('process execution finished.');
}

// Returns the current month plus the prior (n - 1) months, as
// { loadYear: "YYYY", loadMonth: "MM" } objects, oldest first.
function getLastNMonths(n) {
    let months = [];
    let now = new Date();

    for (let i = n - 1; i >= 0; i--) {
        let d = new Date(now.getFullYear(), now.getMonth() - i, 1);
        months.push({
            loadYear: String(d.getFullYear()),
            loadMonth: String(d.getMonth() + 1).padStart(2, "0")
        });
    }

    return months;
}

const MONTH_ABBR_TO_NUMBER = {
    "Jan": "01", "Feb": "02", "Mar": "03", "Apr": "04",
    "May": "05", "Jun": "06", "Jul": "07", "Aug": "08",
    "Sep": "09", "Oct": "10", "Nov": "11", "Dec": "12"
};

// Returns every month present under the model's Time hierarchy, as
// { loadYear: "YYYY", loadMonth: "MM" } objects, parsed from the
// "YYYY - MMM" strings returned by hierarchy.descendants.
function getModelMonths() {
    let raw = JSON.parse(hierarchy.descendants("Time", "Default", "All Time"));
    let months = [];

    let now = new Date();
    let currentYearMonth = now.getFullYear() * 100 + (now.getMonth() + 1); // e.g. 202607

    for (let i = 0; i < raw.length; i++) {
        let parts = raw[i].split(" - ");
        if (parts.length !== 2) {
            script.log(`Warning: could not parse time member "${raw[i]}" - skipping.`);
            continue;
        }

        let year = parts[0].trim();
        let monthAbbr = parts[1].trim();
        let monthNumber = MONTH_ABBR_TO_NUMBER[monthAbbr];

        if (!monthNumber) {
            script.log(`Warning: unrecognised month abbreviation "${monthAbbr}" in "${raw[i]}" - skipping.`);
            continue;
        }

        let yearMonth = parseInt(year, 10) * 100 + parseInt(monthNumber, 10);
        if (yearMonth > currentYearMonth) {
            continue; // skip future months beyond the current one
        }

        months.push({ loadYear: year, loadMonth: monthNumber });
    }

    return months;
}