1

Is there a way to get the value of a JS variable into a Velocity variable?

In a page script, I have some JS code and I'd like to just parse an output to a Velocity variable but I can't seem to find a way.

The other way around seems to work though, I can use the $!pageContext.put and .get to read a Velocity variable and use it's value into some JS code.

I was also trying to create a page parameter, and I could use the $pageParameter.VARIABLE.valUE() to read the parameter value, which didn't help me.

What I'm trying to do is, inside a page script, have a variable that stores how many work items ranging from today-7d to today. I thought this should work with something like #set($newSrFilter = $transaction.workItems.search.query("type:changerequest AND created:[$today-7d$ TO $today$]")) but the $today variable doesn't work in page scripts for some reason.

2 Answers2

1

There is no way of doing this, because Velocity is evaluated on the server side, before the Javascript engine even received the script to run. You have to split the process you intend to achieve into several request and response round-trips.

Claude Brisson
  • 4,085
  • 1
  • 22
  • 30
  • Damn ... thanks for the info. Do you have any more details regarding your suggestion (to split the process)? In Velocity I just found that I can use '$date.get(yyymmdd)', which is half my problem resolved, and now I need to do dates additions/subtraction to it. – Julien Llanes Nov 25 '22 at 09:03
  • Ok, did some testing today and managed to make my query work with just Velocity code. I used 2 page parameters, set them to today's date and today - 7d. Then called them via ''$pageParameters.MYPARAMETER.value()''. I then used the ''$date.format('yyyyMMdd', $myDateVar)'' to format the date so it works in the search query. – Julien Llanes Nov 25 '22 at 10:48
0

I will answer this for future me or anyone else needing this.

As @Claude Brisson mentioned, reading Velocity variables directly from JS is impossible.

I was trying to create a query that dynamically selects WIs that were created between a fixed date and today-7days. This is fine via the Polarion widget using Lucene or Velocity, but when I used the $today keyword inside a page script, it didn't like it.

What I ended up doing is:

  • Create a page parameter of type date, set that to today - 7 days ($aWeekAgo)

  • Create the query inside a page script, via:

     #set($releaseOverdueCompleteFilter = $transaction.workItems.search.query("type:changerequest AND NOT HAS_VALUE:resolution AND status:ApprovedComplete2 AND updated:[$aWeekAgo TO 30000000]"))
     #set($releaseOverdueComplete = $releaseOverdueCompleteFilter.size())
     $!pageContext.put("releaseOverdueComplete", $releaseOverdueComplete)
     $releaseOverdueComplete
    

The $pageContext.put() stores the result inside a JS variable

  • Inside JS, I then access those variables via $pageContext.get("releaseOverdueComplete").

This now works ok, but I have to refresh the page (a liveDoc) once everytime I open it, as it seems the JS code is not loading properly (or maybe it loads before the Velocity code or something like that).

I've not tried yet, but I'm thinking maybe the escape ${esc.d} might have worked directly inside a page script, saving me having to create page parameters (something like ${esc.d}today-7d${esc.d}).

  • Just to comment on my last point above, the escape ${esc.d} works fine within a page script, so no need for page parameters. Example of a query: `#set($AcrInsightFilter = $transaction.workItems.search.query("type:changerequest AND created:[${esc.d}today-30d${esc.d} TO ${esc.d}today${esc.d}]")) #set($AcrInsight = $AcrInsightFilter.size()) $!pageContext.put("AcrInsight", $AcrInsight)` – Julien Llanes Nov 30 '22 at 16:15