How to calculate the number of incidents caused by change on change form

Snow909
Tera Expert

Hello All,

On incident form i have created a new field called "Number of incidents caused by change" which is an integer.

This filed should auto populate with the count of incident caused by change

1. New Field which gets the count of incidents 

2.incidents caused by change related list

3. Edit is the place where we can add incidents or remove incidents

The issue is created a business rule with the below code, it works only when new incidents are added, but when we remove the incidents  the count is not getting updated.

BR code: for testing purpose i was using the info message to show on the form:

NOTE: Both the business rules work and update when new incidents are added but when removed from edit count not getting updated.

Script1 : written on the change form

find_real_file.png

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

// Add your code here
var TaskInc = new GlideRecord('incident');
TaskInc.addQuery('caused_by', current.sys_id);
//TaskInc.addEncodedQuery('caused_by!=NULL');
TaskInc.query();
var count = TaskInc.getRowCount();
gs.addInfoMessage("Total incidents caused by change before if condition --" + TaskInc.getRowCount());
//alert("change happened");

if (TaskInc.next()) {
if (count > 0) {
gs.addInfoMessage("Total incidents is greater than zero --" + TaskInc.getRowCount());

// current.u_number_of_incidents_caused_by_change = TaskInc.getRowCount();
// current.update();
} else {
gs.addInfoMessage("total incident count is Zero");
// current.u_number_of_incidents_caused_by_change = 0;
// current.update();
//gs.addInfoMessage(test)
}
}

})(current, previous);

 

Script 2: written on the incident form

find_real_file.png

(function executeRule(current, previous /*null when async*/ ) {
var task = new GlideRecord('incident');
task.addQuery('caused_by', current.caused_by);
task.query();
//gs.addInfoMessage("Incident count was --" + task.getRowCount());

var Chg = new GlideRecord('change_request');
Chg.addQuery('sys_id', current.caused_by);
Chg.query();
if (Chg.next()) {
Chg.u_number_of_incidents_caused_by_change = task.getRowCount();
Chg.update();

}

})(current, previous);

find_real_file.png

1 ACCEPTED SOLUTION

Hello Sree, 

You can achieve this by an after business rule applied to incident table. 

find_real_file.png

 

Script : 

(function executeRule(current, previous /*null when async*/ ) {
	var sys_id_ch = '';
	var op = '';
	if(gs.nil(current.caused_by)&&!gs.nil(previous.caused_by)){
		sys_id_ch =previous.caused_by;
		op = 'descrease';
	}else if(!gs.nil(current.caused_by)&&gs.nil(previous.caused_by)){
		sys_id_ch = current.caused_by;
		op = 'increase';
	}
    var change_sys_id = current.caused_by;
    var change_rec = new GlideRecord('change_request');
    change_rec.addQuery('sys_id', sys_id_ch);
    change_rec.query();
    if (change_rec.next()) {
		if(op == 'increase'){
			change_rec.u_number_incident = change_rec.u_number_incident +1;
			change_rec.update();
		}
		else if(op == 'descrease'){
			change_rec.u_number_incident = change_rec.u_number_incident -1;
			change_rec.update();
		}
		
    }

})(current, previous);

View solution in original post

26 REPLIES 26

Hajar BENJAHHAR
Mega Sage

Hello, as I understand you problem was about calculating the number of incident caused by a change and the probelem reside when you remove an incident. So what I propose for you is to use a client script and a script include as shown in the picture below:  

find_real_file.png

find_real_file.png

 

Snow909
Tera Expert

Thanks for looking at it and sharing the code. I replicated the code but its not working as expected.

Attached the screen shots for your reference

Script include:

find_real_file.png

 

Client script;

find_real_file.png

 Please correct me if i am missing something.

whenever a change form is loaded it shows zero records. but there are around 7 records which are not updating.

find_real_file.png

Hello, I hope your doing fine. For the code that I shared I thought the field should be in the incident form that's why It didn't work. 

So If you want to execute it in the change form,you have to change only one thing in the code. 

in the ajax.addParam('sysparm_sys_id_causedBy', g_form.getValue('caused_by'); by this syntaxe : 

ajax.addParam('sysparm_sys_id_causedBy', g_form.getUniqueValue()); (You have to get the sys_id of the current record in the change form so to do it I used the function getUniqueValue). 

So please tell me if it works or not. 

find_real_file.png

find_real_file.png

 

Best regards. 

 

Hello ,

when i tried the value is not showing on the change form. Its empty. if i replace with with string value that shows on the incident count.

please correct me if am missing anything.

find_real_file.png