How to collect the values from a 'list' field from Child records to a 'list' field in parent record

Maloy Banerjee1
Tera Expert

Hi All,

 

I have to write a Business Rule to collect the values from a 'list' field from Child records to a 'list' field in the parent record in a collective manner.

 

E.g. There is an incident and that incident is having 3 Incident Tasks. In the Incident form, there is a custom field (glide_list) - Users. I want to collect the values of the Users field from the Incident Task records and put all the users in the Users field of the parent record.

Incident Task 1: Users - User 1, User 2, User 3

Incident Task 2: Users - User 3, User 4, User 5

Incident Task 3: Users - User 1, User 2, User 6, User 7

 

How can we achieve this?

*Note: The Users field in the incident should get updated if the users are removed from the Users field in the Incident task.

 

Can you please suggest?

 

 

Thanks,

Maloy Banerjee

1 ACCEPTED SOLUTION

Amit Gujarathi
Giga Sage
Giga Sage

Hi @Maloy Banerjee1 ,
I trust you are doing great.
To achieve this, you can create a business rule on the Incident Task table that runs on the "after" insert, update, or delete event. The business rule should check if the incident associated with the current task has any other tasks, and if not, update the Users field on the incident with the values from the current task.
Please find the code for the same:

(function executeRule(current, previous /*null when async*/) {
  // Check if the current task is associated with an incident
  if (current.incident.nil()) {
    return;
  }
  
  // Get all tasks associated with the current incident
  var tasks = new GlideRecord('incident_task');
  tasks.addQuery('incident', current.incident);
  tasks.query();
  
  // Create an array to hold all user values
  var users = [];
  
  // Loop through all tasks and collect user values
  while (tasks.next()) {
    var taskUsers = tasks.getValue('users');
    if (taskUsers) {
      taskUsers.split(',').forEach(function(user) {
        if (users.indexOf(user.trim()) === -1) {
          users.push(user.trim());
        }
      });
    }
  }
  
  // Update the incident with the collected user values
  var incident = new GlideRecord('incident');
  if (incident.get(current.incident)) {
    incident.setValue('users', users.join(', '));
    incident.update();
  }
})(current, previous);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

3 REPLIES 3

Jakob Anker
ServiceNow Employee
ServiceNow Employee

This can be done. But why would you do it, what is the user story?

Joshua_Quinn
Kilo Guru

Hello Maloy,

 

You should be able to do this with a set of BRs on the Incident Task table. Set up the BR's to trigger on creation of a Incident task with users in the "Users" field or when that field changes.

 

What you then do is have the script query for all tasks on the incident and put them in a comma separated string and set the parent incident field to that string. It would look something like: 

var incident_gr = new GlideRecord('incident');
incident_gr.get(current.parent.toString());
var users = '';
var task_users = '';
var incident_task_gr = new GlideRecord('incident_task');
incident_task_gr.addQuery('parent', current.parent.toString());
incident_task_gr.query();
while(incident_task_gr.next()){
  task_users = incident_task_gr.getValue('users');
  if(task_users != ''){
    if(users != ''){
      users += ',';
    }
    users += task_users;
  }
}
incident_gr.setValue('users', users);
incident_gr.update();

This will allow the field on the incident table to stay updated when users are added or removed on incident tasks and if a new incident task is created. If you want to account for incident task deletion, have the same script run on a Delete BR for incident task records.

Amit Gujarathi
Giga Sage
Giga Sage

Hi @Maloy Banerjee1 ,
I trust you are doing great.
To achieve this, you can create a business rule on the Incident Task table that runs on the "after" insert, update, or delete event. The business rule should check if the incident associated with the current task has any other tasks, and if not, update the Users field on the incident with the values from the current task.
Please find the code for the same:

(function executeRule(current, previous /*null when async*/) {
  // Check if the current task is associated with an incident
  if (current.incident.nil()) {
    return;
  }
  
  // Get all tasks associated with the current incident
  var tasks = new GlideRecord('incident_task');
  tasks.addQuery('incident', current.incident);
  tasks.query();
  
  // Create an array to hold all user values
  var users = [];
  
  // Loop through all tasks and collect user values
  while (tasks.next()) {
    var taskUsers = tasks.getValue('users');
    if (taskUsers) {
      taskUsers.split(',').forEach(function(user) {
        if (users.indexOf(user.trim()) === -1) {
          users.push(user.trim());
        }
      });
    }
  }
  
  // Update the incident with the collected user values
  var incident = new GlideRecord('incident');
  if (incident.get(current.incident)) {
    incident.setValue('users', users.join(', '));
    incident.update();
  }
})(current, previous);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi