How to pull Catalog item CI to the sctask "Affected CI" related list?

Devi12
Giga Guru

Hi all,

I am creating a catalog item in which there is a list collector variable, which is referencing from business application table of CI. Requirement is: When a user selects any CI in this list collector variable that should be populated on to the "Affected CI" related list on the sctask. How can I do that? 

Devi12_0-1665572343281.png

Thanks in advance!

2 REPLIES 2

Sai Shravan
Mega Sage

Hi @Devi12 

 

You'll want to do this in a business rule on the sc_task table with the following settings.  I'm using the variable name as select_ci.  Just make sure that's correct!

Name: Add Affected CIs

When: After

Insert: True

Advanced: True

Condition: current.select_ci.changes() && !current.select_ci.nil();

(function executeRule(current, previous /*null when async*/ ) {
    var cis = current.variables.select_ci.getDisplayValue().split(/\s*,\s*/); // Split the IDs into an array
    gs.log(cis);
    for (i = 0; i < cis.length; i++) // Iterate through the array & Create a new 'Affected CI' record for each
    {
        var aci = new GlideRecord('task_ci');
        aci.initialize();
        aci.task = current.sys_id; // Associate to this record
        aci.ci_item = cis[i];
        aci.insert();
    }
})(current, previous);

 

Please mark this as the correct answer if I've answered your question.  Thanks!

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Brad Bowman
Kilo Patron
Kilo Patron

This Business Rule script will take the list collector variable values (a comma-separated list of sys_ids - which is what you need to create an Affected CI record, not the display value) and create an Affected CI record for each one that was just added - realizing that users click around a lot and could potentially add a CI, save the Catalog Task record, then add another one,... then maybe remove one/some...  For this reason, the script also includes a check of CIs that were removed from the list collector variable, and will delete the Affected CI record for these.  You can just exclude the last for loop if you don't want to do this for some reason. 

 

Be sure to run this Business Rule before Insert and Update on the sc_task table with the Condition

current.variables.business_application.changes()

where business_application is the name of your list collector variable.  If you want to run this for only this one catalog item or Catalog Task, you can add that to the Condition or Filter Conditions as well.  This is what your Script on the Advanced tab should look like - again depending on your variable name:

(function executeRule(current, previous /*null when async*/) {
	
    var ciBefore = previous.variables.business_application.toString().split(',');
	var ciAfter = current.variables.business_application.toString().split(','); 
	var arrayUtil = new ArrayUtil();
	var ciDel = arrayUtil.diff(ciBefore, ciAfter); //create a new array of CIs that were removed
	var ciAdd = arrayUtil.diff(ciAfter, ciBefore); //create a new array of CIs that were added
		
	for (i = 0; i < ciAdd.length; i++) { //Create an Affected CI record for each newly selected CI
        var aci = new GlideRecord('task_ci');
        aci.initialize();
        aci.task = current.sys_id;
        aci.ci_item = ciAdd[i];
        aci.insert();
    }
	
	for (j = 0; j < ciDel.length; j++) { //Delete the Affected CI record for each CI that was removed
        var aci2 = new GlideRecord('task_ci');
        aci2.addQuery('task', current.sys_id);
		aci2.addQuery('ci_item', ciDel[j]);
        aci2.query();
		if (aci2.next()) {
			aci2.deleteRecord();
		}
    }
})(current, previous);