Get the Localization Context of an Employee Record
The following sample shows how to retrieve the localization context value for a record. The sample uses two different approaches depending on whether the record is loaded in the script. The subsidiary values are hard-coded. If you run this script in your account, be sure to replace the hard-coded values with valid values from your account.
Note:
This sample script uses the require function so you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (a script you attach to a script record and deploy). For more information, see SuiteScript 2.1 Script Basics and SuiteScript 2.1 Script Types.
/**
*@NApiVersion 2.1
*/
// This example calls the getContext() API with options recordType, recordId and record.
require(['N/record', 'N/recordContext'],
function(record, recordContext) {
// Create record
const employee = record.create({
type: record.Type.EMPLOYEE,
isDynamic: true
});
// Set up CA subsidiary
employee.setValue({
fieldId: 'subsidiary',
value: 2
});
employee.setValue({
fieldId: 'entityid',
value: `test_emp_${Date.now()}`
});
const employeeId = employee.save();
// getContext() with options recordType, recordId and contextTypes
const employeeContext = recordContext.getContext({
recordType: record.Type.EMPLOYEE,
recordId: employeeId,
contextTypes: [recordContext.ContextType.LOCALIZATION]
});
log.debug(employeeContext); // Expected log: {"localization":["CA"]}
// Change employee subsidiary to AU
employee.setValue({
fieldId: 'subsidiary',
value: 3
});
// getContext() with options record and contextTypes
const updatedEmployeeContext = recordContext.getContext({
record: employee,
contextTypes: [recordContext.ContextType.LOCALIZATION]
});
log.debug(updatedEmployeeContext); // Expected log: {"localization":["AU"]}
// Delete record
record.delete({
type : record.Type.EMPLOYEE,
id: employeeId
});
});