How can I run a report from a workflow and attach the output to an email?

gjz
Mega Sage

I have a requirement for a new catalog item that I'm not sure how to accomplish.

 

The workflow requires two approvals - both to C-level people who do not use ServiceNow.  I plan to use a create event to send them an approval email.  However, they will need to see the details of the requested item which will make for an extremely long email body.  My first thought was to run a report and export it to PDF to make it easier to read but I don't know how to do that.

 

Here's an example of some of the questions on the form - they are multi line text and as you can see someone can add quite a bit of information to answer the questions.

gjz_0-1666993562823.png

 

Does anyone have an idea how I can run a report, export it and attach it to an email from a workflow?  Or do you have a better idea to satisfy the requirement?  Any alternate ideas or suggestions are welcome!

 

 

4 REPLIES 4

johansec
Tera Guru

I like the report idea, however I have never done that. I have build a pdf attachment using html then added it to the record. Then fire an event that triggers an email that includes attachments. If nothing better comes up let me know if you want to give this a shot

I like this idea, can you share how to do that?

So what I started with was 

  1. Register an event 
  2. Create a script action to listen for this event
  3. Create an html template for what you would like the pdf to look like and put dummy values that you can replace for specific fields like this ( this is what i did you can just put your values directly in if you want) <span>my_short_desc</span>  
    1. This is assuming you have alot of html and want to maybe make it in a different editor to make sure cosmetically it looks good before. ( be aware not all css works in pdfs so be careful )
  4. then i would replace with record values thisRowHTML = thisRowHTML.replace('my_short_desc',current.description.toString());
  5. Now you have a string that contains all your html and your values from the record stored in a variable
  6. Next I used this code to generate the pdf

 

var pageProperties = {
		//HeaderImageAttachmentId: 'fb4067b6db8d8910e01e3fd3e29619e0',
		HeaderImageAlignment: 'LEFT',
		PageSize: 'A4 landscape',
		GeneratePageNumber: 'false',
		TopOrBottomMargin: '72',
		LeftOrRightMargin: '36'
	};

	// Generate the PDF And attach it to the po
	var v = new sn_pdfgeneratorutils.PDFGenerationAPI;
	var result = v.convertToPDFWithHeaderFooter(combined_html, table, record_sys_id, filename, pageProperties, '');

 

  • Here are the parameters
    1. your html variable 
    2. the destination table you want to attach it to ( if fired from wf you can prob use current.table)
    3. the destination record id you want to attach it to 
    4. whatever the file name you want it to be 
    5. the page properties that you can tinker around with to get it to work with how you want
  • Now it should generate the attachment and your almost ready for your email
  • In order to trigger the email i needed to register one more event  so do that
  • Create an event driven notification that includes attachments ( its just a checkbox ) 
  • Then back in your script you will have to fire this event. 
  • I ran into an issue with this because it was happening to quickly and firing the notification before the attachment was finished so i had to add a delay and here is the code for that 

 

	// Set a delay to give time for the pdf to fully generate
	var when = new GlideDateTime();
	var delay = new GlideTime();
	delay.setValue("00:00:10");
	when.add(delay);
	gs.eventQueueScheduled("YOUREVENTNAMEHERE", current, email, "", when);

 

That should do it man. So maybe not the prettiest way but if you jump through theses couple of hoops it should work. 

This may be cool too i didnt know it had the document builder to help with formatting. If you have some trouble getting the html the way you want it and this lets you create a table on the fly using that generator. 

 

Link Here