For the Hardware Asset Management Return Request Catalog Item
On the "Return Equipment" Task, sequence 2, the Assigned To field should populate with the Requested For on the RITM upon creation of the Task.
There are already Assignment Group mappings based on RHM on the task formatter. We want to automatically assign the task to the Requester, provided that the Requester belongs to the Assignment group. If the Requester is not a member of the Assignment group (that is populated first based on RHM), the "Assigned to" will remain blank for manual assignment.I have written a client script with script include but its not working.
function onLoad() {
// Get the requested_for and assignment_group fields
var requestedFor = g_form.getValue('requested_for');
var assignmentGroup = g_form.getValue('assignment_group');
// Log the values to ensure they are being fetched correctly
console.log("Requested For Sys ID: " + requestedFor);
console.log("Assignment Group Sys ID: " + assignmentGroup);
// If requested_for or assignment_group is empty, exit
if (!requestedFor || !assignmentGroup) {
console.log("Either requested_for or assignment_group is empty. Exiting function.");
return;
}
// Use GlideAjax to check if the requested_for user is a member of the assignment_group
var ga = new GlideAjax('CheckUserInGroup');
ga.addParam('sysparm_name', 'isUserInGroup');
ga.addParam('sysparm_user', requestedFor);
ga.addParam('sysparm_group', assignmentGroup);
// Get XML response and handle the result
ga.getXMLAnswer(function(response) {
var isUserInGroup = response.responseXML.documentElement.getAttribute('answer');
console.log("Server response: " + isUserInGroup); // Debug the response from server
if (isUserInGroup === 'true') {
// If the user is a member of the group, set the assigned_to field
g_form.setValue('assigned_to', requestedFor);
} else {
// If the user is not a member, leave the assigned_to field blank
g_form.setValue('assigned_to', '');
}
});
}
*********************************************************************************************************
var CheckUserInGroup = Class.create();
CheckUserInGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isUserInGroup: function() {
var userSysId = this.getParameter('sysparm_user'); // Get the requested_for Sys ID
var groupSysId = this.getParameter('sysparm_group'); // Get the assignment_group Sys ID
// Ensure that the userSysId and groupSysId are valid
if (!userSysId || !groupSysId) {
return 'false'; // Return false if any parameter is missing
}
// Check if the user is a member of the group
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', userSysId);
gr.addQuery('group', groupSysId);
gr.query();
// Return 'true' if the user is a member of the group, else 'false'
return gr.hasNext() ? 'true' : 'false';
}
});
What am I missing??