How to call a UI Macro with parameters?

bcronrath
Kilo Guru

I was reading through UI Macros - ServiceNow Wiki and under description it says "Describe the purpose of the macro and parameters passed to it.".   I'm wondering how you go about passing parameters when calling a UI Macro, and how to retrieve them within the UI Macro itself?

Best regards,

Brian

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Brian,



If you name the parameter "body", for example in the UI page, it is exposed in the UI macro as jvar_body.



Here's an example of how I call a UI macro with parameters from a UI page and then use those parameters.



UI Page code:


              <g2:macro_invoke macro="exam_info_messages" body="${gs.getMessage('exam_all_answered')}" />



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


<!-- info messages -->




<div class="system_message outputmsg_info">


  <table cellspacing="0" cellpadding="0" border="0" class="background_transparent">


  <tr>


  <td class="message_image"><img src="images/outputmsg_info.gifx" alt="${gs.getMessage('Informational Message')}" width="16" height="16"/></td>


  <td width="100%">


  ${jvar_body}


  </td>


  </tr>


  </table>


</div>


</j:jelly>


View solution in original post

5 REPLIES 5

Jon Barnes
Kilo Sage

you need to put an element like this at the top of your macro, where parameter1 will be the parameter name:



<g:macro parameter1="" />



you can access it similar to this:



${jvar_parameter1}



and then call the macro with the parameter you want...



<g:macro_invoke macro="macro name" parameter1="some value" />


Chuck Tomasi
Tera Patron

Hi Brian,



If you name the parameter "body", for example in the UI page, it is exposed in the UI macro as jvar_body.



Here's an example of how I call a UI macro with parameters from a UI page and then use those parameters.



UI Page code:


              <g2:macro_invoke macro="exam_info_messages" body="${gs.getMessage('exam_all_answered')}" />



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


<!-- info messages -->




<div class="system_message outputmsg_info">


  <table cellspacing="0" cellpadding="0" border="0" class="background_transparent">


  <tr>


  <td class="message_image"><img src="images/outputmsg_info.gifx" alt="${gs.getMessage('Informational Message')}" width="16" height="16"/></td>


  <td width="100%">


  ${jvar_body}


  </td>


  </tr>


  </table>


</div>


</j:jelly>


Thanks guys!   I think I'm real close now, but two questions I have are, what would the proper method be to:



a.)   Pass in a dynamic value as the parameter when invoking the UI macro



b.)   Call the parameter within the g:evaluate javascript of the invoked UI macro



Here is how I'm doing in the passing (looping through a bunch of "glMon" gliderecords):



<tbody>


<j:while test=


"${glMon.next()}">


  <g:macro_invoke macro="aggregate_mon_def" sysid="${glMon.sys_id}" service="${glMon.u_business_service.name}" />



  </j:while>


</tbody




Then in that aggregate_mon_def ui macro:



<g:evaluate   object="true">


              var glAggr = new GlideRecord('u_monitoring_aggregation');


  glAggr.orderBy('u_monitoring_definition',jvar_sysid);


  glAggr.orderByDesc('sys_created_on');


  glAggr.addQuery('sys_created_on','>',gs.daysAgo(7));


              glAggr.query();


  glAggr;


      </g:evaluate>


  <tr><td style="padding-left: 5px">${jvar_service}</td>


  <j:while test="${glAggr.next()}">


  <g2:no_escape><td style="padding-left: 5px">${glAggr.u_result.getDisplayValue()}</td></g2:no_escape>


  </j:while>


  </tr>



I suspect I need to do something special on the call as it doesn't seem the value is yet making it to the UI macro when I call it.   I'm also trying to just get them to test output by passing them to a macro and calling <g2:no_escape>${jvar_service}</g2:no_escape> without much luck.   Are you able to see what I might be doing wrong?


Ah n/m figured it out, had an error with my parent ui macro gliderecord call, works like a charm now, thanks guys!