Using Jelly variables on script

Mauricio G Raud
Tera Expert

Hi,

 

I have a requirement to embed a graph on a form, so I created a UI Macro that builds the graph (static) and its displayed properly on the form, but to make this dynamic I need to access the sys_id of the current record.

 

In the macro I have this

 

 

<g2:evaluate var='jvar_sys_id' object="true" jelly="true">
          var sysId = current.getValue('sys_id');
          sysId;
</g2:evaluate>

 

 

and I want to access this variable on the <script> part of the Macro

 

 

<script>
    var data = {
            labels: [${jvar_sys_id}, 'Risk', 'Priority'],
            values: [100, 1, 1]
    };

    drawReport(data);
</script>

 

 

it might be a simple thing that I'm missing but haven't figured it out.

I've read multiple community articles and so far I'm lost with this, can you experts help me with this?. How do I access that variable within <script></script>, or more variables in the future like this one?.

 

Thanks in advance!

 

1 ACCEPTED SOLUTION

Allen Andreas
Administrator
Administrator

Hi,

In the script, you can access variables set by g2:evaluate like so:

$[jvar_sys_id]

Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

View solution in original post

4 REPLIES 4

Allen Andreas
Administrator
Administrator

Hi,

In the script, you can access variables set by g2:evaluate like so:

$[jvar_sys_id]

Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Mauricio G Raud
Tera Expert

Hi Allen,

thank you for your response, but that didn't work. I still can't access the value on the script, in fact the Macro returns blank (it breaks). If this helps, here is my UI Macro

<?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_sys_id' object="true" jelly="true">
          var sysId = current.getValue('sys_id');
          sysId;
	</g2:evaluate>
	<g2:evaluate var='jvar_number' object="true" jelly="true">
          var numb = current.getValue('number');
          numb;
	</g2:evaluate>

    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

    <canvas id="reportArea" width="400" height="400"></canvas>

    <script>
        // Some function that draws the report, it works perfectly for static values
        function drawReport(data) { 
            ... 
                data: {
                    labels: data.labels,        // Data from g2:evaluate is used here
                    datasets: [{...}],
            ...
        } // end of drawReport

        // set labels as jvar_number from g2:evaluate
        var data = {
            labels: [$[jvar_number], 'Label 1', 'Label 2', 'Label 3', 'Label 4', 'Label 5'],
            values: [100, 25, 75, 25, 50, 75]
        };

        drawReport(data);
    </script>

 

 

Mauricio G Raud
Tera Expert

Nevermind, I saw the data on console and it was fine, I forgot the quotation marks on $[jvar_number], so I changed

labels: [$[jvar_number], 'Label 1', 'Label 2', 'Label 3', 'Label 4', 'Label 5'],

to

labels: ["$[jvar_number]", 'Label 1', 'Label 2', 'Label 3', 'Label 4', 'Label 5'],

I feel stupid =S

 

your reply helped @Allen Andreas ! Thank you very much!

No problem! Have a great weekend!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!