Urgent: Need help- UI Macro should run based on Roles.

Yogesh14
Tera Contributor

Hello, @Ankur Bawiskar 

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.

find_real_file.png

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

7 REPLIES 7

Yousaf
Giga Sage

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.***

Yogesh14
Tera Contributor

Hi Yousaf,

I tried the below and properly closing </j2:if> at last but it is not working.

find_real_file.png

 

Regards

Yogesh

Ankur Bawiskar
Tera Patron
Tera Patron

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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.

<j:if test="${gs.hasRole('itil')}">
 
</j:if>