How to update project field from project task record?

MBarrott
Mega Sage

I'm wanting to calculate the percent of tasks completed and update it to the percent_complete field of the overall project record. 

 

To achieve this I have an after insert/update business rule running. 

 

(function executeRule(current, previous /*null when async*/ ) 
{	
	var prjTaskGA = new GlideAggregate('pm_project_task');
	prjTaskGA.addQuery('top_task', current.top_task);
	prjTaskGA.query();
	
	totalGARows = prjTaskGA.getRowCount();
	
	prjTaskGA.addQuery('active', false);
	prjTaskGA.query();

	inactiveGARows = prjTaskGA.getRowCount();
	percentCompleteGA = (inactiveGARows / totalGARows) * 100;

	gs.addInfoMessage('total rows agg found is: ' + totalGARows + ' total inactive rows found is: ' + inactiveGARows + ' Percent complete is: ' + percentCompleteGA.toFixed(2) + '%');

	var gr = new GlideRecord('pm_project');
	gr.addQuery('number', current.top_task);
	gr.query();
	while(gr._next())
	{
		gr.percent_complete = percentCompleteGA.toFixed(2);	
	}
	
	gr.update();

})(current, previous);

 

I'm encountering two issues. 

 

1. the business rule is generating a "Name field can not be empty." error - what's causing that?

2. the percent_complete field is not updating

1 ACCEPTED SOLUTION

MBarrott
Mega Sage

Self-diagnosed and fixed the issue!

 

For anyone curious:

 

1. "Name field can not be empty." was tied to the gr.update() being outside the while loop. 

2. The parenting project field was not updating as the top_task carries the sys_id, so allocating to number generated zero results. Likely also impacted the gr.update() as there was nothing to actually update. 

 

Fixed script below:

 

 

(function executeRule(current, previous /*null when async*/ ) 
{	
	// declare glideaggregate record and query against top_task (parenting project)
	var prjTaskGA = new GlideAggregate('pm_project_task');
	prjTaskGA.addAggregate('COUNT');
	prjTaskGA.addQuery('top_task', current.top_task);
	prjTaskGA.query();
	
	// retrieve total number of task records tied to parent project
	if(prjTaskGA.next())
	{
		totalGARows = prjTaskGA.getAggregate('COUNT');
	}

	// execute additional query to find only inactive tasks tied to parent project
	prjTaskGA.addQuery('active', false);
	prjTaskGA.query();

	// retrieve total number of inactive task records tied to parent project
	if(prjTaskGA.next())
	{
		inactiveGARows = prjTaskGA.getAggregate('COUNT');
	}

	// calculate percent of completed tasks 
	percentCompleteGA = (inactiveGARows / totalGARows) * 100;

	// data checking - remove once working
	//gs.addInfoMessage('total rows agg found is: ' + totalGARows + ' total inactive rows found is: ' + inactiveGARows + ' Percent complete is: ' + percentCompleteGA.toFixed(2) + '%');
	//gs.addInfoMessage('top task is: ' + current.top_task);

	// declare gliderecord for project table and retrieve parent project 
	var gr = new GlideRecord('pm_project');
	gr.addQuery('sys_id', current.top_task);
	gr.query();
	while(gr.next()) // loop through parent project and update percent_complete (rounded to 2 dec. place)
	{
		gr.percent_complete = percentCompleteGA.toFixed(2);	
		gr.update();
	}
})(current, previous);

View solution in original post

1 REPLY 1

MBarrott
Mega Sage

Self-diagnosed and fixed the issue!

 

For anyone curious:

 

1. "Name field can not be empty." was tied to the gr.update() being outside the while loop. 

2. The parenting project field was not updating as the top_task carries the sys_id, so allocating to number generated zero results. Likely also impacted the gr.update() as there was nothing to actually update. 

 

Fixed script below:

 

 

(function executeRule(current, previous /*null when async*/ ) 
{	
	// declare glideaggregate record and query against top_task (parenting project)
	var prjTaskGA = new GlideAggregate('pm_project_task');
	prjTaskGA.addAggregate('COUNT');
	prjTaskGA.addQuery('top_task', current.top_task);
	prjTaskGA.query();
	
	// retrieve total number of task records tied to parent project
	if(prjTaskGA.next())
	{
		totalGARows = prjTaskGA.getAggregate('COUNT');
	}

	// execute additional query to find only inactive tasks tied to parent project
	prjTaskGA.addQuery('active', false);
	prjTaskGA.query();

	// retrieve total number of inactive task records tied to parent project
	if(prjTaskGA.next())
	{
		inactiveGARows = prjTaskGA.getAggregate('COUNT');
	}

	// calculate percent of completed tasks 
	percentCompleteGA = (inactiveGARows / totalGARows) * 100;

	// data checking - remove once working
	//gs.addInfoMessage('total rows agg found is: ' + totalGARows + ' total inactive rows found is: ' + inactiveGARows + ' Percent complete is: ' + percentCompleteGA.toFixed(2) + '%');
	//gs.addInfoMessage('top task is: ' + current.top_task);

	// declare gliderecord for project table and retrieve parent project 
	var gr = new GlideRecord('pm_project');
	gr.addQuery('sys_id', current.top_task);
	gr.query();
	while(gr.next()) // loop through parent project and update percent_complete (rounded to 2 dec. place)
	{
		gr.percent_complete = percentCompleteGA.toFixed(2);	
		gr.update();
	}
})(current, previous);