How to get dynamic values from list collector on form into an object in server script ?

Snehal13
Kilo Sage

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>"} 
  
] 

} 

 

1 ACCEPTED SOLUTION

@Snehal13 

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.

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Snehal13 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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

@Snehal13 

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.

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

perfect. Thanks Ankur. You rock !