Use "if" in Jelly

angela_benway
Kilo Expert

I am very new to Jelly. I am trying to add some text to the kb_article_header. I want to add the approver of that Knowledge article, but not every article will have an approver. This is where I am getting stuck. I only want to show "Approver:" when there is an approver. Right now this is my script and I have tried many different things but the debugger and logs say that it "cannot convert null to an object". Does anybody have any suggestions for what I can change?

 

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

    var gr = new GlideRecord("kb_knowledge");

    gr.addQuery("sys_id", current.sys_id);

    gr.addQuery("u_mop_approver", "!=", '');

    gr.query();

    gr;

</g:evaluate>

 

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

                              <span style="font-weight:bold;padding-left:8px;color:#000000;font-size:14px;">${gs.getMessage("Approver")}:</span><span style="color:#000000;font-size:12px;">$[SP]$[JS,HTML:kb.u_mop_approver.getDisplayValue()]</span>

</j:if>

1 ACCEPTED SOLUTION

Well that is strange.   I guess we will have to stick with the !=:



<j2:if test="$[kb.u_mop_approver != '']">


      <span style="font-weight:bold;padding-left:8px;color:#000000;font-size:14px;">${gs.getMessage("Approver")}:</span>


      <span style="color:#000000;font-size:12px;">$[SP]$[JS,HTML:kb.u_mop_approver.getDisplayValue()]</span>


</j2:if>



I tested a version of this in my instance and it worked.


View solution in original post

8 REPLIES 8

Hi Angela,



Is there an error that you are receiving.   One possibility is that we have to use Phase 2 instead of Phase 1 (all other references to kb are in Phase 2).   Also, we can try a different empty test.   Try this:



<j2:if test="$[!empty(kb.u_mop_approver)]">


      <span style="font-weight:bold;padding-left:8px;color:#000000;font-size:14px;">${gs.getMessage("Approver")}:</span>


      <span style="color:#000000;font-size:12px;">$[SP]$[JS,HTML:kb.u_mop_approver.getDisplayValue()]</span>


</j2:if>


I just tried that. The error I got is below. What you said about Phase 2 holding the "kb" reference makes a lot of sense. Before the errors I got were "Cannot convert null to an object." and "kb" is not defined.



org.mozilla.javascript.EcmaError: "empty" is not defined.
  Caused by error in <refname> at line 1

==> 1: !empty(kb.u_mop_approver)


Well that is strange.   I guess we will have to stick with the !=:



<j2:if test="$[kb.u_mop_approver != '']">


      <span style="font-weight:bold;padding-left:8px;color:#000000;font-size:14px;">${gs.getMessage("Approver")}:</span>


      <span style="color:#000000;font-size:12px;">$[SP]$[JS,HTML:kb.u_mop_approver.getDisplayValue()]</span>


</j2:if>



I tested a version of this in my instance and it worked.


That worked perfectly! Thank you for helping.