Updating parent field when the Child records are updated

vhari2
Tera Expert

I have a Related list called Affected CI under a Form. The requirement is that when the number of Affected CI > 3 the field on parent form Change type should be set to Complex else if it is less then change type is Standard .

I wrote a Business rule on child table :

 

var gr = new GlideRecord('task_ci');
gr.addQuery('task',current.task);
gr.query();

var x = gr.getRowCount();
gs.addInfoMessage(x);
if (x >= 3) {
var ga = new GlideRecord ('u_bau');
ga.addQuery('sys_id',current.task);
ga.query();
while (ga.next()){
if(ga.u_routine == 'No') {
ga.u_change_type = 'Complex';
ga.update();
}

}
}

 

Somehow its not working . When i went to see the log message it says 

1 ACCEPTED SOLUTION

Hi Somehow the code is calculating wrong Count. Its coming correct when aded but when deleted its not giving me a count at all. Somehow i had to write totally a different way to code . I am not sure if this is ideal but it forks for now 

 

if (current.operation() == 'insert') {
var gr = new GlideRecord('task_ci');
gr.addQuery('task', current.task);
gr.query();
var c = gr.getRowCount() + 1;
var ga = new GlideRecord ('u_bau');
ga.addQuery('sys_id',current.task);
ga.addQuery('u_routine','No');
ga.query();
while (ga.next()){
if (c >= 3) {
gs.info(c);
ga.u_change_type = 'Complex';
ga.update();
} else {
ga.u_change_type = 'Standard';
ga.update();
}


}

}
else if (current.operation() == 'delete') {
var gb = new GlideRecord('task_ci');
gb.addQuery('task', current.task);
gb.query();
var d = gb.getRowCount() - 1;

var gd = new GlideRecord ('u_bau');
gd.addQuery('sys_id',current.task);
gd.addQuery('u_routine','No');
gd.query();
while (gd.next()){
if (d >= 3) {
gd.u_change_type = 'Complex';
gd.update();
} else {
gd.u_change_type = 'Standard';
gd.update();
}
}


}
}

View solution in original post

3 REPLIES 3

Brent Sutton
Mega Sage

Hi Vhari,

Can you please try the following script in your business rule and let me know how you get along:

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

	var taskSysId = current.task; //sys_id of affected CI

	//count the number of affected CI's associated with the task.
	var taskCI = new GlideAggregate("task_ci");
	taskCI.addQuery("task",taskSysId);
	taskCI.addAggregate("COUNT");
	taskCI.query();

	//if any CI's are returned then extract the count.
	if (taskCI.next()) {
		var taskCount = taskCI.getAggregate("COUNT");

		if (taskCount > 3) {
			var bau = new GlideRecord("u_bau");

			if (bau.get(taskSysId)) {

				gs.info(gs.getMessage("Value for routine {0}",[bau.u_routine])); //check value of routine to ensure the next test is on the expected value.

				if (bau.u_routine == "No") {
					bau.setValue("u_change_type","Complex");
					bau.update();
				}
			}
		}
	}
	else {
		gs.info(gs.getMessage("No affected CI's can be found for {0}",[current.task.getDisplayValue()]));
	}

})(current, previous);

I've also added some logging statements to check that your test of the "u_routine" value is against the expected value. If "u_routine" is a choice box it might have a display value of "No" but an actual value of "1". If the script doesn't work then check the logs to see what value "u_routine" actually is.

Let me know if it worked for you.

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.

Brent Sutton
Mega Sage

Hi Vhari,

Just checking if the code I provided answered your question? If so, then please mark as correct so other community members can benefit from this information.

Thanks

Brent

Hi Somehow the code is calculating wrong Count. Its coming correct when aded but when deleted its not giving me a count at all. Somehow i had to write totally a different way to code . I am not sure if this is ideal but it forks for now 

 

if (current.operation() == 'insert') {
var gr = new GlideRecord('task_ci');
gr.addQuery('task', current.task);
gr.query();
var c = gr.getRowCount() + 1;
var ga = new GlideRecord ('u_bau');
ga.addQuery('sys_id',current.task);
ga.addQuery('u_routine','No');
ga.query();
while (ga.next()){
if (c >= 3) {
gs.info(c);
ga.u_change_type = 'Complex';
ga.update();
} else {
ga.u_change_type = 'Standard';
ga.update();
}


}

}
else if (current.operation() == 'delete') {
var gb = new GlideRecord('task_ci');
gb.addQuery('task', current.task);
gb.query();
var d = gb.getRowCount() - 1;

var gd = new GlideRecord ('u_bau');
gd.addQuery('sys_id',current.task);
gd.addQuery('u_routine','No');
gd.query();
while (gd.next()){
if (d >= 3) {
gd.u_change_type = 'Complex';
gd.update();
} else {
gd.u_change_type = 'Standard';
gd.update();
}
}


}
}