- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Many a times there is a requirement to auto-populate values in list collector based on change of some other variable.
I have seen few blogs or community answers and thought of creating this blog which would help folks for making sure the script runs in both native and portal view.
Example: Consider there is a catalog request wherein users can submit request for group membership removal for user
User Variable - Reference to sys_user table
Group Variable - Reference to sys_user_group table (List Collector)
We would want to populate values to List Collector Group variable with the groups to which the user belongs to
Script Include: It should be client callable script include
var getAssignGroups = Class.create();
getAssignGroups.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserGroups: function(){
var listGroup = [];
var requester = this.getParameter('sysparm_request_for');
var parm = this.getParameter('sysparm_view');
var jsonArr = [];
var recGrp = new GlideRecord('sys_user_grmember');
recGrp.addQuery('user', requester);
recGrp.query();
while (recGrp.next()) {
if(parm == 'portal'){
listGroup.push(recGrp.group.toString());
}
else if(parm == 'native'){
var obj = {};
obj.name = recGrp.group.name.toString();
obj.sys_id = recGrp.group.toString();
jsonArr.push(obj);
}
}
if(parm == 'portal')
return listGroup.toString();
else
return JSON.stringify(jsonArr);
},
type: 'getAssignGroups'
});
Client Script: Ensure "Isolate Script" field is set to false for onChange Catalog Client Script on User Variable
The field Isolate Script is not present on the form view but from the list layout it can be added and set to false
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
// give here the list collector variable name
g_form.clearValue('group');
var parm = '';
if (window === null)
parm = 'portal';
else
parm = 'native';
var ajax = new GlideAjax('getAssignGroups');
ajax.addParam('sysparm_name', 'getUserGroups');
ajax.addParam('sysparm_request_for', newValue);
ajax.addParam('sysparm_view', parm);
ajax.getXML(populateValues);
}
function populateValues(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var arr = answer.split('||');
// give here the list collector variable name
if (window === null) {
g_form.setValue('group', arr[0]); // Mobile/Portal Compatible
} else {
addItemstoList('group', answer); // Native Compatible
}
}
function addItemstoList(listCollector, groupList) {
var parser = JSON.parse(groupList);
var arrSysId = [];
var arrName = [];
for(var i=0;i<parser.length;i++){
arrName.push(parser[i].name.toString());
arrSysId.push(parser[i].sys_id.toString());
}
var varName = listCollector;
var leftBucket = gel(varName + '_select_0');
var rightBucket = gel(varName + '_select_1');
var rightOptions = rightBucket.options;
var rightIDs = [];
//Remove --None--
for (var k= 0; k < rightOptions.length; k++) {
var value = rightOptions[k].innerHTML;
if (value == '--None--') {
rightBucket.remove(0);
}
}
// Add new options
if (arrName.length > 0) {
var myCIArray = arrName.toString().split(',');
for (var j = 0; j < myCIArray.length; j++) {
addOption(rightBucket, arrSysId[j], myCIArray[j]);
sortSelect(rightBucket);
leftBucket.onchange();
}
}
// sort the buckets
sortSelect(rightBucket);
}
Screenshots:
1) Catalog Item:
2) Catalog Client Script: Isolate Script -> False
3) Native View: User selected is "System Admin"; groups for this user are auto-populated in list collector
4) Portal View: User selected is "System Admin"; groups for this user are auto-populated in list collector
5) "System Admin" belongs to 4 Groups:
Thanks for reading the blog and do provide your inputs/suggestions if any.
Hope you find this article helpful. Don’t forget to Mark it Helpful, Bookmark.
Thanks,
Ankur Bawiskar
- 26,216 Views
- « Previous
-
- 1
- 2
- 3
- 4
- Next »
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.