- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2024 12:55 PM
Hello all,
I am interested in hiding a specific value in the close_code dropdown field on the change_request form. The conditions are:
- Must have a related INC in the "Incidents Caused By Change" related list.
- If the user has the change_manager role then they can see the drop down value.
I have tried Client Scripts and UI Policies to no avail. Anyone else have any other ideas?
Thank you.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2024 01:24 PM
@lonesoac01thanks, I modified the client script slightly (removed all the logs and error handling btw).
function onLoad() {
var currentUser = g_user.userID;
var currentSysId = g_form.getUniqueValue();
var ga = new GlideAjax('global.CheckRelatedRecordAndGroup');
ga.addParam('sysparm_name', 'getRelatedINCs');
ga.addParam('ChangeSYSID', currentSysId);
ga.addParam('UserSYSID', currentUser);
ga.getXMLAnswer(handleResponse);
function handleResponse(response) {
try {
var res = JSON.parse(response);
// Check if there is a related incident record and if the user is not a change manager
if (res.RelatedINCRecord === true && res.UsrIsChgMgr === false) {
// Hide specific drop-down option in the close_code field
hideCloseCodeOption('successful');
} else {}
} catch (e) {}
}
function hideCloseCodeOption(optionValue) {
g_form.removeOption('close_code', optionValue);
}
}
FYI, you might want to use a sys_id instead of the name when querying the group.
Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2024 01:01 PM
Hi @lonesoac01,
You should be able to achieve this via a Client Script.
Can you share what you have tried?
Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2024 01:05 PM
Client Script:
function onLoad() {
var currentUser = g_user.userID;
var currentSysId = g_form.getUniqueValue();
var ga = new GlideAjax('global.CheckRelatedRecordAndGroup');
ga.addParam('sysparm_name', 'getRelatedINCs');
ga.addParam('ChangeSYSID', currentSysId);
ga.addParam('UserSYSID', currentUser);
ga.getXMLAnswer(handleResponse);
function handleResponse(response) {
try {
var res = JSON.parse(response);
console.log('Response received:', res);
// Check if there is a related incident record and if the user is not a change manager
if (res.RelatedINCRecord === 'true' && res.UsrIsChgMgr === 'false') {
console.log('Conditions met, hiding the specific option.');
// Hide specific drop-down option in the close_code field
hideCloseCodeOption('abc');
alert('The selection should not be there!');
} else {
console.log('Conditions not met.');
alert('bleh');
}
} catch (e) {
console.error('Error parsing response: ', e);
}
}
function hideCloseCodeOption(optionValue) {
var field = g_form.getControl('close_code');
if (field) {
var $field = $(field);
var $options = $field.find('option');
console.log('Field options:', $options);
var optionFound = false;
$options.each(function() {
console.log('Checking option:', this.value);
if (this.value === optionValue) {
optionFound = true;
console.log('Found option to hide:', this.value);
$(this).hide();
console.log('Option hidden:', optionValue);
}
});
if (!optionFound) {
console.log('Option not found:', optionValue);
}
} else {
console.error('Field close_code not found');
}
}
}
Script Include:
var CheckRelatedRecordAndGroup = Class.create();
CheckRelatedRecordAndGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getRelatedINCs: function() {
var result = {
RelatedINCRecord: false,
UsrIsChgMgr: false
};
var changeRequestSysId = this.getParameter('ChangeSYSID');
var userSysId = this.getParameter('UserSYSID');
// Check for related incident records
var incidentGR = new GlideRecord('incident');
incidentGR.addQuery('caused_by', changeRequestSysId);
incidentGR.setLimit(1); // Limit to one record for efficiency
incidentGR.query();
if (incidentGR.next()) {
result.RelatedINCRecord = true;
}
// Check if user is part of 'Change Management' group
var groupMemberGR = new GlideRecord('sys_user_grmember');
groupMemberGR.addQuery('group.name', 'Change Management');
groupMemberGR.addQuery('user', userSysId);
groupMemberGR.setLimit(1); // Limit to one record for efficiency
groupMemberGR.query();
if (groupMemberGR.next()) {
result.UsrIsChgMgr = true;
}
return JSON.stringify(result);
},
type: 'CheckRelatedRecordAndGroup'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2024 01:24 PM
@lonesoac01thanks, I modified the client script slightly (removed all the logs and error handling btw).
function onLoad() {
var currentUser = g_user.userID;
var currentSysId = g_form.getUniqueValue();
var ga = new GlideAjax('global.CheckRelatedRecordAndGroup');
ga.addParam('sysparm_name', 'getRelatedINCs');
ga.addParam('ChangeSYSID', currentSysId);
ga.addParam('UserSYSID', currentUser);
ga.getXMLAnswer(handleResponse);
function handleResponse(response) {
try {
var res = JSON.parse(response);
// Check if there is a related incident record and if the user is not a change manager
if (res.RelatedINCRecord === true && res.UsrIsChgMgr === false) {
// Hide specific drop-down option in the close_code field
hideCloseCodeOption('successful');
} else {}
} catch (e) {}
}
function hideCloseCodeOption(optionValue) {
g_form.removeOption('close_code', optionValue);
}
}
FYI, you might want to use a sys_id instead of the name when querying the group.
Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-05-2024 05:20 AM
I implemented the code you gave me and it worked like a charm!
