Take values from a list field and add them to a related list

Chad R
Tera Expert

Hi All,

Been awhile, this one is got me puzzled a bit, the requirement we have a firewall request on the Service Portal where people can choose source, destination, and ports for the request. These values in turn get copied to the SC_Task on fields extended from task as shown below on 

find_real_file.png

these fields are u_source_ip_addresses, u_destination_ip_addresses, u_ports. Where I've been unsuccessful is coming up with a business rule that copies these values to the affected CI's related list as each of these fields reference tables from the CMDB. This is what I have so far but not sure on how to proceed.  The below code is what I have so far for my business rule. I'm attempting to just make one of the fields work before figuring out how to make the rule add all 3 to the affected CI's related list. As always appreciate your help folks!

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

	var array = (current.u_source_ip_addresses.toString()).split(",");


// Loop through the array of source I.P Addresses
for (var i = 0; i < array.length; i++){
    var source = array[i];

    gs.log("Processing Source I.Ps to Related CI's " + array[i]);

//create affected CI Records based on above source I.P array
var CI = new GlideRecord('task_ci');
	CI.initialize();
	CI.setValue('ci_item', source);
	CI.setValue('task', current.sys_id);
	CI.update();
}

})(current, previous);
1 ACCEPTED SOLUTION

Chad R
Tera Expert

So for anyone who comes across this post looking for the same thing the below got it done. This adds to the related list for each item in the list forum fields and adds to it but not creating duplicates if more records are added later. 

 

(function executeRule(current, previous /*null when async*/) {
	//Array utility to compare source IP previous and current list values and give only new values!
	var arr = new ArrayUtil();
	var currentsource = (current.u_source_ip_addresses.toString()).split(",");
	var previoussource = (previous.u_source_ip_addresses.toString()).split(",");
	var newsource = arr.diff(currentsource, previoussource);


// Loop through the array of source I.P Addresses
for (var i = 0; i < newsource.length; i++){
    var source = newsource[i];
//log the array for troubleshooting.
    gs.log("Processing Source I.Ps to Related CI's " + newsource[i]);
	


//create affected CI Records based on above source I.P array and if it does not exist for current record.
var CI = new GlideRecord('task_ci');
	CI.initialize();
	CI.setValue('ci_item', source);
	CI.setValue('task', current.sys_id);
	CI.insert();
	
}

})(current, previous);

View solution in original post

4 REPLIES 4

Aman Kumar S
Kilo Patron

Hey,

instead of CI.update(); use CI.insert();

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions šŸ™‚

Best Regards
Aman Kumar

Ahh thanks for that Kumar, that got it working, however it adds duplicate records now, assuming i need a query in here to see if the record on task_ci exists and only to add the records if the row count comes back as 0. 

Chad R
Tera Expert

So here is where I am now, it was working creating duplicate but trying to use array utility to remove the duplicates and I'm not even getting to the gs.log portion as I get no logs. Not sure what the issue is yet.

 

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

	var arraysource = (current.u_source_ip_addresses.toString()).split(",");
	var arr = new ArrayUtil();
	var newsource = arr.diff(current.arraysource, previous.arraysource);


// Loop through the array of source I.P Addresses
for (var i = 0; i < newsource.length; i++){
    var source = newsource[i];
//log the array for troubleshooting.
    gs.log("Processing Source I.Ps to Related CI's " + newsource[i]);
	


//create affected CI Records based on above source I.P array and if it does not exist for current record.
var CI = new GlideRecord('task_ci');
	CI.initialize();
	CI.setValue('ci_item', source);
	CI.setValue('task', current.sys_id);
	CI.insert();
	
}

})(current, previous);

Chad R
Tera Expert

So for anyone who comes across this post looking for the same thing the below got it done. This adds to the related list for each item in the list forum fields and adds to it but not creating duplicates if more records are added later. 

 

(function executeRule(current, previous /*null when async*/) {
	//Array utility to compare source IP previous and current list values and give only new values!
	var arr = new ArrayUtil();
	var currentsource = (current.u_source_ip_addresses.toString()).split(",");
	var previoussource = (previous.u_source_ip_addresses.toString()).split(",");
	var newsource = arr.diff(currentsource, previoussource);


// Loop through the array of source I.P Addresses
for (var i = 0; i < newsource.length; i++){
    var source = newsource[i];
//log the array for troubleshooting.
    gs.log("Processing Source I.Ps to Related CI's " + newsource[i]);
	


//create affected CI Records based on above source I.P array and if it does not exist for current record.
var CI = new GlideRecord('task_ci');
	CI.initialize();
	CI.setValue('ci_item', source);
	CI.setValue('task', current.sys_id);
	CI.insert();
	
}

})(current, previous);