How to run assignment rules on vulnerable items when resource tag on discovered item is updated

sath
Tera Expert

Hi,

 

We have a bunch of assignment rules configured on vulnerable items based on the values contained in resource tag field of discovered items table. 

How to re-run assignment rules on vulnerable items when resource tag on discovered item is updated?

For example, a discovered item A contains resource tag 'Servers' and it is assigned to servers team. At a later point, if the resource tag on the same discovered item A is changed to 'Hardware' from 'Servers', the assignment group on VI should be updated to hardware team. What are the best options to configure this?

1 ACCEPTED SOLUTION

joe_harvey
ServiceNow Employee
ServiceNow Employee

Hey Sath,

 

You will need to add a gr.update() statement to save the changes to the sn_vul_vulnerable_item GlideRecord object.  I would also suggest adding a verification that the new assignment_group is different before updating and saving the change. This will make your code a little more efficient. 

 

Incidentally, it is best to avoid using "gr" for GlideRecord objects names. This is a relatively recent Best Practice suggestion. You will see it used extensively within OOB code but HealthScan will flag it as an issue. At the very least, I would use either "grVI" or "viGR".

 

I hope that this helps,

--Joe

View solution in original post

7 REPLIES 7

joe_harvey
ServiceNow Employee
ServiceNow Employee

Hey Sath,

this is the basic shell of the BR that I was describing

joe_harvey_0-1701806567657.png

 

You will need to write the script. The basic flow will be something like

  • Open a GlideRecord query on Vulnerable Item table
  • Filter for Vulnerable Items with Discovered Item = [current Discovered Item record]
  • Process each Vulnerable Item that is returned. As I mentioned above, it will be similar to the code in the OOB Vulnerable Item BR named "Run assignment rules". 

I hope that this helps,

--Joe

Hi @joe_harvey , I have used below script for async BR on discovered items table, but it's not changing the assignment group on VIs when resource tag on discovered item changes. Could you please let me know if there's any issue with the below script:

 

(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('sn_vul_vulnerable_item');
gr.addQuery('src_ci',current.sys_id);
gr.query();
if(gr.next()){
    var before = new GlideDateTime();
    var assignment_data = new sn_vul.AssignmentUtils().getAssignmentGroup(gr);
    if (assignment_data) {
        gr.assignment_group = assignment_data['assignment_group'];
        gr.assignment_rule = assignment_data['assignment_rule'];
        gr.assignment_type = 1;
    }
    var delta = GlideDateTime.subtract(before, new GlideDateTime());
    if (gs.nil(sn_vul.assignmentRuleTime))
	sn_vul.assignmentRuleTime = parseFloat(delta.getNumericValue() / 1000);
    else
	sn_vul.assignmentRuleTime += parseFloat(delta.getNumericValue() / 1000);	
}

})(current, previous);

 

 

joe_harvey
ServiceNow Employee
ServiceNow Employee

Hey Sath,

 

You will need to add a gr.update() statement to save the changes to the sn_vul_vulnerable_item GlideRecord object.  I would also suggest adding a verification that the new assignment_group is different before updating and saving the change. This will make your code a little more efficient. 

 

Incidentally, it is best to avoid using "gr" for GlideRecord objects names. This is a relatively recent Best Practice suggestion. You will see it used extensively within OOB code but HealthScan will flag it as an issue. At the very least, I would use either "grVI" or "viGR".

 

I hope that this helps,

--Joe