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

Thanks for the reply Stewe. Can you please help me with laying out above code with Chuck's script? Thanks!

Thanks for the reply Steve. I've built this one with the help of you and Chuck. Can you please help me with the script include to add/remove list records. Thanks!

(function executeRule(current, previous /*null when async*/) {
	var arrayUtil = new ArrayUtil();
	// Get current contents of list field
	var currentList = current.u_data_elements.split(',');
	gs.addInfoMessage('New field Values '+ currentList);
	// Get old value of list field
	var oldList = previous.u_data_elements.split(',');
	gs.addInfoMessage('Old field Values '+ oldList);
	// Get the difference of the array
	arrayUtil.diff(oldList, currentList);
	arrayUtil.diff(currentList, oldList);
	gs.addInfoMessage('Removed Values - ' + arrayUtil.diff(oldList, currentList));
    gs.addInfoMessage('Added Values - ' + arrayUtil.diff(currentList, oldList));
	
	// Your script include to determine what was added/subtracted from the array
	
	// Call script include function to add/remove related records
	
})(current, previous);

Well
I dont think there is a general script include to add match in a related list!


Which related list is it you want to add data in ? 

I sent an example for a fucntion thata adds a user and a group in the sys_user_grmember related list.

 

hope this helps 

function add_to_grmbr(user__sys_id,group_sys_id){
	var grm_gr = new GlideRecord('sys_user_grmember');
	grm_gr.initialize();
	grm_gr.setValue('user',user_sys_id);
	grm_gr.setValue('group',group_sys_id);
	grm_gr.insert();
}

I've created a new table to populate the records based on form's list field values and I created a relationship to the table where list field exist.

So you are set ? 
Or do you acquire more assistance ? 

 

To make this simple.

 

Once you have an array with added items and one one list with removed values. 

 

loop them through 

a simple for loop ore each of the arrays will be fine. 
In the added items loop you use the function I wrote for you, modify it to suite you table. 
In the removed items loop you create a function that removes the row effected. 
using a GlideRecord  (check out deleteRecord). 

 

Good luck. 

/Stewe


Please mark the answers help full or correct if you find them to help you solve or soves your problem so that other users easier can draw benefit of them.