I am trying to change the location on an item fulf...
# suitescript
s
I am trying to change the location on an item fulfillment in a before load user event and am running into issues. After the script runs, if you open the console and retrieve the value, the internal id of the location is set to the correct value that was set by the script, but the description in the UI is not updated. Additionally, and more problematic, the inventory detail is not resetting based on the new location, so NetSuite throws an error saying inventory detail must be configured, when in reality the new location that was set in the script does not need the inventory detail because it doesnt use bins. The fulfillments are being created by Ship Junction integration, which apparently cannot configure the inventory detail, so I am trying to change the location in a before load so the fulfillment will save and then in a before submit, change the location back and configure the inventory detail. Has anyone ever run into this issue of changing the location in a before load and know if its possible to make it work?
l
@steven.smith I asked ChatGPT to answer your question, He provided below details, let me know of this help- It appears that the issue you're facing is due to the UI not updating to reflect the changes made by your script. The User Event Script operates on the server side, and it updates the record's field values. However, it does not automatically update the UI or the client-side form. To resolve this issue, you can use a combination of a Before Load User Event Script to update the field values and a Client Script to update the UI and handle the inventory detail configuration. 1. In the Before Load User Event Script, update the location:
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define(['N/record'], function(record) {
    function beforeLoad(context) {
        if (context.type === context.UserEventType.VIEW || context.type === context.UserEventType.EDIT) {
            var itemFulfillment = context.newRecord;
            
            // Update the location field value
            var newLocationId = '12345'; // Replace with the desired location internal ID
            itemFulfillment.setValue({
                fieldId: 'location',
                value: newLocationId
            });
        }
    }

    return {
        beforeLoad: beforeLoad
    };
});
2. Create a Client Script to update the UI and handle inventory detail configuration:
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define([], function() {
    function pageInit(context) {
        var itemFulfillment = context.currentRecord;

        // Update the location in the UI
        var newLocationId = '12345'; // Replace with the desired location internal ID
        itemFulfillment.setValue({
            fieldId: 'location',
            value: newLocationId
        });

        // Handle inventory detail
        var itemSublistId = 'item';
        var itemCount = itemFulfillment.getLineCount({ sublistId: itemSublistId });

        for (var i = 0; i < itemCount; i++) {
            // Create inventory detail subrecord
            itemFulfillment.selectLine({ sublistId: itemSublistId, line: i });
            var inventoryDetailSubrecord = itemFulfillment.getCurrentSublistSubrecord({
                sublistId: itemSublistId,
                fieldId: 'inventorydetail'
            });

            if (!inventoryDetailSubrecord) {
                inventoryDetailSubrecord = itemFulfillment.createCurrentSublistSubrecord({
                    sublistId: itemSublistId,
                    fieldId: 'inventorydetail'
                });
            }

            // Set inventory detail subrecord fields
            var quantity = itemFulfillment.getCurrentSublistValue({ sublistId: itemSublistId, fieldId: 'quantity' });
            inventoryDetailSubrecord.setValue({ fieldId: 'quantity', value: quantity });

            // Add or update inventory assignment
            var inventoryAssignmentCount = inventoryDetailSubrecord.getLineCount({ sublistId: 'inventoryassignment' });
            if (inventoryAssignmentCount === 0) {
                inventoryDetailSubrecord.selectNewLine({ sublistId: 'inventoryassignment' });
            } else {
                inventoryDetailSubrecord.selectLine({ sublistId: 'inventoryassignment', line: 0 });
            }

            // Set inventory assignment fields
            inventoryDetailSubrecord.setCurrentSublistValue({ sublistId: 'inventoryassignment', fieldId: 'quantity', value: quantity });
            inventoryDetailSubrecord.setCurrentSublistValue({ sublistId: 'inventoryassignment', fieldId: 'location', value: newLocationId });

            // Commit the inventory assignment line
            inventoryDetailSubrecord.commitLine({ sublistId: 'inventoryassignment' });

            // Save the inventory detail subrecord
            itemFulfillment.commitLine({ sublistId: itemSublistId });
        }
    }

    return {
        pageInit: pageInit
    };
});
This code snippet assumes that the new location does not use bins. It creates or updates the inventory detail subrecord for each item in the item fulfillment and sets the inventory assignment with the new location and the item's quantity. Please note that this example assumes a simple scenario and might need to be adjusted to fit your specific business logic and requirements.
r
beforeLoad sounds like the wrong entry point.