The CreatorCon Call for Content is officially open! Get started here.

How to print array and object in jelly Script ???

khanshaheen
Kilo Explorer

Hi Experts,

 

I am trying to print  request and related requested item. Its printing all the request number but not requested item.

Can you please go through the code , and check what is incorrect??

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<h3 style="margin-bottom:20px;">Global Code Search</h3>

<g2:evaluate var="jvar_catArray" object="true">
var catArray =[]; // array to store values
var grcat = new GlideRecord("sc_request");
grcat.addQuery("active", "true");
grcat.query();
var i=0;
while(grcat.next())
{

catArray[i]={};
catArray[i]={number: grcat.getValue('number'), sysID: grcat.getValue('sys_id')};
var req=new GlideRecord('sc_req_item');
req.addQuery("request",catArray[i].sysID);
req.query();
catArray[i].record=[];
var j=0;
while(req.next())
{
catArray[i].record[j]={};
catArray[i].record[j]={rnumber:req.getValue('number'), rsysid:req.getValue('sys_id')};

j=j+1;
}
i=i+1;
}
catArray;
</g2:evaluate>
<j2:forEach var="jvar_value" items="$[catArray]">
<g2:evaluate jelly="true" object="true">

var num = jelly.jvar_value.number;
var sysid = jelly.jvar_value.sysID;
</g2:evaluate>
<li>Request Values are :$[num] and $[sysid]</li>
<j2:forEach var="jvar_reqvalue" items="$[jvar_value.record]">
<g2:evaluate jelly="true" object="true">
var rnum = jelly.jvar_reqvalue.rnumber; //not getting values here.
var rsysid = jelly.jvar_reqvalue.rsysid; //not getting values
</g2:evaluate>
<li> Requested Item Values are :$[rnum] and $[rsysid]</li>

</j2:forEach>
<br/>
</j2:forEach>
</j:jelly>

3 REPLIES 3

khanshaheen
Kilo Explorer

Replace the code with  below 

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<h3 style="margin-bottom:20px;">Global Code Search</h3>

<g2:evaluate var="jvar_catArray" object="true">
var catArray =[]; // array to store values
var grcat = new GlideRecord("sc_request");
grcat.addQuery("active", "true");
grcat.query();
var i=0;
while(grcat.next())
{

catArray[i]={};
catArray[i]={number: grcat.getValue('number'), sysID: grcat.getValue('sys_id')};
var req=new GlideRecord('sc_req_item');
req.addQuery("request",catArray[i].sysID);
req.query();
catArray[i].record=[];
var j=0;
while(req.next())
{
catArray[i].record[j]={};
catArray[i].record[j]={rnumber:req.getValue('number'), rsysid:req.getValue('sys_id')};

j=j+1;
}
i=i+1;
}
catArray;
</g2:evaluate>
<j2:forEach var="jvar_value" items="$[catArray]">
<g2:evaluate jelly="true" object="true">

var num = jelly.jvar_value.number;
var sysid = jelly.jvar_value.sysID;
</g2:evaluate>
<li>Request Values are :$[num] and $[sysid]</li>
<g2:evaluate jelly="true" var="jvar_tableRec" object="true" expression="jelly.jvar_value.record"/>
<j2:forEach var="jvar_reqvalue" items="$[jvar_tableRec]">
<g2:evaluate jelly="true">
var rnum = jelly.jvar_reqvalue.rnumber;
var rsysid = jelly.jvar_reqvalue.rsysid;
</g2:evaluate>
<li> Requested Item Values are :$[rnum] and $[rsysid]</li>

</j2:forEach>
<br/>
</j2:forEach>
</j:jelly>

 

but still didnt get any output:

 

Getting below error :

org.mozilla.javascript.EcmaError: Cannot read property "rnumber" from undefined
Caused by error in Phase 2 Jelly: ftp://gsft_database_form/sys_ui_page.1341d9da4f02230046d3aa2f9310c79c.html.2 at line 1

==> 1: var rnum = jelly.jvar_reqvalue.rnumber;
2: var rsysid = jelly.jvar_reqvalue.rsysid;

 

I found that both versions of your script worked in my PDI without any changes and without errors. 

However, inter-weaving in and out of jelly and the jelly (g) evaluates gets confusing. I do realize that it's necessary at times but with this example (albeit, I don't know all of what is needed) I thought that simplifying the script would make it easier to maintain. Also thinking about this in a manner that only one GlideRecord call is used.

