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

Lets add a little more challenge to this. I want to call a macro (displaying as a formatter on the CMDB form) recursively. Because I'm using "current" to lookup references to the selected CI, the scripts run in phase 2. I don't think there is any sort of scope associated to the macro because I can reference the primary macro values from the inner (recursive) macro.



What ends up happening is that a recursive forEach loop is processed and I get an error message stating the "<j2:forEach> requires a closing tag" message.



Is this possible to do? I know it would be much easier in Angular but my requirement is to display a "Location Map" on the form of the selected CI.



So, I have a JavaScript Object that looks like this:
[{


    name: "Server1",


    parents: [{


          name: "Rack1",


          parents: [{


                  name: "Computer Room1"


                  parents: []


            }]


    }]


}]



I want to display this hierarchy in the formatter visually. keeping it easy it might look like this
Server1 --> Rack1 --> Computer Room 1



Primary 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">


      <g2:evaluate var="jvar_li" object="true">


                  var util = new FISCiRelUtil();


                  var locationInfo = util.getLocationInfo(current);// returns an object described above


                  locationInfo;


        </g2:evaluate>


        <j2:if test="$[jvar_li.length > 0]">


        <div class="navbar navbar-default" role="navigationi">


                <h1 style="display: inline-block;" class="navbar-title">


                        Configuration Item Location - $[current.u_number]


                </h1>


        </div>


        <div>


                      <j2:forEach items="$[jvar_li]" var="jvar_currentCi">


                              <g:macro_invoke macro="fis_cmdb_location_part" body="$[jvar_currentCi]" />


                      </j2:forEach>


        </div>


        </j2:if>


</j:jelly>



Recursive Macrow


<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">  


    <div>


              $[jvar_body.name]


              <j2:forEach items="$[jvar_body.parents]" var="jvar_subCurrentCi">


                            <g:macro_invoke macro="fis_cmdb_location_part" body="$[jvar_subCurrentCi]" />


              </j2:forEach>


      </div>


</j:jelly>



Thanks in advance,


AA