- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2020 12:21 AM
Hi,
In our Group Creation SC form, we have a "Group Member" List Collector1 variable(variable attribute: glide_list reference: user table) where user manually select the users from the list. OnSubmit or Onchange of Catalog, we need to identify which of these users already has an existing role and move those users with no roles to another List Collector2 with same attribute as the first (variable attribute: glide_list reference: user table) to separate them and to make the referencing easier in the Workflow approval process.
List Collector 1 Variable - users input all members (with and without role)
List Collector 2 (Hidden in the form)- This needs to populate with users from List Collector 1 with no ITIL role
List Collector 3 (Hidden in the form)- This needs to populate with users from List Collector 1 with ITIL role
I initially created an Onchange Client Script and Script include for same requirement before to identify if user already has an ITIL role but field is only a reference and no population required, List collectors seem to be complex.
Any help will be very much appreciated. Thank you!
Solved! Go to Solution.
- Labels:
-
Service Catalog

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2020 06:26 PM
Hi This can be done if you need to split the users into individual variables. If you're using a dedicated workflow for this item, you could just use a script action that runs through the variable and itterates over the values and then stores the values in two arrays on the workflow scratchpad?
Using an onChange script will be invoked every time a user is added.
var CatUtils = Class.create();
CatUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isLicensed: function(){
var answer = false;
var user = new GlideRecord('sys_user_has_role');
user.addQuery('user' , this.getParameter('sysparm_user'));
user.addQuery('role', '282bf1fac6112285017366cb5f867469'); //ITIL Role
user.query();
if(user.hasNext()){
answer = true;
}
return answer;
},
type: 'CatUtils'
});
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('CatUtils');
ga.addParam('sysparm_name','isLicensed');
ga.addParam('sysparm_user',newValue);
ga.getXMLAnswer(response);
function response(answer){
try{
if(answer == 'true'){
addUser(newValue,"license");
} else{
addUser(newValue,"unlicense");
}
g_form.setValue('users','');
}catch(e){
}
}
function addUser(value,list){
var vals = g_form.getValue(list);
if(vals.indexOf(",") > -1 || vals != ''){
vals += "," + value
} else{
vals = value;
}
g_form.setValue(list,vals);
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2020 06:26 PM
Hi This can be done if you need to split the users into individual variables. If you're using a dedicated workflow for this item, you could just use a script action that runs through the variable and itterates over the values and then stores the values in two arrays on the workflow scratchpad?
Using an onChange script will be invoked every time a user is added.
var CatUtils = Class.create();
CatUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isLicensed: function(){
var answer = false;
var user = new GlideRecord('sys_user_has_role');
user.addQuery('user' , this.getParameter('sysparm_user'));
user.addQuery('role', '282bf1fac6112285017366cb5f867469'); //ITIL Role
user.query();
if(user.hasNext()){
answer = true;
}
return answer;
},
type: 'CatUtils'
});
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('CatUtils');
ga.addParam('sysparm_name','isLicensed');
ga.addParam('sysparm_user',newValue);
ga.getXMLAnswer(response);
function response(answer){
try{
if(answer == 'true'){
addUser(newValue,"license");
} else{
addUser(newValue,"unlicense");
}
g_form.setValue('users','');
}catch(e){
}
}
function addUser(value,list){
var vals = g_form.getValue(list);
if(vals.indexOf(",") > -1 || vals != ''){
vals += "," + value
} else{
vals = value;
}
g_form.setValue(list,vals);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2020 10:36 PM
Thanks for this script, Kieran. Yes, we plan to use the values stored in the 2 variables to make it easier in the WF approval process.
I tried this in our instance but the license/unlicense list doesn't seem populating onchange. 😞 Can you help me check below please? Also, do you think this will run if multiple users are selected at once? or is Onsubmit better?
Onchange client Script on users variable
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('CatUtils');
ga.addParam('sysparm_name','isLicensed');
ga.addParam('sysparm_user',newValue);
ga.getXMLAnswer(response);
function response(answer){
try{
if(answer == 'true'){
addUser(newValue,"license");
} else{
addUser(newValue,"unlicense");
}
g_form.setValue('users','');
}catch(e){
}
}
function addUser(value,list){
var vals = g_form.getValue(list);
if(vals.indexOf(",") > -1 || vals != ''){
vals += "," + value;
} else{
vals = value;
}
g_form.setValue(list,vals);
}
}
Script Include
var CatUtils = Class.create();
CatUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isLicensed: function(){
var answer = false;
var user = new GlideRecord('sys_user_has_role');
user.addQuery('user' , this.getParameter('sysparm_user'));
user.addQuery('role', '282bf1fac6112285017366cb5f867469'); //ITIL Role
user.query();
if(user.hasNext()){
answer = true;
}
return answer;
},
type: 'CatUtils'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2020 10:52 PM
Hi,
update as below
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('CatUtils');
ga.addParam('sysparm_name','isLicensed');
ga.addParam('sysparm_user',newValue);
ga.getXMLAnswer(response);
function response(answer){
try{
if(answer.toString() == 'true'){
addUser(newValue,"license");
} else{
addUser(newValue,"unlicense");
}
g_form.setValue('users','');
}catch(e){
}
}
function addUser(value,list){
var vals = g_form.getValue(list);
if(vals.indexOf(",") > -1 || vals != ''){
vals += "," + value;
} else{
vals = value;
}
g_form.setValue(list,vals.toString());
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2020 11:05 PM