Hello Everyone! I am trying to deploy a SuiteScri...
# general
s
Hello Everyone! I am trying to deploy a SuiteScript to automatically set the Out-of-stock behavior based on the quantity available The logic goes something like this If (quantityAvailable = 0) => Set outofstockbehavior to "Disallow back orders but display out-of-stock message" else => Set outofstockbehavior to "Allow back orders with no out-of-stock message" The error I'm getting is this.
s
Your function is trying to access a record but for some reason it is null. Can’t be more specific without code.
s
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType ScheduledScript
 * @NModuleScope SameAccount
 */

define(['N/record', 'N/search'], function(record, search) {
    function execute(context) {
        // Load the item record
        var itemRecord = record.load({
            type: record.Type.INVENTORY_ITEM,
            id: 10336,
            isDynamic: false,
        });

        // Get the out-of-stock behavior
        var outOfStockBehavior = itemRecord.getValue({
            fieldId: 'outofstockbehavior'
        });

        var quantityAvailable = itemRecord.getValue({
                    fieldId: 'quantityavailable'
                });

        // Set the out-of-stock behavior based on the quantity available
            if (quantityAvailable > 0) {
                itemRecord.setValue({
                    fieldId: 'outofstockbehavior',
                    value: record.FieldValue.ENABLE
                });
            } else {
                itemRecord.setValue({
                    fieldId: 'outofstockbehavior',
                    value: record.FieldValue.DISABLE
                });
            }

            // Save the item record
            itemRecord.save();



        // Log the out-of-stock behavior
        log.debug({
            title: 'Item Out-of-Stock Behavior',
            details: 'Item ID: 10336, Out-of-Stock Behavior: ' + outOfStockBehavior
        });
    }

    return {
        execute: execute
    };
});
s
The record isn’t loading. Try putting the id in single quotes.