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 set multiple variable value in jelly script and pass its value to javascript?

ServiceNow SA
Kilo Guru

Hi Team,

I need to know how can i pass multiple values in jelly variables and then add them in query using glide record.

Currently it is overriding the values and i am getting only one value.

Here is my code:

<?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 object="true" var="jvar_statements" jelly="true">
var stmt = new GlideRecord('sn_compliance_policy_statement');
var qr = stmt.addJoinQuery('sn_compliance_m2m_policy_policy_statement', 'sys_id', 'content');
qr.addCondition('document', current.sys_id + '');
qr.orderBy('order');
stmt.query();
stmt;
</g:evaluate>

<j:while test="${jvar_statements.next()}">
<p style="font-weight: bold;"> ${jvar_statements.getValue('name')}</p>

<j:set var="jvar_statements1" value="${jvar_statements.getValue('sys_id')}"/>        
</j:while>

<g:evaluate object="true" var="jvar_childstatements" jelly="true">
var pol = new GlideRecord('sn_compliance_policy_statement');
pol.addQuery('parent','${jvar_statements1}');
pol.orderBy('order');
pol.query();
pol;
</g:evaluate>

<j:while test="${jvar_childstatements.next()}">
<a href="sn_compliance_policy_statement.do?sys_id=${jvar_statements.getValue('sys_id')}">${pol.getValue('name')}</a>

</j:while>

</j:jelly>

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

I don't think you can pass value/object from g:evaluate to another g:evaluate

why not query for this table 'sn_compliance_policy_statement' based on the previous query and push the sys_id and name in json object and then iterate over this object using forEach loop;

can you try to update code as below and check whether it works

<?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 object="true" var="jvar_statements" jelly="true">
var arr = [];

var stmt = new GlideRecord('sn_compliance_policy_statement');
var qr = stmt.addJoinQuery('sn_compliance_m2m_policy_policy_statement', 'sys_id', 'content');
qr.addCondition('document', current.sys_id + '');
qr.orderBy('order');
stmt.query();
while(stmt.next()){
var pol = new GlideRecord('sn_compliance_policy_statement');
pol.addQuery('parent',stmt.sys_id);
pol.orderBy('order');
pol.query();
while(pol.next()){
var jsonObj = {};
jsonObj.sys_id = stmt.sys_id.toString();
jsonObj.name = stmt.name.toString();
arr.push(jsonObj);
}
}
arr;
</g:evaluate>

<j:forEach items="${jvar_statements}" var="jvar_json">                             

<a href="sn_compliance_policy_statement.do?sys_id=${jvar_json.sys_id}">${jvar_json.name}</a>
				
</j:forEach> 

</j:jelly>

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

I don't think you can pass value/object from g:evaluate to another g:evaluate

why not query for this table 'sn_compliance_policy_statement' based on the previous query and push the sys_id and name in json object and then iterate over this object using forEach loop;

can you try to update code as below and check whether it works

<?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 object="true" var="jvar_statements" jelly="true">
var arr = [];

var stmt = new GlideRecord('sn_compliance_policy_statement');
var qr = stmt.addJoinQuery('sn_compliance_m2m_policy_policy_statement', 'sys_id', 'content');
qr.addCondition('document', current.sys_id + '');
qr.orderBy('order');
stmt.query();
while(stmt.next()){
var pol = new GlideRecord('sn_compliance_policy_statement');
pol.addQuery('parent',stmt.sys_id);
pol.orderBy('order');
pol.query();
while(pol.next()){
var jsonObj = {};
jsonObj.sys_id = stmt.sys_id.toString();
jsonObj.name = stmt.name.toString();
arr.push(jsonObj);
}
}
arr;
</g:evaluate>

<j:forEach items="${jvar_statements}" var="jvar_json">                             

<a href="sn_compliance_policy_statement.do?sys_id=${jvar_json.sys_id}">${jvar_json.name}</a>
				
</j:forEach> 

</j:jelly>

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Thanks Ankur. But what it is doing with this code is it is displaying the link to parent policy statement.

However i got it working by querying inside the <j:while test="${jvar_statements.next()}"> and running another while.

Based every parent, it displays the child statement link.

@ServiceNow SA 

Hope you are doing good.

Let me know if I have answered your question.

If so, please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

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

@ServiceNow SA 

Hope you are doing good.

Let me know if I have answered your question.

If so, please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

 

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