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.

Use Top Task to Identify All Project Tasks Associated With A Particular Project

James Rogers
Tera Contributor

I am writing a business rule for table pm_project that will use a GlideRecord query to identify all project tasks associated with a project. I am trying to use a field called top_task because both pm_project and pm_project_task include a top_task field. Top_task is a reference to the parent of both pm_project and pm_project_task. I know all records have the same top task value because I have logged via gs.info the top_task values for both the project and tasks. The values all match. Nonetheless, it seems as if pm_project cannot query pm_project_task via a business rule, and I do not know why, or how to get around it. Below is my business rule attached to pm_project. The rule fires OK. It just does not read anything from pm_project_task. I have even tried printing getRowCount() and always get 0, even though there were twelve tasks associated with the project. Please advise.

 

var project_task = new GlideRecord('pm_project_task');
project_task.addQuery('top_task', current.top_task);
project_task.query();
 
while (project_task.next())
gs.info('Project Task: ' +project_task.number + ' Project:  ' + current.number);
1 REPLY 1

Bert_c1
Kilo Patron

try:

var project_task = new GlideRecord('pm_project_task');
project_task.addQuery('top_task', current.top_task);
project_task.query();
 
while (project_task.next()) {
     gs.info('Project Task: ' +project_task.number + ' Project:  ' + current.number);
}

provided there is a field named 'top_task' present on each table.  Why not use 'parent'?  Using top_task I have:

 

var pmprj = new GlideRecord('pm_project');
pmprj.query();
while (pmprj.next()) {
	var project_task = new GlideRecord('pm_project_task');
	project_task.addQuery('top_task', pmprj.top_task);
	project_task.query();

	while (project_task.next()) {
		gs.info('Project Task: ' +project_task.number + ' Project:  ' + pmprj.number);
	}
}

which seems to show what your looking for when run in scripts background. The BR script I posted should work