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.

How to use conditions in Jelly?

nachosg
Giga Expert

Hello,

I am developing an UI page using Jelly. I am struggling with this because I am not being able to understand how the conditions work.

In summary, I need to show a message depending on a condition (I need a function to retrieve an array and if this array is empty or not, I will show one message or another). I have been testing and I have not been able to do it.

This is my code:

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

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

    <g2:evaluate var="jvar_reasons" object="true">

     

        var reasons = new ReasonManager().retrieveReasons();

        reasons;

</g2:evaluate>

<g2:evaluate var="jvar_no_error" object="true" jelly ="true">

     

        var reasons = jelly.jvar_reasons;

        var length = reasons.length;

        var result = length > 0;

        result;

</g2:evaluate>

  <j:set var="jvar_test" value="$[result]"/>

    <j:if test="true">

         1

    </j:if>

    <j:if test="$[result]">

        2

    </j:if>

    <j:if test="${jvar_test}">

        3

    </j:if>

    ${jvar_test}

    $[jvar_no_error]
</j:jelly>



The only number that is being showed is the "1". But, it's very strange because when I show the variables "jvar_test" and "jvar_no_error", both are true. I would really appreciate any help with this issue.

Thanks.

1 ACCEPTED SOLUTION

I didn't need to use the "toString()", only with j2 I have achieved to show the numbers.

The code :

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

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

    <g2:evaluate var="jvar_reasons" object="true">
        var reasons = new ReasonManager().retrieveReasons();
        reasons;
    </g2:evaluate>

    <g2:evaluate var="jvar_no_error" object="true" jelly ="true">
        var reasons = jelly.jvar_reasons;
        var length = reasons.length;
        var result = length > 0;
        result;
    </g2:evaluate>

    <j2:if test="$[jvar_no_error]">
        2
    </j2:if>

</j:jelly>

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

try to use toString() to compare

Also use j2 since you are using phase 2

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

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

    <g2:evaluate var="jvar_reasons" object="true">
        var reasons = new ReasonManager().retrieveReasons();
        reasons;
    </g2:evaluate>

    <g2:evaluate var="jvar_no_error" object="true" jelly ="true">
        var reasons = jelly.jvar_reasons;
        var length = reasons.length;
        var result = length > 0;
        result;
    </g2:evaluate>

    <j2:if test="$[jvar_no_error.toString() == 'true]">
        2
    </j2:if>

</j:jelly>

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

I didn't need to use the "toString()", only with j2 I have achieved to show the numbers.

The code :

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

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

    <g2:evaluate var="jvar_reasons" object="true">
        var reasons = new ReasonManager().retrieveReasons();
        reasons;
    </g2:evaluate>

    <g2:evaluate var="jvar_no_error" object="true" jelly ="true">
        var reasons = jelly.jvar_reasons;
        var length = reasons.length;
        var result = length > 0;
        result;
    </g2:evaluate>

    <j2:if test="$[jvar_no_error]">
        2
    </j2:if>

</j:jelly>

@nachosg 

Did you mistakenly marked your own response as correct?

I also mentioned the same to use j2 tag

Please mark appropriate response as correct.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader