Updating a List field with a Business Rule each time another field changes

Wayne Richmond
Tera Guru

Hi there. I have the following requirement: On the cmdb_ci_computer table I have added a new List field called u_users which references the sys_user table. When assigned_to (also references sys_user) changes I want to copy the value to the u_users field, however, I don't want to replace the existing value but rather add to it. What I'll eventually have is a list of users who have been recorded to have used a computer. I also don't want duplicates adding but I figure SN might sort this for me automatically. Any thoughts? This is my current effort:

When: before (Insert and Update)

Condition: [Assigned to] [changes]

(function executeRule(current, previous /*null when async*/) {

current.u_users = current.assigned_to;

})(current, previous);

Thanks

1 ACCEPTED SOLUTION

Justin Abbott
Giga Guru

UPDATED to filter out duplicates.



I would capture what's already in the field, then add the new person.



Something like this...



(function executeRule(current, previous /*null when async*/) {


  var users = current.u_users.split(',');



  users.push(current.assigned_to.toString());



  users = new ArrayUtil().unique(users);



  current.u_users = users.toString();



})(current, previous);


View solution in original post

5 REPLIES 5

Justin Abbott
Giga Guru

UPDATED to filter out duplicates.



I would capture what's already in the field, then add the new person.



Something like this...



(function executeRule(current, previous /*null when async*/) {


  var users = current.u_users.split(',');



  users.push(current.assigned_to.toString());



  users = new ArrayUtil().unique(users);



  current.u_users = users.toString();



})(current, previous);


Hey, thanks for the reply! That worked a treat, except, it did create duplicates:


find_real_file.png


Any idea how I could prevent this?


When I tried it out in my instance, it worked just fine without duplicates.



Can you paste the code you tried? The use of '.toString()' was incredibly important here.


Thanks! I missed your update before I tried the code. Having added the updated code it works perfectly! Thank you so much, that's really really helpful. All the best my friend.