Scheduled Report to email contacts that are in the report's output

James Rostron1
Tera Expert

I have many Scheduled Reports setup; some more complex than others but have hit a snag with scripting conditions so that it only emails the report out to named individuals in the report.

I tried referencing the script from the post here; HELP! How to dynamically set recipients of an existing Scheduled Report to email addresses that are ... but had trouble using this in my report.

This particular report is simple in that it it populates 'x' amount of data grouped by the "Assigned to" on all currently active tickets

My report is defined as......

snow.png

Instead of the report emailing a distribution list in the "Email Address" field, i just want it to email the contacts that appear in "assigned to" list who do already have an email address attached to their "contact card" in Service Now. I believe a script will achieve this but have no experience writing script.

Any help would be greatly appreciated.

1 ACCEPTED SOLUTION

Valor1
Giga Guru

Welcome to the community, James!



After creating your report, pull down the menu and choose "Schedule". Note that you'll want to omit the "Assigned to" query parameter.. we'll take care of that later in our script:


find_real_file.png


Then on the resulting page, click the "Conditional" box, and a script area shows up. Taking the suggested script from the other thread you referenced, paste it in the script area:


find_real_file.png



Here's the script that should work for you:


answer = lookupUsersAndUpdateReport();


function lookupUsersAndUpdateReport(){


  // look up "assigned_to" people -- assumes "table" is task-based


  var senderArray = [];



  var taskQueryGR = new GlideRecord(current.report.table);


  taskQueryGR.addEncodedQuery(current.report.filter);


  taskQueryGR.query();


  while (taskQueryGR.next()){


          senderArray.push(taskQueryGR.assigned_to + '');


  }



  current.user_list = senderArray.join(',');


  current.setWorkflow(false);


  current.update();


  current.setWorkflow(true);



  // only return true if there were records listed in the result set


  if (senderArray.length > 0){


          return true;


  }


  return false;


}



..And that should do it!


View solution in original post

5 REPLIES 5

Valor1
Giga Guru

Welcome to the community, James!



After creating your report, pull down the menu and choose "Schedule". Note that you'll want to omit the "Assigned to" query parameter.. we'll take care of that later in our script:


find_real_file.png


Then on the resulting page, click the "Conditional" box, and a script area shows up. Taking the suggested script from the other thread you referenced, paste it in the script area:


find_real_file.png



Here's the script that should work for you:


answer = lookupUsersAndUpdateReport();


function lookupUsersAndUpdateReport(){


  // look up "assigned_to" people -- assumes "table" is task-based


  var senderArray = [];



  var taskQueryGR = new GlideRecord(current.report.table);


  taskQueryGR.addEncodedQuery(current.report.filter);


  taskQueryGR.query();


  while (taskQueryGR.next()){


          senderArray.push(taskQueryGR.assigned_to + '');


  }



  current.user_list = senderArray.join(',');


  current.setWorkflow(false);


  current.update();


  current.setWorkflow(true);



  // only return true if there were records listed in the result set


  if (senderArray.length > 0){


          return true;


  }


  return false;


}



..And that should do it!


Thank you so much Valor! I confirm that this still works now, 3 years later.

I noticed that in your script, if a user is assigned to 3 tasks in the report results, he gets added to the list of users 3 times. That means there's 2 duplicate entries for the user. I'm also unsure of what the impact of trying to set blank users are, which may happen if it processes a task that has a blank assigned_to.

Here is the way I fixed this duplication and avoided tasks with a blank assigned_to.

 

answer = lookupUsersAndUpdateReport();
function lookupUsersAndUpdateReport(){
	// look up "assigned_to" people -- assumes "table" is task-based
	var senderArray = [];
	var PrevSender = '';

	var taskQueryGR = new GlideRecord(current.report.table);
	taskQueryGR.addEncodedQuery(current.report.filter);
	taskQueryGR.addEncodedQuery('assigned_toISNOTEMPTY');
	taskQueryGR.orderBy('assigned_to');
	taskQueryGR.query();
	while (taskQueryGR.next()){
		if (PrevSender != taskQueryGR.assigned_to + ''){ //filters out duplicate assignees
			senderArray.push(taskQueryGR.assigned_to + '');
			PrevSender = taskQueryGR.assigned_to + '';
		}
	}

	current.user_list = senderArray.join(','); 
	current.setWorkflow(false);
	current.update();
	current.setWorkflow(true);

	// only return true if there were records listed in the result set
	if (senderArray.length > 0){
		return true;
	}
	return false;
}

Elton2
Tera Contributor

Hi @Valor1, @ how are you?!

Could you please give me a suggestion?! 

 

I have a Report needs to be sent to the Group Holder, but this Holder is in a Reference field called "Business Partner Department" (PRINT_1),

 

I checked and suggested that I created a "Dot Walk" in the "Condition" section of the Schedule (PRINT_2), but unfortunately is not working:

 

var grCmdb = new GlideRecord('cmdb_ci_business_company');
grCmdb.query();
while (grCmdb.next()) {
    if (grCmdb.u_business_partner_department.u_holder) {
        gs.info(grCmdb.u_business_partner_department.u_holder);
    }

}

I just need to get the "Holder" of a reference field for the Report to be sent.

 

Could anyone please give me a tip or script suggestion?

Thanks for the support!

James Rostron1
Tera Expert

thanks very much...working great now!