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.

access jelly variable inside <script> tag

kjmauriello
Mega Expert

I have a UI Page that retrieves data like this:

<g:evaluate var='jvar_events' object='true'>

  var dt = new Date(2015, 0, 15, 0,0,0,0);

  var gr = new isUtils();

  var events = gr.getArrayOfObjects('74ad1ff3c611227d01d25feac2af603f', dt);

  events;

</g:evaluate>

That executes fine, and jvar_events is an array of objects (I confirmed this with a <j:forEach... loop.

I need to pass jvar_events to a jQuery function inside the <script></script> block.

<script>

      var $j_custom = jQuery.noConflict(true);

      $j_custom(document).ready(function () {

              parm1:test,

              parm2: test,

              parm3: ${jvar_events}

</script>

I've tried ${jvar_events}, '$jvar_events}, neither works.

I've also tried executing this inside the <script> and using events as parm3:

  var dt = new Date(2015, 0, 15, 0,0,0,0);

  var gr = new isUtils();

  var events = gr.getArrayOfObjects('74ad1ff3c611227d01d25feac2af603f', dt);

when I create the same array inside the <script> tags with javascript it works fine, so I must not be accessing the jelly variable properly.

I cannot find any valid Wiki or articles on the subject.

Any assistance would be greatly appreciated.

Kevin

11 REPLIES 11

Hi Kevin,



You should be able to pass the value, but it will be passed as a string. If it's just an array you could just do a join and then a split when referencing it in the client side script.



<g:evaluate var='jvar_events'>


  var dt = new Date(2015, 0, 15, 0,0,0,0);


  var gr = new isUtils();


  var events = gr.getArrayOfObjects('74ad1ff3c611227d01d25feac2af603f', dt);


  events.join();


</g:evaluate>


<script>


  var eventsStr = '${jvar_events}';


  var events = eventsStr.split(',');


      var $j_custom = jQuery.noConflict(true);


      $j_custom(document).ready(function () {


              parm1:test,


              parm2: test,


              parm3: events


      }


</script>


kjmauriello
Mega Expert

I added the following to the script:


      var ev = '${jvar_events}';


      alert('${jvar_events}');


      alert("${jvar_events}");


      alert(ev);


They all display org.mozilla.javascript.NativeArray@........


I was hoping it would return [Object]


I guess I just need to figure out how to pull the data from the array;


You could also JSON encode this so you can pass an object.


How would I JSON encode it ??


That may be my best bet.


Guess I should have posted a link.



I think you should be able to use the following on the server:


var encodedObject = new JSON().encode(object);



Then use the following to decode on the client:


JSONParser - ServiceNow Wiki