Passing values from <script> to <j:set> in UI Macro

uma17
Tera Guru

Hi ,

can anyone let me know, how to pass value from script to Jelly in UI Macro

<script>     var name="XYZ"; </script>

<j:set var="jvar_user" value="${name}"/>  

<p>My Name is : ${jvar_user}</p>

I am not able to get the name value in <j:set>

6 REPLIES 6

Jake Gillespie
Mega Guru

Hi Uma,



You can't pass values from the <script> section to the Jelly, because the <script> section contains the javascript code which will run after the page has been loaded. Jelly is rendered into HTML before the page is loaded.



In the example below, the code inside the <g:evaluate....> tag will run a server side glide record query, and return the object as "jvar_gr", which is available to the jelly tags (e.g <j:if....>). In this case, the value returned in an object so you can call the .hasNext() method inside the <j:if...> tag.



Example from Wiki:


<g:evaluate var="jvar_gr" object="true">


  var gr = new GlideRecord("incident");


  gr.addQuery("active", true);


  gr.query();


  gr;


</g:evaluate>



<j:if test="${!jvar_gr.hasNext()}">


  We did not find any active incidents.


</j:if>


<j:if test="${jvar_gr.next()}">


  We found ${jvar_gr.getRowCount()} active incidents.


</j:if>



Here is another example of getting a string value from the server, and accessing it in the client side <script> section:


<g:evaluate jelly="true" object="true">


  var caller_name = '';


  var gr = new GlideRecord("incident");


  gr.addQuery("active", true);


  gr.query();


  if(gr.next()){


  caller_name = gr.caller_id.name;   // get name of caller and copy to caller_name variable


  }


</g:evaluate>



<script>>


  var js_string = ${caller_name}; // access the caller_name variable from the <g:evaluate> section


</script>>



In the above example, when the page is rendered on the server, the caller's name from the record in the glide query will be copied to a variable called "caller_name", and the javascript variable called js_string (in the <script> section) will be initialised with the same value. After the page has loaded, none of the jelly (<g:evaluate> or <j:if...>) tags will be available as these are rendered/processed before the HTML is sent to the client. The js_string variable would simply appear as if you had assigned the actual value in your <script> section.



For more information, please see the Jelly Script section on the Wiki.



Regards,


Jake


Thank you Jake,



But I want to know, is there a way were I can get the value from Catalog item variable to UI Macro in <g:evaluate> tag


May i know where u created macro either in catalog item or request submission form(RITM).


I have created a macro in Catalog item