UI Builder: a A lookup to user roles returns nothing for one user
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2022 04:45 PM
In my UI Builder page I'm relying on user roles to tell me if certain form fields should be read-only. If the the user has any of roles [X,Y,Z], then the fields are editable, otherwise they are read only.
To do this I'm putting a boolean variable "readOnly" in the client state, and using it as the value for "Read only" on certain fields. The state variable is populated as follows:
I have a data source doing a lookup to sys_user_has_role, where User is (dynamic) Me.
In the Data Fetch Succeeded handler I pull out the roles from the data source and check whether any value matches one of certain roles. A match means the user should be allowed to edit certain field.
var userRoles = [];
var data = api.data.look_up_user_roles.results;
console.log(data);
for (var index = 0; index < data.length; index++) {
var role = data[index].role.displayValue;
if (userRoles.indexOf(role) == -1) userRoles.push(role); // unique list of roles for the looked-up user
}
console.log("User roles: " + JSON.stringify(userRoles));
// The user must have one of these roles to edit the fields
var editRoles = [
'A',
'B',
'C',
'D',
];
// If any element of the first array is found in the second, the user can edit the fields.
for (var index = 0; index < editRoles.length; index++) {
if (userRoles.indexOf(editRoles[index]) > -1) {
console.log("Found a match on " + editRoles[index]);
api.setState("readOnly", false);
return;
}
}
console.log("No match");
api.setState("readOnly", true);
I then use the state variable in certain fields.
When I impersonate my test users, this works for all users except one. There's nothing different about that user. For that user I get an empty array:
User roles: []
No match
In fact that user does have one of the roles I'm checking for, by virtue of belonging to a group that has that role. All the other test users have a similar setup. I get the same result if I assign the role directly to the user. So, what could be going on here?
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2022 12:32 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-19-2022 07:28 AM
I'm just going to handle this with variants and remove the data source. Relying on client-side scripting here is just bad design.