Jelly: Glide Evaluate Tag Variable

jancaasi
Mega Expert

Hi Everyone,

I have a bit of a problem with passing variables in the evaluate tag. Below is the script:

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

        var gr = new GlideRecord('xxxx_database_view');

        gr.query();

        gr;

  </g:evaluate>

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

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

          <tr>

                <td>${jvar_gr.getValue('xxx_employee')}</td>

                <td>${jvar_gr.getValue('xxx_dateinput')}</td>

                <td>${jvar_gr.getValue('xxx_hours')}</td>

                <td>${jvar_gr.getValue('xxx_activity')}</td>

                <td>${jvar_gr.getValue('xxx_description')}</td>

                <td>${jvar_gr.getValue('yyy_team')}</td>

          </tr>

          </j:while>

  </j:if>

If I don't use the var="jvar_gr" in the evaluate tag and just continue on with the glide record script that I usually use, it works. Its returning the records that I want to return.

I tried adding an alert script after line 6 and the alert script is working. But when I tried putting the alert script between line 7 and 8, the alert script isn't working and I'm guessing that there's something wrong with the variable jvar_gr. Can someone tell me what's wrong with my code?

My reference: Jelly Tags - ServiceNow Wiki

1 ACCEPTED SOLUTION

Hi Jan,



Sure, no problem.



The variable you set to store the new gliderecord, is the name you should set in the g:evaluate tag. In your case your set the var as gr; it's recommended to use something logical, like I did with the table name, to avoid confusion.



I would write your code like the following:



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


  var incident = new GlideRecord("incident"); // mark this the same name as your g_evaluate variable name


  incident.addQuery("active", "true");


  incident.query();


  incident; // this is the variable put into the variable jvar_gr


</g:evaluate>



The jvar prefix, is in the wiki; but I believe that's for back-end queries, rather than front-end. I suspect it doesn't always mix well. I have never had it work, tbf. The way I described is how it always works for me. Not declaring the variable can and will lead to some odd results, so I do recommend declaring it.



I keep hitting submit, before I'm ready to answer fully, apologies. Its 2am here.


View solution in original post

7 REPLIES 7

meia_ahaesy
Mega Expert

You don't need to set a "jvar_" variable prefix from my experience when you're returning the object in the g:evaluate; but it does have to match the gliderecord variable. Also, it's redundant to have the <j:if> to check if there's a next when you have a <j:while> there. The while won't loop if there are no records.



Here's an example of mine:



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


              var category = new GlideRecord('u_category_table');


                        category.addQuery('u_category_type', 'sample');


                        category.addQuery('u_category_desc', 'DESCRIPTION1');


                        category.addQuery('u_category_display', true);


                        category.query();


</g:evaluate>


Hi Meia,



Thanks for the response. Can you elaborate a bit about it does have to match the gliderecord variable? Base on the example below from the wiki, I think that as long as you declare the variable gr in the last line before the end of evaluate tag you can actually pass the variable inside the evaluate tag to the variable that you have declared in the evaluate tag.



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


  var gr = new GlideRecord("incident");


  gr.addQuery("active", "true");


  gr.query();


  gr; // this is the variable put into the variable jvar_gr


</g:evaluate>



I'm also planning to drop declaring the variable in my code since it will work anyway's even if I don't use it. The only reason why I declared a variable in the evaluate tag is for learning purposes.


Hi Jan,



Sure, no problem.



The variable you set to store the new gliderecord, is the name you should set in the g:evaluate tag. In your case your set the var as gr; it's recommended to use something logical, like I did with the table name, to avoid confusion.



I would write your code like the following:



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


  var incident = new GlideRecord("incident"); // mark this the same name as your g_evaluate variable name


  incident.addQuery("active", "true");


  incident.query();


  incident; // this is the variable put into the variable jvar_gr


</g:evaluate>



The jvar prefix, is in the wiki; but I believe that's for back-end queries, rather than front-end. I suspect it doesn't always mix well. I have never had it work, tbf. The way I described is how it always works for me. Not declaring the variable can and will lead to some odd results, so I do recommend declaring it.



I keep hitting submit, before I'm ready to answer fully, apologies. Its 2am here.


AMAZING!!! It worked!!



I also made a few test. First, I used the variable jvar_gr as a name of the variable. It didn't worked. Then I used myRecord as the variable name in the evaluate tag and everything is working fine now. I wonder if jvar_gr is a reserved word or you can't put underscores in the variable name inside the evaluate tag? Anyway's, many thanks a lot. This is a new experience for me.