UI macros in front of caller field

kaushiki berter
Tera Expert

Hi, I was learning about UI macros and my requirement is to add Assign me icon in front of caller field so that whenever user clicks on that icon, he can assign himself as caller. I have created following UI macro and added it to the dictionary level on caller field.

After adding this I was not able to see that macro on caller field in new form. Kindly help me with the functionality.

kaushikiberter_0-1722233372995.pngkaushikiberter_1-1722233497505.png

 

 

1 ACCEPTED SOLUTION

kaushiki berter
Tera Expert

I have found the solution. Above scripts were missing the main functionality. The exact solution should be that whenever the UI macro is clicked it should call a function which should contain the parameter as the field name. With the below code I was able to perform the functionality. 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<span id="add_me_button" class="btn btn-default icon-user-add" tabindex="0" title="Add me" onclick="addMe('caller_id')">
  <span class="sr-only">Add me</span>
</span>
<script>
  function addMe(reference) {
    var userID = g_user.userID;
    g_form.setValue(reference, userID);
  }
</script>
</j:jelly>

View solution in original post

6 REPLIES 6

Voona Rohila
Kilo Patron
Kilo Patron

Hi @kaushiki berter 

Try this code in your Macro

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<span id="${jvar_n}" class="btn btn-default icon-user-add" tabindex="0" title="Add me">
<span class="sr-only">Add me</span>
</span><script>
      function addMe(reference) {
         //Get the user reference field and populate the current user
         var s = reference.split('.');
         var referenceField = s[1];
         g_form.setValue(referenceField, '$[gs.getUserID()]');
      }
   </script>
</j:jelly>



Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Thank you for your solution!

this macro is not at all visible if I am keeping both the macros on this field 

kaushikiberter_0-1722278291411.png

And if I remove the out of box one and keep the custom one then also it is not working and not adding me in caller field.

kaushikiberter_1-1722278457981.png

 

 

Can you please share updated code?

You can use below code, I tested in my pdi and it's working as expected

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<span id="${jvar_n}" class="btn btn-default icon-user-add" tabindex="0" title="Add me">
<span class="sr-only">Add me</span>
</span><script>
      function addMe(reference) {
         //Get the user reference field and populate the current user
         var s = reference.split('.');
         var referenceField = s[1];
         g_form.setValue(referenceField, '$[gs.getUserID()]');
      }
   </script>
</j:jelly>

VoonaRohila_0-1722326269034.png

VoonaRohila_1-1722326296722.png

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Thank you for the solution!

With the above changes I can see all the ui macros all together. Previously I used , instead of ; in the syntax of ref_contributions in attribute section due to which it was not working . However with the above code I am still not able to add currently logged in user in the caller field (i.e. System administrator in pdi) 

kaushikiberter_0-1722330371011.png

It is basically adding nothing in the field.

UI macro- 

kaushikiberter_1-1722330477777.png

code

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<span id="${jvar_n}" class="btn btn-default icon-user-add" tabindex="0" title="Add me">
<span class="sr-only">Add me</span>
</span><script>
      function addMe(reference) {
         //Get the user reference field and populate the current user
         var s = reference.split('.');
         var referenceField = s[1];
         g_form.setValue(referenceField, '$[gs.getUserID()]');
      }
   </script>
</j:jelly>

kaushikiberter_2-1722330579115.png