Confirm bundle installation, enable features, create account

This sample is a bundle installation script that makes sure the bundle being installed is version 2.0, and 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.0 includes the following:

SuiteScript 1.0 Script

SuiteScript 2.0 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.x
 * @NScriptType BundleInstallationScript
 */

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

        if (isInEffect){
            log.debug({
                title: Feature,
                details: featureId + 'enabled'
            });
        } else {
            var featureError = error.create({
                name: 'INSTALLATION_ERROR',
                message: 'Feature' + featureId + 'must be enabled. Please enable the feature and try again.'
            });
            throw featureError;
        }
    }

    return {
        beforeInstall: function beforeInstall(params) {
            checkFeatureEnabled('WORKORDERS');

            if (params.version.toString() === "2.0") {
                checkFeatureEnabled('MULTICURRENCY');
            }
        },

        beforeUpdate: function beforeUpdate(params) {
            checkFeatureEnabled('WORKORDERS');

            if (params.toVersion.toString() === "2.0") {
                checkFeatureEnabled('MULTICURRENCY');
            }
        },

        afterUpdate: function afterUpdate(params) {
            if (params.fromVersion.toString() !== toversion.toString()) {
                var randomnumber = Math.floor(Math.random()*10000);
                var objRecord = record.create({
                    type: 'account'
                });
                objRecord.setValue({
                    fieldId: 'accttype',
                    value: 'Bank'
                });
                objRecord.setValue({
                    fieldId: 'acctnumber',
                    value: randomnumber
                });
                objRecord.setValue({
                    fieldId: 'acctname',
                    value: 'Acct '+toversion
                });
                objRecord.save({
                    enableSourcing: true
                });
            }
        }
    };
}); 

                  

General Notices