Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Instance or local variables in WebApps

Avatar

Level 1

Hello, 

I would like to know if there is a way to use a local or an instance variable in my WebApp. I want some variables to communicate between different activities in my webApp with no access or read only access from the client side. I'm already using the context variables (ctx.vars) but those are Global and are exposed to the public and can be easily changed from the browser (These variables are used server side only and there is no need for them to be displayed elsewhere) .

Thank you.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @dhfw ,

I have one little (hack) trick to achieve what you want, it is not official and could stop working anytime (works for years). Also works (for years) in workflows. This way I do not have to stringify objects but move then between activities and access them as objects everywhere else:

How to?

  • Add java script library to your web app

Marcel_Szimonisz_0-1669541456582.png

  • JS Script library contains simple object declaration. I called it DL like data layer don't ask me why 🙂 
 var DL:{}

 

  • Lets move variable between Script and Script 2.

Marcel_Szimonisz_1-1669541602645.png

 

  • define your variable in Script activity

Marcel_Szimonisz_3-1669541772873.png

  • read and display in Script 2

Marcel_Szimonisz_4-1669541795120.png

 

  • result

Marcel_Szimonisz_5-1669541898461.png


Also it works in page activity and in every other activity that uses JSSP inside web app

<%=DL.variable%>

Marcel_Szimonisz_6-1669542420561.png

 

 

Marcel Szimonisz

MarTech Consultant
for more tips visit my blog
https://www.martechnotes.com/

View solution in original post

11 Replies

Avatar

Level 3

Hi @dhfw 

 

To avoid SQL injections, use bound parameters with $(). 

Eg. 

sqlGetInt( "select iRecipientId from NmsRecipient where sEmail = $(sz) limit 1", request.getParameter('email'));

sqlExec("UPDATE "+vars.tableName+" SET sEmail=$(sz) WHERE iId=$(l)", cleanedEmail, record.$id);

For more information click.

Also there's a Blog which you can go through.

Documentation of binding the parameter.

 

Also the variables that is defined in the variables tab under properties of WebApps, flows throughout the WebApp and can be used anytime in any of the activity.

 

Regards

Avatar

Level 1

Hi @AkshayAnand 

 

Thank you for your response but this is not really what I need. What I need is an instance variables similar to the variables used in workflows  like described Here to send data between different activities like a JS activity and a Test activity.

instance.vars.foo = "bar1"
vars.foo = "bar2"
task.vars.foo = "bar3"

I am aware of there variables tab under properties of WebApps but those variables are in the Global context. As I mentioned above, i'm already using the global ctx.vars to communicate between the client side and the server side of the WebApp. What I need is a secure way to communicate between different activities server side exclusively.

I hope my description was clearer this time. 

 

Thank you.

Regards.

Avatar

Level 3

Hi @dhfw 

 

Thank you for your explanation. As mentioned in my previous comment, you can use bound parameters with $() for secure transfer as instance variables can't be used. (instance.vars.x or vars.x will throw undefined error).

Repasting the links :-

Documentation of binding the parameter.

For more information click.

Also there's a Blog which you can go through.

 

Regards

Avatar

Level 1

Hi @AkshayAnand 

Thank again for your response but I still fail to see how I can use bound parameters to communicate variables between two activities. These variables are server side only and are in no way parameters sent from the client side. They are not even inserted into the database, there is no SQL (update, select, insert) involved in my use case.

I'm aware that instance variables can't be used, that's why I was asking for something similar or a turnaround.

 

Thank you once again.

Regards.

Avatar

Correct answer by
Community Advisor

Hello @dhfw ,

I have one little (hack) trick to achieve what you want, it is not official and could stop working anytime (works for years). Also works (for years) in workflows. This way I do not have to stringify objects but move then between activities and access them as objects everywhere else:

How to?

  • Add java script library to your web app

Marcel_Szimonisz_0-1669541456582.png

  • JS Script library contains simple object declaration. I called it DL like data layer don't ask me why 🙂 
 var DL:{}

 

  • Lets move variable between Script and Script 2.

Marcel_Szimonisz_1-1669541602645.png

 

  • define your variable in Script activity

Marcel_Szimonisz_3-1669541772873.png

  • read and display in Script 2

Marcel_Szimonisz_4-1669541795120.png

 

  • result

Marcel_Szimonisz_5-1669541898461.png


Also it works in page activity and in every other activity that uses JSSP inside web app

<%=DL.variable%>

Marcel_Szimonisz_6-1669542420561.png

 

 

Marcel Szimonisz

MarTech Consultant
for more tips visit my blog
https://www.martechnotes.com/

Avatar

Level 1

Thank you @Marcel_Szimonisz, this seems like the closest solution to what I need. 

Do you I think I can create multiple objects with a randomly generated name for each user instance to avoid conflicts and destroy these objects at the end of my webApp workflow as to not overload the server with useless variables ? 

Avatar

Community Advisor

Hello @dhfw.

sure you can create as many variables, and objects (arrays what not) this way. Give it a try and let me know how it went 

Marcel

Avatar

Level 1

Hello @Marcel_Szimonisz .

The solution sadly didn't work for my use case 😢.

Passing variables between two activities works perfectly except for when there is an HTML page between them. The application probably re-execute the script when a client side request is made which resets the stored variables. 

 

dhfw_0-1669637360312.png

Result : 

dhfw_1-1669637377916.png

 

 

dhfw_2-1669637413250.png

Result : 

dhfw_3-1669637423853.png

 

EDIT : I can actually make it work by rearranging some activities and declarations, so thank you so much for the help.

 

EDIT 2 :  The test activity of the webApp only recognizes the formContext which is basically "ctx" and I sadly can not find  way to use the variables declared in the JS script library in the test activity. 

generated code : 

dhfw_0-1669646181039.png

 

Avatar

Community Advisor

Hello @dhfw 

Page will "reset" that special variable.. so you need to store to ctx.vars as string and then restore

 

Marcel

Avatar

Community Advisor

Hello @dhfw,
Also are you sure you can change the ctx variables from client side?

  • You can save the special variable to ctx variable before test. Because there is functionality from client to change ctx data is it for all ctx types I assume. 
  • For after page you can save the variables to database and then read them again (create your own schema where you query by webapp name and get variables in json string to restore them after)

 

Marcel