onLoad client script not working to make field editable only to a certain assignment group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2023 12:02 PM
Hi everyone,
I have a requirement, to make a specific field only editable to members of a specific group. I don't want to use business rules, or table UI policies or actions. I'm trying to do this via a catalog client script and GlideAjax. When it tested it yesterday it was working, now it's not working and behaving unexpectedly. Looking for some advice on how this can be done using a Client Script. My code is below -
function onLoad() {
var ga = new GlideAjax('u_SetCostCenterMicrosoftTeams');
var user = g_user.userID;
//parameters for GlideAjax = name of function and user
ga.addParam('sysparm_name', 'checkUserGroup');
ga.addParam('sysparm_user', user);
ga.getXML(setCostCenterEditable);
function setCostCenterEditable(response) {
//get the answer in XML format and parse it
var answer = response.responseXML.documentElement.getAttribute("answer");
//if answer is true set the cost center variable to read only, otherwise set read only to false
if (answer == true) {
g_form.setReadOnly('oracle_cost_center', false);
g_form.setReadOnly('requester_name', true);
g_form.setReadOnly('requester_email', true);
g_form.setReadOnly('requester_phone', true);
g_form.setReadOnly('office_location', true);
g_form.setReadOnly('do_you_have_an_address_different_than_your_office_location_to_enter', true);
g_form.setReadOnly('enter_your_other_address_here', true);
g_form.setReadOnly('if_you_feel_the_cost_center_above_is_inaccurate_please_enter_the_correct_cost_center_below', true);
g_form.setReadOnly('phone_type', true);
g_form.setReadOnly('additional_comments', true);
} else if (!answer) {
g_form.setReadOnly('oracle_cost_center', true);
g_form.setReadOnly('requester_name', true);
g_form.setReadOnly('requester_email', true);
g_form.setReadOnly('requester_phone', true);
g_form.setReadOnly('office_location', true);
g_form.setReadOnly('do_you_have_an_address_different_than_your_office_location_to_enter', true);
g_form.setReadOnly('enter_your_other_address_here', true);
g_form.setReadOnly('if_you_feel_the_cost_center_above_is_inaccurate_please_enter_the_correct_cost_center_below', true);
g_form.setReadOnly('phone_type', true);
g_form.setReadOnly('additional_comments', true);
}
}
}
GlideAjax Script Include -
var u_SetCostCenterMicrosoftTeams =
Class.create();
u_SetCostCenterMicrosoftTeams.prototype =
Object.extendsObject(AbstractAjaxProcessor, {
//function to set the cost center based on the department number that is in the user's profile
setCostCenter: function() {
var costCenterGR = new GlideRecord('cmn_cost_center');
var userGR = new GlideRecord('sys_user');
userGR.get(this.getParameter('sysparm_user'));
costCenterGR.addQuery('name', '=', userGR.u_department_number);
costCenterGR.query();
//if cost center GR has a next record AND the department string legnth is greater than or equal to 1 then return the sys_id of the cost center record
if (costCenterGR.next()) {
if (userGR.u_department_number.toString().length >= 1) {
return costCenterGR.sys_id;
}
// otherwise return false as a boolean flag
else {
return false;
}
}
},
//function to check if the user is in the Carrier Services or Telecom group, if they are then return true so they can edit the cost center if need be
checkUserGroup: function() {
var grpMbrGR = new GlideRecord('sys_user_grmember');
var carrierServices = '5c603803db1d13445a8158a8dc9619eb'; //sys_id of carrier services group
var telecom = '54f03443db1d13445a8158a8dc9619bd'; //sys_id of telecom group
//query the sys_user_grmember table with the user, the carrier services group, and the telecom group
grpMbrGR.addQuery('user', this.getParameter('sysparm_user'));
grpMbrGR.addQuery('group', carrierServices);
grpMbrGR.addORQuery('group', telecom);
grpMbrGR.query();
//return true or false depending upon query results
if (grpMbrGR.next()) {
return true;
} else {
return false;
}
},
type: 'u_SetCostCenterMicrosoftTeams'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2023 04:08 PM
There are a few things that could be causing the unexpected behavior you're seeing in your code. Here are some things to check:
Make sure that the GlideAjax script include u_SetCostCenterMicrosoftTeams is active.
Double-check that the sys_ids for the carrierServices and telecom groups are correct and haven't changed.
Try logging some debug information to the console to help diagnose the problem. For example, you could add the following lines to your setCostCenterEditable function:
console.log("answer:", answer);
console.log("readOnly:", g_form.isReadOnly('oracle_cost_center'));
Then open the browser console (usually F12) and see what's logged when the form loads.
Make sure that the field names you're setting to read-only or editable are correct and haven't changed.
Check that the g_user variable is defined and contains a userID property.
Verify that the client script is running on the correct form.
Finally, try clearing your browser cache and reloading the page to ensure you're running the latest version of the code.
I hope that helps you diagnose the issue you're facing!
Please mark my answer correct/helpful in case it adds value and moves you a step closer to your desired ServiceNow solution goal.
Thanks,
Punit