- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
I have a custom action that creates a map-like object (as in {'a': 'Alpha', 'b': 'Bravo', 'c': 'Charlie', ...}) with a variable number of elements, which I would like to store in a flow variable and share to subsequent steps. When I try to return the map as an 'Object' output type, it is returned empty. I can return it as a stringified JSON in a String output, but would like to avoid having to convert the map to and from JSON string every time I need to use it. What would the proper way be to return the actual map object?
Here is a simplified code example:
And test results:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago
So answering my own question - I ended up doing what I was trying to avoid. I store stringified JSON in the flow variable and instantiate it as a real JSON object every time I need to use the map. V8 documentation claims that such conversions are *very* efficient; obviously, ServiceNow doesn't use V8, but I can't imagine Rhino being much slower, especially for the relatively flat maps that I am using.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @ILYA STEIN ,
Please try to use below code
function execute(inputs, outputs) {
var map = {
a: 'Alpha',
b: 'Bravo',
c: 'Charlie',
d: 'Delta'
};
var arrayOutput = [];
for (var key in map) {
arrayOutput.push({ key: key, value: map[key] });
}
outputs.results = arrayOutput; // Output type: Array of Objects
outputs.message = map;
}
Please mark my answer correct and helpful if this works for you
Thanks and Regards,
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Are you suggesting that I should return an array of objects rather than an object? I thought about that option, but again, i would like to use the map as is and not have to convert it for each use...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago
So answering my own question - I ended up doing what I was trying to avoid. I store stringified JSON in the flow variable and instantiate it as a real JSON object every time I need to use the map. V8 documentation claims that such conversions are *very* efficient; obviously, ServiceNow doesn't use V8, but I can't imagine Rhino being much slower, especially for the relatively flat maps that I am using.
