Jelly Variable value disappearing

Li8
Tera Contributor

Hi All,

 

Could really do with some help working out what's happening to the value of my jelly variable.

My Example below is a dynamic content block on a dashboard. The content block talks to a script include with an ajax call.

The ajax call only returns a string.

The issue i have is the variable jvar_output3 returns the string correctly in the "alert" and pops on screen but when i try to assign it to the "j set" command on the very next line. Nothing seems to pass through.

I've also tried many different ways to pass the variable at the end of the script. 

It's probably something simple but i think i'm loosing the scope of the variable after it leaves the "<script>" part of the code.

Code below should work on any instance and has been simplified for my working example.

 

Thanks in advance 🙂

 

This is the dynamic content block

-------------------------------------------------------------------------------------------

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

<!-- OPEN MENU SECTION TABLE -->
<div>
<button type="button" onclick="runCode()">Refresh View</button><div></div>
- Element Name: ${jvar_id} <div></div>
- Result: ${jvar_variable1}
</div>

<script>
function runCode() {
var ga = new GlideAjax('project_Utils');
ga.addParam('sysparm_name','showMyURL');
ga.getXMLWait();
jvar_result1=ga.getAnswer();
return jvar_result1;
}

<j:set var="jvar_output2" value="this string" />
jvar_output3 = runCode();
alert(jvar_output3);
<!-- My Issue is here with jvar_output3-->
<j:set var="jvar_variable1" value="${jvar_output3}" />
<j:set var="jvar_variable2" value="variable2" />
</script>

<g:evaluate var="jvar_id" jelly="true">
jvar_var1 = "variable1";
jvar_var1;

</g:evaluate>

<div>
<button type="button" onclick="runCode()">Rerun function</button><div></div>
- Element Name ID: ${jvar_id} <div></div>
- Element Name var1: ${jvar_variable1} <div></div>
- Element Name var2: ${jvar_variable2} <div></div>
- Element Name result1: ${jvar_result1} <div></div>
- Element Name jvaroutput: ${jvar_output3}
</div>
<g:breakpoint var="jvar_id" />

</j:jelly>

 

------------------------------------------------------------------------------------------------------------------------

 

This is the client callable script include ( simplified for the example )

 

var project_Utils = Class.create();
project_Utils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

showMyURL : function(){

return "ScriptIncludeReturnString";

},

type: 'project_Utils'
});

 

------------------------------------------------------------------------------------------------------------------------

 

 

 

2 REPLIES 2

jaheerhattiwale
Mega Sage
Mega Sage

@Li8 Its not possible to get the variable value out of script but there is a best way to achieve it without using GlideAjax.

 

Add g:evaluate tag on top of the code which should run the code in the script include which is called by GlideAjax then its very easy to use the variable in g:evaluate in j:set.

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

-O-
Kilo Patron
Kilo Patron

Elements of the form <j:* />, <j2:* />, <g:* />, or <g2:* /> enclose/denote stuff that is interpreted/executed only servers side only when the page is generated. The same in case of constructs ${} and $[]. Therefore there can be no "communication" between that stuff and stuff in <script /> nodes - these being executed client side in the browser after it has been generated on server side and after it hes been loaded by the browser.

So if we would break up your jelly into what is executed on server side during generation, we would get:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
	<!-- OPEN MENU SECTION TABLE -->
	${jvar_id}
	${jvar_variable1}

		<j:set var="jvar_output2" value="this string" />
		<j:set var="jvar_variable1" value="${jvar_output3}" />
		<j:set var="jvar_variable2" value = "variable2" />

	<g:evaluate var="jvar_id" jelly="true">
		jvar_var1 = "variable1";
		jvar_var1;
	</g:evaluate>

		${jvar_id}
		${jvar_variable1}
		${jvar_variable2}
		${jvar_result1}
		${jvar_output3}

	<g:breakpoint var="jvar_id" />
</j:jelly>

And executing all that results in a page that has source:

<div>
	<button type="button" onclick="runCode()">Refresh View</button>
	<div></div>
	- Element Name: <nothing, cause jvar_id is not set when this is generated>
	<div></div>
	- Result: <nothing, cause jvar_variable1 is not set when this is generated>
</div>
<script>
	function runCode() {
		var ga = new GlideAjax('project_Utils');
		ga.addParam('sysparm_name', 'showMyURL');
		ga.getXMLWait();
		jvar_result1 = ga.getAnswer();
		return jvar_result1;
	}

	jvar_output3 = runCode();
		
	alert(jvar_output3);
</script>
<div>
	<button type="button" onclick="runCode()">Rerun function</button>
	<div></div>
	- Element Name ID: variable1
	<div></div>
	- Element Name var1: <nothing because jvar_variable1 was set, but was set to jvar_output3 which was not defined server side>
	<div></div>
	- Element Name var2: variable2 <div></div>
	- Element Name result1: <nothing because jvar_result1 was not defined server side>
	<div></div>
	- Element Name jvaroutput: <nothing because jvar_output3 was not defined server side>
</div>

In the end Jelly is like PHP: it generates HTML code. Just like in PHP you cannot communicate with the original PHP code once the page has been generated, sent to and loaded into the browser, one cannot communicate with Jelly code once the page has been generated, sent to and loaded into the browser. And vice versa.