Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

if condition in UI page jelly script

samadam
Kilo Sage

I have two variables, one being set from syspam value and other from a query. I want to use them in a query based on if there is a value passed as parameter or if null use the value set in the g:evaluate.

This only works if syspam variable is populated. Using the test condition the value gets set but in the query it doesn't work. I tried to use "jvar_minc" insated of '${jvar_minc}' but this only works for one condition. How can I get this working. Is there a way to have if condition in g:evaluate?

<g:evaluate jelly="true">

  <j:set var="jvar_inc" value="${RP.getParameterValue('sysparm_inc')}" />

  </g:evaluate>

<g:evaluate jelly="true">

  var currentval = new GlideRecord('u_temp'); .addEncodedQuery('u_start_date&lt;=javascript:gs.daysAgoEnd(0)^u_end_date&gt;=javascript:gs.daysAgoStart(0)');

currntval.query();

currentval.next();

  var jvar_minc = currentval.q_val;

  </g:evaluate>

<j:if test="${!empty(jvar_inc)}">

  <j:set var="jvar_minc" value="${jvar_inc}" />

  </j:if>

  <g:evaluate jelly="true">

  var inc = new GlideRecord('u_temp_table');

  inc.addQuery('u_inc_name', '${jvar_minc}');

  inc.addQuery('u_id', '${jvar_id}');

  inc.query();

  </g:evaluate>

when i print the values they come up right, its in the query it doesn't do the right evaluation.

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Sam,



You don't need a g:evaluate tag around the first j:set. g:evaluate is for running JavaScript. Also, when you use jelly="true", it says "I'm going to use a copy of the jelly variables", but you're not using them. Best practice is to avoid using ${variable} calls inside g:evaluate tags. You're half way there with jelly="true", but didn't use them. Finally, you had a hanging '.addEncodedQuery()" call with no object. Where did jvar_id come from? It is used in the second g:evaluate statement, but I don't see it set anywhere. Here's a cleaned up version of your script.



  <j:set var="jvar_inc" value="${RP.getParameterValue('sysparm_inc')}" />


<g:evaluate var="jvar_minc" jelly="true">


  var currentval = new GlideRecord('u_temp');


  currentval.addEncodedQuery('u_start_date&lt;=javascript:gs.daysAgoEnd(0)^u_end_date&gt;=javascript:gs.daysAgoStart(0)');


  currntval.query();


  currentval.next();


  currentval.q_val;


  </g:evaluate>


<j:if test="${!empty(jvar_inc)}">


  <j:set var="jvar_minc" value="${jvar_inc}" />


  </j:if>



  <g:evaluate jelly="true">


  var inc = new GlideRecord('u_temp_table');


  inc.addQuery('u_inc_name', jelly.var_minc);


  inc.addQuery('u_id', jelly.jvar_id); // What is jvar_id???


  inc.query();


  </g:evaluate>


View solution in original post

11 REPLIES 11

I finally got it to work with this

<g:evaluate var="jvar_forecasts" jelly="true" object="true" >
     var jvar_id = RP.getWindowProperties().get('ei_sys_id');
     gs.log('***jvar_id outside= ' + jvar_id);

     var arrForecasts = [];
     var grForecasts = new GlideRecord('u_ehs_forecasts');
     grForecasts.addQuery('u_forecast_parent', jvar_id);
     grForecasts.query();

....

</g:evaluate>

 

Thanks for your time,
Shannon

can you please tell me how to use between, >,< operators in g:evluate block using glide record.  None of them operators working.  The same operators work fine in server side scripts but not in g:evaluate block.