Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How can add records to related list using a list field on the form?

Paul125
Kilo Guru

Hello, I have a list field on the form much like a watch list. As I add values to the list field and saves the form a new record should be created, this record contains reference to current record and a reference to added value.

I've created a new table and created a system definition>relationships with the field values 'Applies to table-[table I wanted to show the related list], 'Queries from table-[table I've created]. I am stopped here now. Can someone help me move this further? Thanks!

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

I would do this with a business rule that checks the values in the list field and checks the values in the related list and does a compare to see if anything has been added or removed from the list field then updates the related list accordingly. This is where I invite you to take a look at script includes so you can break it down in to atomic pieces of what's being checked and done.

Sample business rule:

Name: Update Related List

Active: true

Insert: true

Update: true

When: After

Table: Your parent table with the list field on it

Condition: current.your_list_field.changes()

Script:

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

   // Get current contents of list field
   var currentList = current.your_list_field.split(',');
   // Get old value of list field
   var oldList = previous.your_list_field.split(',');

   // Get the difference of the array
   // Your script include to determine what was added/subtracted from the array

   // Call script include function to add/remove related records

})(current, previous);

View solution in original post

9 REPLIES 9

Chuck Tomasi
Tera Patron

I would do this with a business rule that checks the values in the list field and checks the values in the related list and does a compare to see if anything has been added or removed from the list field then updates the related list accordingly. This is where I invite you to take a look at script includes so you can break it down in to atomic pieces of what's being checked and done.

Sample business rule:

Name: Update Related List

Active: true

Insert: true

Update: true

When: After

Table: Your parent table with the list field on it

Condition: current.your_list_field.changes()

Script:

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

   // Get current contents of list field
   var currentList = current.your_list_field.split(',');
   // Get old value of list field
   var oldList = previous.your_list_field.split(',');

   // Get the difference of the array
   // Your script include to determine what was added/subtracted from the array

   // Call script include function to add/remove related records

})(current, previous);

Thanks for the reply Chuck. Can you please provide/navigate me to script include that you've mentioned? Thanks!

Chuck, please let me know your input.

Stewe Lundin
Mega Guru
var arrayUtil = new ArrayUtil();

var before_change = [1,2,3,4,5];
var after_change  = [2,3,5,6,7];

gs.print('arrayUtil.diff(before_change,after_change) - ' + arrayUtil.diff(before_change,after_change));
gs.print('arrayUtil.diff(after_change,before_change) - ' + arrayUtil.diff(after_change,before_change));
 
 
*** Script: arrayUtil.diff(before_change,after_change) - 1,4
*** Script: arrayUtil.diff(after_change,before_change) - 6,7
[0:00:00.004] Total Time