0

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

antechf11
  • 55
  • 4

1 Answers1

0

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
    }

});
W.S.
  • 1,401
  • 1
  • 7
  • 9
  • thank's for that learn about 2 method this case. But if use `fieldChanged` can explain me about permissions on the employee record – antechf11 Dec 23 '22 at 02:32
  • If the role of the user who's executing has no permissions on the employee record, you could expose a Suitelet API (with a deployment with `execute as admin` as checked) and just do an http request to that endpoint to get the data. – W.S. Dec 23 '22 at 07:50