So, although the ritms are not directly on the request record, the request record is referenced on the ritm. In the example script below the request record is accessed via the ritms. And a placeholder is used to help with avoiding duplication since the request record could be attached to multiple ritms. The end result is the same except with less jelly scripting and leveraging javascript to do the object building in one evaluate statement.

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
	<g2:evaluate var="jvar_reqArray" jelly="true" object="true">
		var eq = "request.active=true";
		var ritm = new GlideRecord('sc_req_item');
		ritm.addActiveQuery();
		ritm.addEncodedQuery(eq);
		ritm.query();
		var reqExists = []; //used to keep count of request records
		var reqs = [];  //will eventually hold request and correlating ritms
		var ritms = []; //hold all ritms to start
		while(ritm.next()){
		    //don't duplicate the requests
			if(reqExists.indexOf(ritm.request.getDisplayValue())== -1) {
		        //keeping count of requests in reqExists array
				reqExists.push(ritm.request.getDisplayValue());
		     
		        //for now hold just request objects with empty records array
				reqs.push({number: ritm.request.getDisplayValue(), sys_id: ritm.request.getValue(), records: []});
			}
            // push all ritms with request identifier in ritm array 
			ritms.push({request: ritm.request.getDisplayValue(), record: {number:ritm.getValue('number'), sys_id: ritm.getUniqueValue()}});

		}
		
		// add ritms to correlating requests
		ritms.forEach(function findMatchingReq(ritm){
			reqs.forEach(function addRitmToMatchingReq(req){
				if(req.number == ritm.request){
					req.records.push(ritm.record)
				}
			})
		});
		
		reqs;  //end result is array with request objects and correlating ritms
	</g2:evaluate>
	<j2:forEach var="jvar_req" items="$[jvar_reqArray]">
		<h3>$[jvar_req.number] : $[jvar_req.sys_id]</h3>
		<ul>
			<j2:forEach var="jvar_ritm" items="$[jvar_req.records]">
				<li>$[jvar_ritm.number] : $[jvar_ritm.sys_id]</li>
			</j2:forEach>
		</ul>
	</j2:forEach>
</j:jelly>

Again, I don't know the full requirements but hopefully this helps.

khanshaheen
Kilo Explorer

Yes , above is all also correct .

But Actually i m trying to replicating one big UI page issue in small one , where UI page showing one record from a table and all related records also.

So , i choose to create a UI page where pushing request detail with all requested item.

 

And below code is working as expected now..after doing some minor changes..

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<h3 style="margin-bottom:20px;">Global Code Search</h3>

<g2:evaluate var="jvar_catArray" object="true">
var catArray =[]; // array to store values
var grcat = new GlideRecord("sc_request");
grcat.addQuery("active", "true");
grcat.query();
var i=0;
while(grcat.next())
{

catArray[i]={};
catArray[i]={number: grcat.getValue('number'), sysID: grcat.getValue('sys_id')};
var req=new GlideRecord('sc_req_item');
req.addQuery("request",catArray[i].sysID);
req.query();
catArray[i].record=[];
var j=0;
while(req.next())
{
catArray[i].record[j]={};
catArray[i].record[j]={rnumber:req.getValue('number'), rsysid:req.getValue('sys_id')};

j=j+1;
}
i=i+1;
}
catArray;
</g2:evaluate>
<j2:forEach var="jvar_value" items="$[catArray]">
<g2:evaluate jelly="true" object="true">

var num = jelly.jvar_value.number;
var sysid = jelly.jvar_value.sysID;

</g2:evaluate>
<li>Request Values are :$[num] and $[sysid]</li>
<g2:evaluate jelly="true" var="jvar_tableRec" object="true" >
var tableRec=[];
tableRec=jelly.jvar_value.record;
tableRec;

</g2:evaluate>

<j2:forEach var="jvar_reqvalue" items="$[tableRec]">
<g2:evaluate jelly="true" object="true">
var rnum = jelly.jvar_reqvalue.rnumber;
var rsysid = jelly.jvar_reqvalue.rsysid;
</g2:evaluate>
<li>Requested Item Values are :$[rnum] and $[rsysid]
</li>

</j2:forEach>
<br/>
</j2:forEach>
</j:jelly>