Working with a developer that doesn't have much su...
# suitescript
n
Working with a developer that doesn't have much suitescript/netsuite experience. Trying to create a script to import new billing schedules (will eventually want to upload 285 of them). Getting the following error when trying to upload the script to NetSuite. "Fail to evaluate script: {"type":"error.SuiteScriptModuleLoaderError","name":"UNEXPECTED_ERROR","message":"syntax error (SS_SCRIPT_FOR_METADATA#10)","stack":[]}"
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType MapReduceScript
 */
define(['N/record', 'N/search', 'N/runtime'],
    /**
 * @param{record} record
 * @param{search} search
 */
    (record, search, runtime) => {
        const getInputData = (inputContext) => {
            return [
                {'freq':'Monthly','name':'Monthly - 1 Year - No Arrears 12/1 *DO NOT EDIT*','re':1,'nr':12},
                {'freq':'Monthly','name':'Monthly - 1 Year - No Arrears 11/1 *DO NOT EDIT*','re':1,'nr':11}
            ];
        }

        const reduce = (reduceContext) => {
            let inp = reduceContext.values[0];
            let freq = inp.freq;
            let n = inp.name;
            let nr = <http://inp.nr|inp.nr>;
            let re = <http://inp.re|inp.re>;

            var sched = record.create({
                type: record.Type.BILLING_SCHEDULE, 
                isDynamic: true,
                defaultValues: { 'schedtype': SCHEDULE_TYPE }                
            });

            sched.setValue({ fieldId: 'applytosubtotal', value: 'F' });
            sched.setValue({ fieldId: 'frequency', value: freq });
            sched.setValue({ fieldId: 'inarrears', value: 'F' });
            sched.setValue({ fieldId: 'initialamount', value: 0 });
            sched.setValue({ fieldId: 'inuse', value: 'T' });
            sched.setValue({ fieldId: 'isinactive', value: 'F' });
            sched.setValue({ fieldId: 'ispublic', value: 'T' });
            sched.setValue({ fieldId: 'name', value: n });
            sched.setValue({ fieldId: 'numberremaining', value: nr });
            sched.setValue({ fieldId: 'repeatevery', value: re });
            sched.setValue({ fieldId: 'scheduletype', value: 'STD' });

            sched.save({ ignoreMandatoryFields: false });
        }

        return {getInputData, reduce}
    }
);
e
Change the
@NApiVersion
to
2.1
n
We did just realize that. Now it is failing at get input data.
n
Failing how?
n
Just didn't have a .js after the script name in NetSuite. Got it to get past the grab input data.
It just didn't create any records. But the developer is giving it another look over.
n
I'd add some error processing to a summary section in case it's "silently" failing.
on the lines of: summary.reduceSummary.errors.iterator().each(function (key, error) { log.error('Reduce Error for key: ' + key, error); return true; });
e
Pretty sure billing schedule doesn't use default values, but if it does,
SCHEDULE_TYPE
isn't defined
e
Billing Schedules are not listed as supporting defaultValues
m
Also
reduceContext.values
is a string[], you need to JSON.parse it.
this 1
n
Thank you. We got it working. To follow up here is the code we used.
Copy code
/**
 * @NApiVersion 2.1
 * @NScriptType MapReduceScript
 */
define(['N/record', 'N/search', 'N/runtime'],
    /**
 * @param{record} record
 * @param{search} search
 */
    (record, search, runtime) => {
        const getInputData = (inputContext) => {
            log.debug('getInputData');
            const x = [
                {'freq':'Monthly','name':'Monthly - 1 Year - No Arrears 12/1 *DO NOT EDIT*','re':1,'nr':12},
                {'freq':'Monthly','name':'Monthly - 1 Year - No Arrears 11/1 *DO NOT EDIT*','re':1,'nr':11}
            ];

            return x;
        }

        const reduce = (reduceContext) => {
            log.debug('reduce', reduceContext);

            try{
                let inp = reduceContext.values[0];
                let freq = inp.freq;
                let n = inp.name;
                let nr = <http://inp.nr|inp.nr>;
                let re = <http://inp.re|inp.re>;

                log.debug(inp);
                log.debug(freq);
                log.debug(n);
                log.debug(nr);
                log.debug(re);

                var sched = record.create({
                    type: record.Type.BILLING_SCHEDULE, 
                    isDynamic: true,
                    defaultValues: { }                
                });

                sched.setValue({ fieldId: 'applytosubtotal', value: 'F' });
                sched.setValue({ fieldId: 'frequency', value: freq });
                sched.setValue({ fieldId: 'inarrears', value: 'F' });
                sched.setValue({ fieldId: 'initialamount', value: 0 });
                sched.setValue({ fieldId: 'inuse', value: 'T' });
                sched.setValue({ fieldId: 'isinactive', value: 'F' });
                sched.setValue({ fieldId: 'ispublic', value: 'T' });
                sched.setValue({ fieldId: 'name', value: n });
                sched.setValue({ fieldId: 'numberremaining', value: nr });
                sched.setValue({ fieldId: 'repeatevery', value: re });
                sched.setValue({ fieldId: 'scheduletype', value: 'STD' });

                sched.save({ ignoreMandatoryFields: false });
            }
            catch(e){
                log.error(e);
            }
        }

        return {getInputData, reduce}
    }
);