How to pass any variable value from client script to UI Macro

patSnow
Tera Contributor

find_real_file.png

Hi All,

Based on User selection, I want to show "allocated PC's from cmdb_ci_pc_hardware". For that i have created "macro with label" variable and also created one on change client script. I want to pass User sys_id to ui micro so i can query records from table using glide record. But its not working as expected. I gone through many community answers but still struggling to correct my code. Can someone please help me.  I can also use script include to call ui macro but not sure how to pass parameters to ui macro and how to use those parameters inside evaluate tag.

Client script:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   g_form.getValue("user");
    
    sendUser(g_form.getValue("user"));
   
}

UI 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">
<script>

function sendUser(user)

{
var user=g_form.addInfoMessage("useer"+user);
    }

</script>
    <table border="0" cellspacing="2" cellpadding="0" width="100%">   
     <tr>   
   <td style="text-align:center;background:#70B8FF; border-radius:5px;">Allocated Personal Computers</td>
                                  
   </tr>   
        
    
   <g:evaluate var="jvar_member" object="true">   
             
                     var member = new GlideRecord('cmdb_ci_pc_hardware');
                     member.addQuery("assigned_to",user);
                     member.query();
                     member;
   </g:evaluate>
   <j:while test="${jvar_member.next()}">
     <tr><td>${member.name.getDisplayValue()}</td></tr>
  </j:while>
    
   </table>
</j:jelly>

 

4 REPLIES 4

Jon Barnes
Kilo Sage

everything in your g:evaluate element gets processed only when the macro first loads. for this use case, your sendUser function should really use GlideAjax to fetch the data via an ajax call to a script include on the server, and then build your html table in the ajax response and add it using JQuery (or similar) to an element you will define here.

Thanks for the reply

is it possible to call UI Macro from script include.....how i can send values to macro?

No, you have to go the other direction. the concept is to put the lines of code you currently have in your g:evaluate section into a client-callable script include. Then your sendUser will use a GlideAjax call to call the function on the script include. this will return your resulting data which you can then parse and render as HTML in your macro.

have a look here for samples on doing GlideAjax calls:

https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=c_GlideAjaxAPI

Sanjay Bagri1
Tera Guru
Hi , Please go once below link might be helpful for you. https://community.servicenow.com/community?id=community_question&sys_id=3c3b0beddb5cdbc01dcaf3231f9619d2&view_source=searchResult Thanks Sanjay Bagri