Confirm Bundle installation, Enable Features, and Create Account

This sample is a bundle installation script that makes sure the bundle being installed is version 2.1, confirms that the Work Orders and Multiple Currencies features are enabled, and then creates an account record.

The conversion of this script from SuiteScript 1.0 to SuiteScript 2.1 includes the following:

SuiteScript 1.0 Script

SuiteScript 2.1 Script

                    function beforeInstall(toversion){
    checkFeatureEnabled('WORKORDERS');
    if (toversion.toString() == "2.0" ){
        checkFeatureEnabled('MULTICURRENCY');
    }
}

function afterInstall(toversion){
    var randomnumber=Math.floor(Math.random()*10000);
    var objRecord = nlapiCreateRecord('account');
    objRecord.setFieldValue('accttype','Bank');
    objRecord.setFieldValue('acctnumber',randomnumber);
    objRecord.setFieldValue('acctname','Acct '+toversion);
    nlapiSubmitRecord(objRecord, true);
}

function beforeUpdate(fromversion, toversion){
    checkFeatureEnabled('WORKORDERS');
    if (toversion.toString() == "2.0" ){
        checkFeatureEnabled('MULTICURRENCY');
    }
}

function afterUpdate(fromversion, toversion){
    if (fromversion.toString() != toversion.toString()){
        var randomnumber=Math.floor(Math.random()*10000);
        var objRecord = nlapiCreateRecord('account');
        objRecord.setFieldValue('accttype','Bank');
        objRecord.setFieldValue('acctnumber',randomnumber);
        objRecord.setFieldValue('acctname','Acct '+toversion);
        nlapiSubmitRecord(objRecord, true);
    }
}

function checkFeatureEnabled(featureId){
    var objContext = nlapiGetContext();
    var feature = objContext.getFeature(featureId);

    if (feature){
        nlapiLogExecution('DEBUG','Feature',featureId+' enabled');
    } else {
        throw new nlobjError('INSTALLATION_ERROR','Feature '+ featureId+' must be enabled. Please enable the feature and try again.');
    }
 } 

                  
                    /**
 * @NApiVersion 2.1
 * @NScriptType BundleInstallationScript
 */

define(['N/runtime', 'N/error', 'N/record'], (runtime, error, record) => {
    const checkFeatureEnabled = (featureId) => {
        const isInEffect = runtime.isFeatureInEffect({
            feature: featureId
        });

        if (isInEffect) {
            log.debug({
                title: 'Feature Enabled',
                details: `${featureId} is enabled.`
            });
            return;
        }

        throw error.create({
            name: 'INSTALLATION_ERROR',
            message: `Feature ${featureId} must be enabled. Please enable the feature and try again.`
        });
    };

    const beforeInstall = (params) => {
        checkFeatureEnabled('WORKORDERS');

        if (String(params.version) === '2.1') {
            checkFeatureEnabled('MULTICURRENCY');
        }
    };

    const beforeUpdate = (params) => {
        checkFeatureEnabled('WORKORDERS');

        if (String(params.toVersion) === '2.1') {
            checkFeatureEnabled('MULTICURRENCY');
        }
    };

    const afterUpdate = (params) => {
        const fromVersion = String(params.fromVersion);
        const toVersion = String(params.toVersion);

        if (fromVersion !== toVersion) {
            const randomNumber = Math.floor(Math.random() * 10000);

            const accountRecord = record.create({
                type: record.Type.ACCOUNT
            });

            accountRecord.setValue({
                fieldId: 'accttype',
                value: 'Bank'
            });

            accountRecord.setValue({
                fieldId: 'acctnumber',
                value: randomNumber
            });

            accountRecord.setValue({
                fieldId: 'acctname',
                value: `Acct ${toVersion}`
            });

            accountRecord.save({
                enableSourcing: true
            });
        }
    };

    return {
        beforeInstall,
        beforeUpdate,
        afterUpdate
    };
}); 

                  

General Notices