Urgent: Need help- UI Macro should run based on Roles.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-15-2022 04:58 AM
Hello,
I have created a UI Macro: to add new options to a field and it is working fine for me. But when i am impersonating to other user with ITIL role it is not working.
If we add any value in Add Data field and click on Primary Button and save the form then the value will be added and we can see that value in New Values dropdown.
UI Macro script:
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<a onclick='updateList()'>
<span
aria-label="${HTML:gs.getMessage('Update List Field Choices')}"
title="${gs.getMessage('Update List Field Choices')}"
alt="${gs.getMessage('Update List Field Choices')}"
>
<button type="button" class="btn btn-primary">Primary</button>
</span>
</a>
<script>
function updateList(){
var listfield = 'u_new_values';//replace with the name of your List field
var tablename = 'u_new_table';//replace with the name of the table you created the List field on
var choicetext = g_form.getValue('u_add_data');//replace with the name of your String field
var choiceGR = new GlideRecord('sys_choice');
//first check to make sure the choice doesn't already exist
choiceGR.addQuery('name', tablename);
choiceGR.addQuery('element', listfield);
choiceGR.addQuery('value', choicetext);
choiceGR.query();
if(choiceGR.next()){
alert('Choice already exists on List');
return;
}
else{
//choice not found so create a new one
choiceGR.initialize();
choiceGR.name = tablename;
choiceGR.element = listfield;
choiceGR.language = 'en';
choiceGR.label = choicetext;
choiceGR.value = choicetext;
choiceGR.insert();
alert('Choice added to List');
g_form.clearValue('u_add_data');//replace with the name of your String field
}
}
</script>
</j:jelly>
Regards
Yogesh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-15-2022 05:24 AM
Hi Yogesh,
Can you please try this.
<j:if test="${gs.hasRole('itil')}">
Restricting visibility of UI Macro based on roles
Can we restrict the UI macro visibility to only certain roles of users?
UI macro visible only to some users
Mark Correct and Helpful if it helps.
***Mark Correct or Helpful if it helps.***
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-15-2022 06:13 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-15-2022 07:45 AM
Hi,
So what's not working?
No alert is coming?
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<a onclick='updateList()'>
<span
aria-label="${HTML:gs.getMessage('Update List Field Choices')}"
title="${gs.getMessage('Update List Field Choices')}"
alt="${gs.getMessage('Update List Field Choices')}"
>
<button type="button" class="btn btn-primary">Primary</button>
</span>
</a>
<script>
function updateList(){
var listfield = 'u_new_values';//replace with the name of your List field
var tablename = 'u_new_table';//replace with the name of the table you created the List field on
var choicetext = g_form.getValue('u_add_data');//replace with the name of your String field
var choiceGR = new GlideRecord('sys_choice');
//first check to make sure the choice doesn't already exist
choiceGR.addQuery('name', tablename);
choiceGR.addQuery('element', listfield);
choiceGR.addQuery('value', choicetext);
choiceGR.query();
if(choiceGR.next()){
alert('Choice already exists on List');
return;
}
else{
//choice not found so create a new one
choiceGR.initialize();
choiceGR.name = tablename;
choiceGR.element = listfield;
choiceGR.language = 'en';
choiceGR.label = choicetext;
choiceGR.value = choicetext;
choiceGR.insert();
alert('Choice added to List');
g_form.clearValue('u_add_data');//replace with the name of your String field
}
}
</script>
</j:jelly>
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
‎09-15-2022 08:01 AM
Hi Ankur,
For Admin role it is working fine, adding new options in new value field and getting alerts as well. But for other users i am only getting alerts but the new options are not adding up.
also i tried to add this line of code but it is not working.