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

Hi,

this line is not present in your UI macro

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

It seems the itil user is not able to insert new choices using GlideRecord

Can you check what comes in alert for bold

<?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
        alert(choiceGr.canCreate());
        alert(choiceGr.canWrite())
        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,

after adding these 2 lines nothing is happening. I mean no alerts and the data is also not getting inserted.

alert(choiceGr.canCreate());
alert(choiceGr.canWrite());

 

Regards

Yogesh

Hi,

not very sure then

regards
Ankur

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