Hello, I am new to OutSystems and is trying to experiment a little.
When I tried to send and get a list of structure, I could not, as OutSystems JavaScript widget only accepts simple data type.
So is there is anyway to send and receive a list from a JavaScript node?
Well, this is a quite interesting topic @Hossam Ghonim, however, I do not think there is a direct way to use lists with a JavaScript node. But we may use a workaround to send and get lists form JavaScript to OutSystems.
Setting Using Objects
Assuming we have an structure like this, a football player structure:
-- Name-- Age-- Sign Date-- Salary-- Is Retired
We can convert this Structure to an Object and send it to JavaScript node.
However, I do not recommend this method as the converted object will be very complex. Here is a screenshot for the generated object by this method:
Note that this example is for a simple structure not a list of structure. In case of a list, it would be more complicated. So this is not a recommended practice.
Setting Using JsonSerialize
Convert your list to a Json string using JsonSerialize, and send this string as an input to the Js node.
In the Js node, convert the Json string to a JavaScript array using "JSON.parse()" method.
However, you may need to use a "reviver" function to parse the dates in the Structure.
Here you can see that the array of objects were created clear. So I recommend this method.
Getting Using JsonDeseralize
Assuming you have an array of the same object, and want to export it from the Js node, we are going to use "JSON.stringify()" method.
You may need to use "replacer" function to adjust any date or datetime so that it may be parsed by OutSystems. To test it, make sure that you date or datetime string value can be parsed by "TextToDate" and "TextToDatetime" in OutSystems.
Before passing the Json string to JsonDeseralize node, make sure that the Structure matches object coming out of the Js node, and that each attribute has the name of the Json attribute defined in it.
You can find a demo for getting and sending lists and objects to Js node, in the file attached.
Hope this was of help.
Thanks @Mustafa Emad Shaker. Json seralize and deseralize did solve the issue. But I wonder why this functionality was not added by outsystems!!
I did wonder the same, and went to the Ideas' section, but found several persons who posted about this issue.
But I think Nuno Reis answered this question below.
Hi @Hossam Ghonim ,
To pass a list into a JS node, first use JSONSerialize to convert it into JSON text and map that into a Text input parameter for the JS node.
Inside the JavaScript node, read it with JSON.parse() .
To return a list from JavaScript, do the reverse: build your list in JS, convert it with JSON.stringify() assign it to a Text output parameter, then use JSONDeserialize to convert that JSON string back into your structure list.
Thanks @Mihai Melencu for sharing this info.
Hello.Everytime that question pops up, the answer is to serialize/deserialize with JSON so it can pass as a string.
https://www.outsystems.com/forums/discussion/55986/list-in-javascript/
https://www.outsystems.com/forums/discussion/68477/how-to-use-a-text-list-as-input-to-javascript/
https://www.outsystems.com/forums/discussion/84491/passing-list-as-input-of-javascripts/
https://www.outsystems.com/forums/discussion/66982/best-method-to-convert-a-list-into-javascript-array/
https://www.outsystems.com/forums/discussion/97518/how-to-output-javascript-as-a-list/
You get the point.
It is possible to do it with some variations, but it will depend in your use case,
As a general rule, OutSystems structures are supposed to be used in OutSystems and JavaScript structures in JavaScript. Data between them should be some configurations or results of operation, not the full dataset.
Thanks Nuno Reis for sharing this info.