<g:evaluate> tag

uma17
Tera Guru

Hi All,

I am new to <g:evaluate> tag, I want the Logged in user name to be displayed for which I wrote the below code but it's not working.Can anyone help me in correcting the below code.

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

        var gr = new GlideRecord('sys_user');

        gr.addEncodedQuery('name='+gs.getUserID());

        gr.query();

      gr;

  </g:evaluate>

  <div class="myprofile"> <img class="" src="icon.png" alt=""/>

                <h4>${jvar_log_user.getDisplayValue('user_name')}</h4>

                <h4>${jvar_log_user.getValue('user_name')}</h4>

Thanks,

Uma

1 ACCEPTED SOLUTION

Joe McCarty1
ServiceNow Employee
ServiceNow Employee

getUserID returns the user's sys_id rather than name as in your encoded query and you need to advance the GlideRecord using next()...Try this:



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


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


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


        var gr = new GlideRecord('sys_user');


        gr.addEncodedQuery('sys_id='+gs.getUserID());


        gr.query();


  gr.next();


      gr;


  </g:evaluate>


  <div class="myprofile"> <img class="" src="icon.png" alt=""/>


        <h4>${jvar_log_user.getDisplayValue('user_name')}</h4>


    <h4>${jvar_log_user.getValue('user_name')}</h4></div>


</j:jelly>


View solution in original post

4 REPLIES 4

Joe McCarty1
ServiceNow Employee
ServiceNow Employee

getUserID returns the user's sys_id rather than name as in your encoded query and you need to advance the GlideRecord using next()...Try this:



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


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


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


        var gr = new GlideRecord('sys_user');


        gr.addEncodedQuery('sys_id='+gs.getUserID());


        gr.query();


  gr.next();


      gr;


  </g:evaluate>


  <div class="myprofile"> <img class="" src="icon.png" alt=""/>


        <h4>${jvar_log_user.getDisplayValue('user_name')}</h4>


    <h4>${jvar_log_user.getValue('user_name')}</h4></div>


</j:jelly>


tomleg
Giga Contributor

Further on Joe's response, you can even shorten your query to look like (note my comments in bold):



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


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


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


        var gr = new GlideRecord('sys_user');


        gr.get(gs.getUserID()); // you can do this as you are only querying against sys_id


        gr;


  </g:evaluate>


  <div class="myprofile"> <img class="" src="icon.png" alt=""/>


    <h4>${jvar_log_user.getValue('user_name')}</h4></div>     <!-- You don't need getDisplayValue as user_name field is a string field -->


</j:jelly>


Hi Tom,




Do we have to put this script in processing section or HTML?


Hi Shreya,



That whole script needs to be put into the HTML section. Let me know if there is any other question.