When working with NetSuite in a multi-subsidiary environment, it’s often necessary to determine which subsidiaries a user has access to based on their role. This information is critical for filtering transactions, applying business logic, or enforcing access controls.
In this article, I’ll walk you through a SuiteScript 2.x function that retrieves the subsidiaries accessible to a user’s current role.
const getSubsidiaryForUser = (currentRole) => {
try {
var roleSearchObj = search.create({
type: "role",
filters: [
["internalid", "anyof", currentRole]
],
columns: [
search.createColumn({ name: "name", label: "Name" }),
search.createColumn({ name: "subsidiaryoption", label: "Accessible Subsidiaries Option" }),
search.createColumn({ name: "subsidiary", join: "user", label: "Subsidiary" }),
search.createColumn({ name: "subsidiaries", label: "Accessible Subsidiaries" })
]
});
var subsidiaryFilters = {};
roleSearchObj.run().each(function (result) {
var subsidairyOption = result.getValue(roleSearchObj.columns[1]);
if (subsidairyOption == "OWN") {
// User has access only to their own subsidiary
var userSubsidiary = result.getValue(roleSearchObj.columns[2]);
subsidiaryFilters[userSubsidiary] = result.getText(roleSearchObj.columns[2]);
} else {
// User has access to multiple subsidiaries
var subsidiaries = result.getValue(roleSearchObj.columns[3]);
subsidiaryFilters[subsidaries] = result.getText(roleSearchObj.columns[3]);
return true;
}
});
return subsidiaryFilters;
} catch (e) {
log.error('Error in getSubsidiaryForUser', e);
return {};
}
};
How It Works
- Search the Role Record
- We use a
search.createon therolerecord type and filter by theinternalidof the current role. - Fetch Subsidiary Access Information
subsidiaryoption: tells us if the role is limited to the user’s own subsidiary or has access to multiple subsidiaries.subsidiary(joined with user): gives the specific subsidiary if the role is restricted to the user’s own.subsidiaries: provides the list of subsidiaries if the role allows access to multiple.