Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Comparing two strings in <j2:if>

uma17
Tera Guru

Hi All,

I have no idea about <j2:if > tags and their syntax

I want to compare

$[gs.getUserDisplayName()]

$[sc_request.opened_by.name]

<j2:if test="$[sc_request.opened_by.name] == $[gs.getUserName()]">

//Do Something

</j2:if>

How do I compare these two strings, can any one help me?

Thanks

Uma

1 ACCEPTED SOLUTION

Tanaji Patil
Tera Guru

Hi uma,



There is a mistake in your code. gs.getUserName() returns you the user ID of the current user and not the name of the current user. You should either use -


1) sc_request.opened_by.name == gs.getUserDisplayName();


2) sc_request.opened_by.user_name == gs.getUserName();



I agree with kumaran. Below is the script you can use


<g2:evaluate var="jvar_answer" jelly="true">


var answer = 'false';


var sc_request = new GlideRecord('sc_request');


.


.


sc_request.query();


if(sc_request.next()) {


        if(sc_request.opened_by.name == gs.getUserDisplayName()) {   //sc_request should be GlideRecord Object as shown


                                                                                                                                                                          //Also you can use "sc_request.opened_by == gs.getUserID()"


                  answer = 'true';


        }


}


answer;


</g2:evaluate>




<j2:if test="$[jvar_answer == 'true']">


        //Do what you want to do here


</j2:if>



Please mark the response helpful/correct if it helped you.



Thanks,


Tanaji


View solution in original post

7 REPLIES 7

rob_pastore
ServiceNow Employee

It's been a while since Iv'e done this... Try something like this:



<j2:if test="$[sc_request.opened_by.name == gs.getUserName()]">


Thank's for the reply Robert , but it did not work.


manikorada
ServiceNow Employee

uma,



is sc_request is a gliderecord object you defined?


Use <g:evaluate> tag and use service now 'if' condition