- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2025 07:37 PM
I have a form that has a list collector to accept multiple values. Once form is submitted, these values need to be collected and built into an object as below.
If user selects 2 values from the list collector on form, the 'selection' should have 2values as below. If selected 5, then selection in object must have 5 values as below
How to dynamically push the selected values into 1 object ? Need to achieve this in a server script.
{
"name":"snehal",
"account":"<email>",
"selection":[
{"selectionType":"multiple","choiceValue":"<Value1>","description":"<description field from form>"},
{"selectionType":"multiple","choiceValue":"<Value2>","description":<description field from form>"}
]
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2025 08:40 PM
something like this
// Assume these variables are available from your form or context:
var name = 'snehal';
var account = '<email>';
var selection = []; // This will hold your selection objects
// Get the list collector values (comma-separated sys_ids)
var selectedValues = current.variables.<list_collector_variable>; // e.g., 'sys_id1,sys_id2'
var description = current.variables.<description_variable>; // Adjust as per your form
if (selectedValues) {
var sysIds = selectedValues.split(',');
for (var i = 0; i < sysIds.length; i++) {
var gr = new GlideRecord('<table_name>'); // Replace with the table referenced by the list collector
if (gr.get(sysIds[i])) {
selection.push({
"selectionType": "multiple",
"choiceValue": gr.getDisplayValue(), // Or use another field if needed
"description": description // Or gr.<field> if description is per selection
});
}
}
}
// Build the final object
var resultObject = {
"name": name,
"account": account,
"selection": selection
};
// Now resultObject contains the dynamic array under 'selection'
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2025 08:21 PM
where in JSON object you wish to place that? how the json should look like
share some details, screenshots etc for that list collector
you can iterate that list collector and form json object
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2025 08:29 PM - edited 05-13-2025 08:38 PM
Refer my object sample above.
I want to place under "selection". If 2 values selected from LIST Collector on form, "selection" in the object sample must have 2 sets
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2025 08:40 PM
something like this
// Assume these variables are available from your form or context:
var name = 'snehal';
var account = '<email>';
var selection = []; // This will hold your selection objects
// Get the list collector values (comma-separated sys_ids)
var selectedValues = current.variables.<list_collector_variable>; // e.g., 'sys_id1,sys_id2'
var description = current.variables.<description_variable>; // Adjust as per your form
if (selectedValues) {
var sysIds = selectedValues.split(',');
for (var i = 0; i < sysIds.length; i++) {
var gr = new GlideRecord('<table_name>'); // Replace with the table referenced by the list collector
if (gr.get(sysIds[i])) {
selection.push({
"selectionType": "multiple",
"choiceValue": gr.getDisplayValue(), // Or use another field if needed
"description": description // Or gr.<field> if description is per selection
});
}
}
}
// Build the final object
var resultObject = {
"name": name,
"account": account,
"selection": selection
};
// Now resultObject contains the dynamic array under 'selection'
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2025 02:14 AM
perfect. Thanks Ankur. You rock !