Jelly if conditon

Arkesh Kumar
Giga Guru

Hi,

 

I am new to jelly scripting, required help to add if statement.

we have a custom field in incident table called 'u_record_producer' I want to display variable editor on incident task so I have created below UI macro script but for new incidents created from record producer is working fine but for older ticket, we are facing issue.

 

So I need to add a if condition like if u_record_producer is not empty then display variable editor. Please help

 

<?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:requires name="scripts/js_includes_catalog.js" includes="true"/>
       <g:requires name="styles/${new CatalogCssSelector().getVariableCss()}" includes="true"/>
<g:requires name="scripts/classes/ServiceCatalogForm.js"/>	
<g2:evaluate var="jvar_catalog_item">
function eval_cat_item() {
               var cat_item = "";
var catItemProducedGr = new GlideRecord("sc_item_produced_record");
catItemProducedGr.addQuery("record_key", current.incident);
catItemProducedGr.query();
if (catItemProducedGr.next())
cat_item = catItemProducedGr.getValue("producer");
               return cat_item;
             }
            eval_cat_item();
       </g2:evaluate>
       <g2:evaluate var="jvar_cat_sets" jelly="true">
             var jvar_cat_sets = "";
             var gr = new GlideRecord("io_set_item");
             gr.addQuery("sc_cat_item", jelly.jvar_catalog_item);
             gr.query();
             while (gr.next()) {
                   if (jvar_cat_sets.length > 0)
                         jvar_cat_sets += ",";
                   jvar_cat_sets += gr.variable_set;
             }
             jvar_cat_sets;
       </g2:evaluate>
<div data-sn-macro-sys-id="${jvar_macro_sys_id}">
<g2:evaluate jelly="true"> var recordObject = new GlideRecord('incident');
recordObject.get(current.incident);
</g2:evaluate>
<j2:set var="ref" value="recordObject" />
<j:set var="jvar_producer_target_record" value="true"/>
<g2:client_script type="catalog_item" catalogItem="$[jvar_catalog_item]"/>
<g:inline template="catalog_ui_policy.xml"/>
<g2:render_component componentName="com.glideapp.servicecatalog.DefaultQuestionEditor"/>
</div>

</j:jelly>
2 REPLIES 2

Tim Grindlay
Kilo Sage

Hi, 

I'm no expert in Jelly but I've just spent a few days creating a variable summarizer. Looks like you've copied some of this code from the com_glideapp_questionset_default_question_editor UI Macro and you're missing the following line:

<j2:if test="$[jvar_catalog_item != '']">

 It's on line 38 in the original, then you need to add a matching close tag.

Sai Shravan
Mega Sage

Hi @Arkesh Kumar ,

 

To add the if statement to display the variable editor only when the u_record_producer field is not empty, you can modify the code as follows:

 

After the following line:
<g2:evaluate var="jvar_catalog_item">

Add the following if statement:

<j:if test="${gs.nil(current.u_record_producer) == false}">

 

Below is the updated code you can give try 

<?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:requires name="scripts/js_includes_catalog.js" includes="true"/>
<g:requires name="styles/${new CatalogCssSelector().getVariableCss()}" includes="true"/>
<g:requires name="scripts/classes/ServiceCatalogForm.js"/>
<g2:evaluate var="jvar_catalog_item">
function eval_cat_item() {
var cat_item = "";
var catItemProducedGr = new GlideRecord("sc_item_produced_record");
catItemProducedGr.addQuery("record_key", current.incident);
catItemProducedGr.query();
if (catItemProducedGr.next())
cat_item = catItemProducedGr.getValue("producer");
return cat_item;
}
eval_cat_item();
</g2:evaluate>
<g2:evaluate var="jvar_cat_sets" jelly="true">
var jvar_cat_sets = "";
var gr = new GlideRecord("io_set_item");
gr.addQuery("sc_cat_item", jelly.jvar_catalog_item);
gr.query();
while (gr.next()) {
if (jvar_cat_sets.length > 0)
jvar_cat_sets += ",";
jvar_cat_sets += gr.variable_set;
}
jvar_cat_sets;
</g2:evaluate>

<div data-sn-macro-sys-id="${jvar_macro_sys_id}">
<g2:evaluate jelly="true"> var recordObject = new GlideRecord('incident');
recordObject.get(current.incident);
</g2:evaluate>
<j2:set var="ref" value="recordObject" />
<j:if test="${gs.nil(current.u_record_producer) == false}">
<g2:client_script type="catalog_item" catalogItem="$[jvar_catalog_item]"/>
<g:inline template="catalog_ui_policy.xml"/>
<g2:render_component componentName="com.glideapp.servicecatalog.DefaultQuestionEditor"/>
</j:if>
</div>
</j:jelly>

 

Regards,
Shravan.
Please mark this as helpful and correct answer, if this helps you

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you