Skip to main content

Session values in Intrexx

· 4 min read
Cosmin Valeanu
Low Code Developer

When creating applications using Intrexx, you will likely need to store and read values specific to that particular user's session. The most important aspect of session values is that they can be accessed from any page of your Intrexx portal.

Intrexx already uses session values to determine different aspects of a page. For example, the language is a session value stored by the system and is used to determine which language should be displayed on that application instance.

Reading values from the session in controls is straightforward. Select your control, open the Binding tab, and select Session. After that, you can type the name of the variable that you want to use from the session. You can do the same for default values!

Control element details

Velocity Template Language (VTL)

In VTL, you can store and read session variables using the object $Session. This object has multiple methods that allow you to interact with the current user’s session. For our example, we will insert the following code into static text for programming:

Static text for programming dialog

In this example, we display the initial value of our session variable (an empty string), and if our session variable is empty, we change it to show the string “Now it has changed”. Notice that we are not only restricted to strings and can store other data types (like arrays) in a session variable.

You can find the documentation of the $Session object here.

JavaScript

In Javascript, we cannot directly read and write values from the session. This means we must call a VTL script (or Groovy) to do that instead. We can create the following VTL script to write a value in the current session:

#set($strVarName = $!Request.get(“fr_varName”))
#set($objValue = $!Request.get(“fr_varValue”))

#if($strVarName)
$Session.put($strVarName, $objValue)
#end

In this script, we get the variable name and value as request parameters, and we store them in the current session. Remember, since our value comes from a request, it will always be of String type.

In the JavaScript editor, we can use the following function to get our variable name and a control GUID and write this value as a session variable. Example:

//function to store a value in user session
function saveSessVar(name, valueFieldGUID){

var value = getElement(valueFieldGUID) ? Browser.getValue(getElement(valueFieldGUID)) : null ;
var el = getElement(valueFieldGUID);

ajaxVTL("internal/application/resource/<GUID OF YOUR APP>/setSessionVar.vm", {
dataType: 'text',
data: {
fr_varName: name,
fr_varValue: value,
},
success: function (data) {
},
//async: false,
ixWaitAnim: {
element: el,
type: "overlay"
}
}, el);

return true;
}

To read the value of a session variable in JavaScript, you can use the approach shown in the first two chapters of the article.

Groovy

Like the VTL scripts, we can also read and write session values in Groovy scripts. In this case, we can use the object g_session, which is similar to the object $Session from VTL. Example:

response.json()

def varName = g_request.get(“fr_varName”)
def varValue = g_request.get(“fr_varValue”)

g_sesion.put(varName, varValue)

def mySessVar = g_session.get(varName)

writeJSON(“mySessVar”: mySessVar)

In this example, we get the name and value as request parameters, we store them in the current session. Of course, we must use a JavaScript function to send the parameters to our Groovy script. For example:

function saveSessVarGroovy(name, valueFieldGUID){

var value = getElement(valueFieldGUID) ? Browser.getValue(getElement(valueFieldGUID)) : null ;
const el = getElement(valueFieldGUID);

ix.api.request.ajax().requestAppGroovy("saveSessVarGroovy.groovy", {
dataType: "json",
data: {
"fr_varName": name,
"fr_varValue": value,
},
success: function (data) {
Browser.setValue(getElement(valueFieldGUID), data["mySessVar"]);
},
ixWaitAnim: {
element: el,
type: "overlay"
}
}, el);

return true;
}

Finally, we can call this function to get the value of a control and our desired name and call the Groovy script to store it in the session.

Conclusion

Session values can be used for various purposes, and Intrexx offers many ways to manipulate them. There is no wrong and right approach as they all have pros and cons. Most of the time, you must adjust based on the complexity of the workflow you are trying to implement in your Intrexx application.