Populate related list from catalog variable

Brian DeBlis
Tera Expert

I am looking at populating the requested item --> parent related list from a list collector variable on a catalog item when the catalog task gets closed. Any help on how to formulate the script would be appreciated.

1 ACCEPTED SOLUTION

DpkSharma
Giga Expert

Hi @Brian DeBlis 

To populate the requested item parent related list from a list collector variable on a catalog item when the catalog task gets closed, you can use the following steps:

  1. Open the Requested Item (RITM) form in ServiceNow's Form Designer.
  2. Identify the related list where you want the selected items from the list collector variable to be displayed individually.
  3. Add a new client script to the RITM form.
  4. In the client script, use the onComplete event to trigger the population of the parent related list.
  5. Retrieve the selected items from the list collector variable using the getValue() method.
  6. Iterate through the selected items and create new records in the parent related list.
  7. Set the appropriate field values for each new record based on the selected items.
  8. Save the new records in the parent related list.
    Here is an example of how the client script might look like:
function onComplete() {
  var listCollectorVariable = g_form.getControl('<list_collector_variable_sys_id>');
  var selectedItems = listCollectorVariable.getValue();

  for (var i = 0; i < selectedItems.length; i++) {
    var selectedItem = selectedItems[i];
    var newRecord = new GlideRecord('<parent_related_list_table>');

    // Set field values for the new record
    newRecord.initialize();
    newRecord.<field1> = selectedItem.<field1>;
    newRecord.<field2> = selectedItem.<field2>;
    // Set other field values as needed

    newRecord.insert();
  }
}

 

 

View solution in original post

2 REPLIES 2

SanjivMeher
Kilo Patron
Kilo Patron

What do you have in the list collector?

If those are task records, you can create a onAfter BR and get the list collector using current.request_item.variables.<list collector name> and then for each element in the list collector, update the task record with requested item as parent record.


Please mark this response as correct or helpful if it assisted you with your question.

DpkSharma
Giga Expert

Hi @Brian DeBlis 

To populate the requested item parent related list from a list collector variable on a catalog item when the catalog task gets closed, you can use the following steps:

  1. Open the Requested Item (RITM) form in ServiceNow's Form Designer.
  2. Identify the related list where you want the selected items from the list collector variable to be displayed individually.
  3. Add a new client script to the RITM form.
  4. In the client script, use the onComplete event to trigger the population of the parent related list.
  5. Retrieve the selected items from the list collector variable using the getValue() method.
  6. Iterate through the selected items and create new records in the parent related list.
  7. Set the appropriate field values for each new record based on the selected items.
  8. Save the new records in the parent related list.
    Here is an example of how the client script might look like:
function onComplete() {
  var listCollectorVariable = g_form.getControl('<list_collector_variable_sys_id>');
  var selectedItems = listCollectorVariable.getValue();

  for (var i = 0; i < selectedItems.length; i++) {
    var selectedItem = selectedItems[i];
    var newRecord = new GlideRecord('<parent_related_list_table>');

    // Set field values for the new record
    newRecord.initialize();
    newRecord.<field1> = selectedItem.<field1>;
    newRecord.<field2> = selectedItem.<field2>;
    // Set other field values as needed

    newRecord.insert();
  }
}