Pass JavaScript value to OutSystems Variable
Platform Version
10.0.1023.3

Hello !!!!

Before I came here to ask help, I searched a lot here and found some questions regarding my situation (like this: https://www.outsystems.com/forums/discussion/7626/how-do-you-pass-a-javascript-variable-value-into-application-variable/). The problem is that none of the answers helped me. 

What I want?

In the preparation of my screen I have a Javascript action "RunJavaScript" which do:

var passwordEncrypted = CryptoJS.AES.encrypt('"+ValueToEncrypt+"', 'SecretKey');'

I need to pass the value of "passwordEncrypted" to a variable of OutSystems. In my research I read people saying to add a input hidden field on screen and pass the value to it. So I created a local variable "PasswordHidden" and linked to the the hidden input (Name: PasswordHiddenInput).

In the same javascript code which I get the value of "passwordEncrypted" right after it I did:

document.getElementById('"+PasswordHiddenInput.Id+"').value = passwordEncrypted.toString()

But this give me an error, because ".value" is null and it is not allowing to attribute it's value to "PasswordHiddenInput.Id"

Anyone knows how can I pass the value "passwordEncrypted.toString()" to a OutSystems variable?

Thank you very much for your help.

Hi Guilherme,

You should run your Javascript code on the screen in a Javascript block, not in preparation. Because HTML elements are not ready yet in preparation.

You still have two options:

  • You can run your all javascript codes on the screen. (It is not a good way if you need to keep secret key hidden from user)
  • You can use CryptoAPI application from the forge that allows you to encrypt your plain text on the server-side.


I hope it works for you.


Sincerely,

Necmettin

Hi again,

I also want to mention that you can run your Javascript code in a screen action but it needs to be triggered with a button.


Sincerely,

Necmettin

Hey @Necmettin Sahinbay thank you for your answer !

I forgot to mention: the encrypted password is sent to a classic asp code via javascript function "buildPost()" which gets the values of the input hidden fields on the expression of the screen. The screen has only one expression (among other input fields in it) with informations like:

"<input id=""txtPass"" type=""hidden"" name=""txtPass"" value="""+PasswordHidden+""" />

which PasswordHidden should have the value of 

document.getElementById('"+PasswordHiddenInput.Id+"').value = passwordEncrypted.toString()

Do you know how can I do it?

Thank you again for your help.

Please check the attached oml file.

Input is a named to get its real HTML element id and wrapper container is hidden.

There is an expression on the screen that sets inputs value. 

You can see a button to check if the variable is set correctly or not. 


Best regards,

Necmettin

JavascriptPassValueToVariable.oml

Thank you for your answer. I tried here but it doesn't worked yet. I'm not sure what I'm doing wrong here. Look at my print below:

Didn't worked yet to fill the hidden input with the javascript value.

Did you change Escape Content property of expression to False?

Also, you can not pass a value to Outsystems variable via custom HTML input. You have to use Outsystems input. 



Sincerely,
Necmettin

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.