Set Assigned To Field Depeand on Assignment Group In Catalog variables
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I created two variable name as "Assignments Group" as a reference field and refer it to Group table , and "Assigned To" and refer it to the user table , if there is a single user in the assignment group then the user should set auto in the assigned to field
6 REPLIES 6
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello @piyushbedre ,
Use the below script include & Client script the achieve the output as per you requirement :
Script Include :
checkSingleUser: function() {
var groupId = this.getParameter('sysparm_group_id');
var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery('group', groupId);
grMember.query();
if (grMember.getRowCount() == 1) {
if (grMember.next()) {
var result = {
sys_id: grMember.getValue('user'),
name: grMember.user.name.toString()
};
return JSON.stringify(result);
}
}
return null;
}
Catalog client script :
if (newValue === '') {
g_form.clearValue('assigned_to');
return;
}
var ga = new GlideAjax('<SC_Name>');
ga.addParam('sysparm_name', 'checkSingleUser');
ga.addParam('sysparm_group_id', newValue);
ga.getXMLAnswer(function(answer) {
if (answer) {
var userObj = JSON.parse(answer);
g_form.setValue('assigned_to', userObj.sys_id);
} else {
g_form.clearValue('assigned_to');
}
});
If my response helped mark as helpful and accept the solution.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @piyushbedre
You can achieve this requirement using an **onChange Catalog Client Script + GlideAjax + Script Include**.
Requirement:
I have two variables:
1. Assignment Group
Type: Reference
Table: Group (`sys_user_group`)
2. Assigned To
Type: Reference
Table: User (`sys_user`)
Expected behavior:
When user selects an Assignment Group:
If the selected group has only one member, automatically populate the Assigned To field with that user.
If the group has multiple users, leave the field empty.
Step 1: Create Script Include (GetSingleGroupMember)
Make sure Client Callable is checked.
var GetSingleGroupMember = Class.create();
GetSingleGroupMember.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUser: function() {
var groupId = this.getParameter('sysparm_group');
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group', groupId);
gr.query();
var count = 0;
var userId = '';
while (gr.next()) {
count++;
userId = gr.user.toString();
if (count > 1) {
return '';
}
}
return userId;
},
type: 'GetSingleGroupMember'
});
Step 2: Create onChange Catalog Client Script on Assignment Group
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.setValue('asigned_to',"");
var ga = new GlideAjax('GetSingleGroupMember');
ga.addParam('sysparm_name', 'getUser');
ga.addParam('sysparm_group', newValue);
ga.getXMLAnswer(function(response) {
if (response) {
g_form.setValue('assigned_to', response);
}
});
}
How this works:
User selects Assignment Group
Client Script calls Script Include using GlideAjax
Script Include checks `sys_user_grmember`
If only one member exists → return user sys_id
Assigned To gets populated automatically
If multiple users exist → field remains empty
Test Results:
Group A → 1 member → Assigned To auto-populated
Group B → Multiple members → Assigned To remains blank
Hope this helps.