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.

Custom field is not updated on the current status of related incidents in Problem Record.

MihirG
Tera Contributor
Name: AssociatedIncCounts
Table: Problem[Problem]
Active: Checked
Advanced: Checked
When To Run: Before chosen
                           Update: checked.
 
Script:
(function executeRule(current, previous /*null when async*/ ) {

    var gr = new GlideRecord('incident');
    gr.addQuery('problem_id', current.sys_id);
    gr.query();
    var x;
    while (gr.next()) {
        x=gr.getRowCount();
    }
    current.u_associated_incident_count = x;
    current.update();

})(current, previous);
 
Whenever on Open any Problem Record, the ‘Associated Incident count’ custom field does not get updated. If anyone can provide assistance with this issue, I would greatly appreciate it. Thank you. Attached the Problem record image for your reference.
1 ACCEPTED SOLUTION

Najmuddin Mohd
Mega Sage

Hi @MihirG ,

When we are adding incidents to Problem record, it updates the Problem[problem_id] on the incident record.
So, the business rule is to be written on the Incident record.

 

First query the incident table with all incidents with similar Problem[problem_id]  associated and update the count on the problem record.

When: After Insert/Update
Condition: Problem changes 

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

	// Add your code here

	//gs.addInfoMessage('Previous: ' + previous.problem_id);
	//gs.addInfoMessage('current ' + current.problem_id);

	if(current.problem_id != ''){
		var id = current.problem_id; // When incident is added 
	}else{
		var id = previous.problem_id; // When incident is removed
	}
	
	var grInc = new GlideRecord('incident');
	grInc.addQuery('problem_id',id);
	grInc.query();
		
	//gs.addInfoMessage(grInc.getRowCount());

	var grPRB = new GlideRecord('problem');
	grPRB.addQuery('sys_id',id);
	grPRB.query();

	if(grPRB.next()){
		grPRB.u_incidentcount = grInc.getRowCount();
		grPRB.update();
	}


})(current, previous);

 

NajmuddinMohd_0-1723877787180.png

 


If this information helps you, kindly mark it as Helpful and Accept the solution.

Regards,
Najmuddin.

View solution in original post

2 REPLIES 2

Najmuddin Mohd
Mega Sage

Hi @MihirG ,

When we are adding incidents to Problem record, it updates the Problem[problem_id] on the incident record.
So, the business rule is to be written on the Incident record.

 

First query the incident table with all incidents with similar Problem[problem_id]  associated and update the count on the problem record.

When: After Insert/Update
Condition: Problem changes 

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

	// Add your code here

	//gs.addInfoMessage('Previous: ' + previous.problem_id);
	//gs.addInfoMessage('current ' + current.problem_id);

	if(current.problem_id != ''){
		var id = current.problem_id; // When incident is added 
	}else{
		var id = previous.problem_id; // When incident is removed
	}
	
	var grInc = new GlideRecord('incident');
	grInc.addQuery('problem_id',id);
	grInc.query();
		
	//gs.addInfoMessage(grInc.getRowCount());

	var grPRB = new GlideRecord('problem');
	grPRB.addQuery('sys_id',id);
	grPRB.query();

	if(grPRB.next()){
		grPRB.u_incidentcount = grInc.getRowCount();
		grPRB.update();
	}


})(current, previous);

 

NajmuddinMohd_0-1723877787180.png

 


If this information helps you, kindly mark it as Helpful and Accept the solution.

Regards,
Najmuddin.

MihirG
Tera Contributor

Thanks