- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2022 12:25 AM
Hi all,
How to get list of roles belong to the user in drop down in catalog item?
Please suggest me the script include and catalog client script to achieve this requirement.
Thanks
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2022 12:54 AM
Hi
Please update your scripts as provided below:
Script Include: Just to remind
- Client Callable: true
- Script:
var yourScriptIncludeName = Class.create(); yourScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, { getStates: function() { var list = []; var userValue = this.getParameter('sysparm_val'); //Issue1: Table name was not in quotes var map = new GlideRecord('sys_user_has_role'); map.addQuery('user.sys_id', userValue); map.query(); while (map.next()) { var eObj = {}; eObj.name = map.role.name + ''; eObj.sys_id = map.getValue('role'); list.push(eObj); } } return JSON.stringify(list); }, type: 'yourScriptIncludeName' });
Client Script:
- Type: On Change
- Field/Variable: Field corresponding to req
- Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === '') { return; } //GlideAjax Call to fetch Roles for Requestor var ga = new GlideAjax('yourScriptIncludeName'); ga.addParam('sysparm_name', 'getStates'); ga.addParam('sysparm_val', newValue); ga.getXMLAnswer(stateCallback); } function stateCallback(answer) { alert('answer=' + answer); var i = 1; var objList = JSON.parse(answer); g_form.clearOptions('role'); for (var keys in objList) { g_form.addOption('role', objList.sys_id, objList.name); i++; } //Select the Last Option g_form.setValue('role', objList[i].sys_id, objList[i].name); }
Remark: Ensure the variable/field is of Type Choice.
Please mark my answer as correct if this solves your issues!
If it helped you in any way then please mark helpful!
Thanks and regards,
Kartik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2022 12:54 AM
Hi
Please update your scripts as provided below:
Script Include: Just to remind
- Client Callable: true
- Script:
var yourScriptIncludeName = Class.create(); yourScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, { getStates: function() { var list = []; var userValue = this.getParameter('sysparm_val'); //Issue1: Table name was not in quotes var map = new GlideRecord('sys_user_has_role'); map.addQuery('user.sys_id', userValue); map.query(); while (map.next()) { var eObj = {}; eObj.name = map.role.name + ''; eObj.sys_id = map.getValue('role'); list.push(eObj); } } return JSON.stringify(list); }, type: 'yourScriptIncludeName' });
Client Script:
- Type: On Change
- Field/Variable: Field corresponding to req
- Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === '') { return; } //GlideAjax Call to fetch Roles for Requestor var ga = new GlideAjax('yourScriptIncludeName'); ga.addParam('sysparm_name', 'getStates'); ga.addParam('sysparm_val', newValue); ga.getXMLAnswer(stateCallback); } function stateCallback(answer) { alert('answer=' + answer); var i = 1; var objList = JSON.parse(answer); g_form.clearOptions('role'); for (var keys in objList) { g_form.addOption('role', objList.sys_id, objList.name); i++; } //Select the Last Option g_form.setValue('role', objList[i].sys_id, objList[i].name); }
Remark: Ensure the variable/field is of Type Choice.
Please mark my answer as correct if this solves your issues!
If it helped you in any way then please mark helpful!
Thanks and regards,
Kartik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2022 11:07 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2022 11:35 PM
Hi
Did you try to copy the whole function (in Script Include) and Client Script as I have changed the parameter "sysparm_val" and did you also ensure that:
- Script Include is Client Callable
- Client Script is onChange and it is on Field/Variable corresponding to Requested For
Please check these and let me know if this solves your issue.
Please mark my answer as correct if this solves your issues!
If it helped you in any way then please mark helpful!
Thanks and regards,
Kartik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2022 10:36 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2022 03:03 AM
Hi
In the response, you are receiving an array of objects so we need to adapt our client script, so please find the updated Client Script:
Client Script:
- Type: On Change
- Field/Variable: Field corresponding to req
- Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === '') { //If form is loading or Requester is empty then //Clear the options g_form.clearOptions('role'); return; } //Clear options to rebuild the list on change of Requester g_form.clearOptions('role'); //GlideAjax Call to fetch Roles for Requestor var ga = new GlideAjax('yourScriptIncludeName'); ga.addParam('sysparm_name', 'getStates'); ga.addParam('sysparm_val', newValue); ga.getXMLAnswer(stateCallback); } function stateCallback(answer) { alert('answer=' + answer); if(answer) { for(var i = 0; i < answer.length; i++) { var objList = JSON.parse(answer[i]); g_form.addOption('role', objList.sysID, objList.name); //Select the Last Option if(i == answer.length - 1) { g_form.setValue('role', objList.sys_id, objList.name); } } } }
Remark: Ensure the variable/field is of Type Choice.
Please mark my answer as correct if this solves your issues!
If it helped you in any way then please mark helpful!
Thanks and regards,
Kartik