Suitescript fieldChanges location field

358 Views Asked by At

I'm still learning in netsuite. and I have a case like the following

on the Requisition form, if input requestor in requisiton, how to change location field automatically. we can get the location in the employee field

Requisition

employee field

can show an simple code for this case, thank's for helping

1

There are 1 best solutions below

2
On

I would advise to go for a UserEvent script and use the beforeLoad hook instead to set the default values.

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
*/
define(["N/runtime"], function (runtime) {

    function beforeLoad(context) {
        var type = context.type
        var UserEventType = context.UserEventType;
        var newRecord = context.newRecord
        
        if (type != UserEventType.CREATE) return;
        
        var user = runtime.getCurrentUser()
        newRecord.setValue({ fieldId: 'subsidiary', value: user.subsidiary });
        newRecord.setValue({ fieldId: 'location', value: user.location });
        
    };

    return {
        beforeLoad: beforeLoad
    }

});

If you do want to use the fieldChanged in the client script and search for the location of the employee, your client script will need permissions on the employee record.

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
*/
define(["N/search"], function (search) {

    function fieldChanged(context) {
        var currentRecord = context.currentRecord
        var fieldId = context.fieldId

        if (fieldId != 'entity') return
        
        const id = currentRecord.getValue({ fieldId: fieldId })
        if (!id) return
        
        search.lookupFields.promise({
            type : search.Type.EMPLOYEE,
            id : id,
            columns : [ 'subsidiary', 'location' ]
        }).then(function(user) {
            currentRecord.setValue({ fieldId: 'subsidiary', value: user.subsidiary });
            currentRecord.setValue({ fieldId: 'location', value: user.location });
        });
    }

    return {
        fieldChanged: fieldChanged
    }

});