Issue in making "adhoc approval" field read-only on change request
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
Hello Team,
on change request table there is field called "adhoc approval" type glide list which reference to assignment groups table, in which we can select multiple groups.
This is like an additional approval triggered on change request if we add any group or groups to this field.
Now, make this field editable till the groups or group selected in the field triggered as group approval on the change request.
I tried this script include and client script, but it is getting read-only once it has value in the field, but it should be read-only once the valu triggered and present in group approval of that change.
script include:
client script:
function onLoad() {
var adhocGroups = g_form.getValue('u_adhoc_approval'); // could be multiple groups
// Start editable by default
g_form.setReadOnly('u_adhoc_approval', false);
if (adhocGroups) {
var ga = new GlideAjax('CheckAdhocApproval');
ga.addParam('sysparm_name', 'checkApproval');
ga.addParam('sysparm_change', g_form.getUniqueValue()); // current Change sys_id
ga.addParam('sysparm_group', adhocGroups); // pass all selected groups
ga.getXMLAnswer(function(answer) {
if (answer === 'true') {
// Lock the field if any selected group already has a triggered approval
g_form.setReadOnly('u_adhoc_approval', true);
}
});
}
}
script include: (client callable)
var CheckAdhocApproval = Class.create();
CheckAdhocApproval.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkApproval: function() {
var changeId = this.getParameter('sysparm_change');
var groupId = this.getParameter('sysparm_group');
if (!changeId || !groupId) {
return 'false';
}
var gr = new GlideRecord('sysapproval_group');
gr.addQuery('parent', changeId); // same Change Request
gr.addQuery('group', groupId); // exact group sys_id
gr.query();
if (gr.hasNext()) {
return 'true'; // exact match found
}
return 'false';
}
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
Hi @Sriram28 ,
You can do below :
-
Modify the Script Include to accept a comma-separated list of group IDs and check if any of those groups have an approval record for the change.
-
Modify the client script to send these group IDs correctly and handle the asynchronous result.
I am sharing the code for the Script Include (This is not tested, so pleaseverify before implementig):
var CheckAdhocApproval = Class.create();
CheckAdhocApproval.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkApproval: function() {
var changeId = this.getParameter('sysparm_change');
var groupIds = this.getParameter('sysparm_group'); // comma-separated list
if (!changeId || !groupIds) {
return 'false';
}
var groupArray = groupIds.split(',');
var gr = new GlideRecord('sysapproval_group');
gr.addQuery('parent', changeId);
gr.addQuery('group', 'IN', groupArray);
gr.query();
if (gr.hasNext()) {
return 'true'; // One or more groups found in approval records
}
return 'false';
}
});
And ensure the client script sends the comma-separated values and sets the field read-only only when any group approval is triggered:
function onLoad() {
var adhocGroups = g_form.getValue('u_adhoc_approval'); // comma separated sys_ids
// Start editable by default
g_form.setReadOnly('u_adhoc_approval', false);
if (adhocGroups) {
var ga = new GlideAjax('CheckAdhocApproval');
ga.addParam('sysparm_name', 'checkApproval');
ga.addParam('sysparm_change', g_form.getUniqueValue()); // current Change sys_id
ga.addParam('sysparm_group', adhocGroups); // comma separated groups
ga.getXMLAnswer(function(answer) {
if (answer === 'true') {
// Lock the field only if any selected group already has a triggered approval
g_form.setReadOnly('u_adhoc_approval', true);
}
});
}
}
Sandeep Dutta
Please mark the answer correct & Helpful, if i could help you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hello @SANDEEP DUTTA ,
thanks for your response, still it is getting read-only once the the field has value.
Not waiting till the approval record triggeres
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago - last edited 2 hours ago
update as this
Script Include:
var CheckAdhocApproval = Class.create();
CheckAdhocApproval.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkApproval: function() {
var change = this.getParameter('sysparm_change');
var group = this.getParameter('sysparm_group'); // Comma-separated list
if (!change || !group)
return 'false';
var gr = new GlideRecord('sysapproval_group');
gr.addQuery('parent', change);
gr.addQuery('assignment_group', 'IN', group.toString());
gr.query();
if (gr.hasNext()) {
// Any group found; approval triggered
return 'true';
}
return 'false';
}
});
Client Script:
function onLoad() {
var adhocGroups = g_form.getValue('u_adhoc_approval');
g_form.setReadOnly('u_adhoc_approval', false);
if (adhocGroups) {
var ga = new GlideAjax('CheckAdhocApproval');
ga.addParam('sysparm_name', 'checkApproval');
ga.addParam('sysparm_change', g_form.getUniqueValue());
ga.addParam('sysparm_group', adhocGroups.toString());
ga.getXMLAnswer(function(answer) {
alert('answer is' + answer );
if (answer === 'true') {
g_form.setReadOnly('u_adhoc_approval', true);
}
});
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader