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

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>


samadam
Kilo Sage

Thanks a lot. Got it working with your suggestions.


Shannon Burns
Kilo Sage

Chuck,

I know you're a super busy guy, but I feel like this is related in that I just don't know how to set/access the variable properly.  If you have a minute can you give me some guidance on what I'm trying to do here?

I'm trying to pull in a value from my UI Action - stored via this code
dialog.setPreference('ei_sys_id', sys_id);

Into my UI Page - I have tried using multiple variations of g:evaluate an j:set to get the sys_id to use to query the table and build the array I need to print and nothing is working - but then again I am blindly stabbing in the dark, because I know very little about jelly.

Essentially what I want to is take that value from the UI action above and store it in a variable that would be referenced where <insertvariablehere> is in the snippet below.

<g:evaluate var="jvar_forecasts" object="true" jelly="true">
var arrForecasts = [];
var grForecasts = new GlideRecord("u_ehs_forecasts");
grForecasts.addEncodedQuery('u_forecast_parent=' + <insertvariablehere>)};
grForecasts.query();

 

Thanks,
Shannon

I found several examples of getPreference in UI pages. It appears to be accessed in JavaScript like this (from the catalog_find UI page...

   <g:evaluate var="jvar_theme">
		var theme = gs.getPreference('glide.css.theme.ui16');
		if (null == theme){
			var company = gs.getUser().getCompanyRecord();
			if (null != company)
				theme = company.getValue("theme");
		}
		theme;
   </g:evaluate>