Export of incidents to PDF

Brian Lancaster
Tera Sage

There are 240 incidents that we need to PDF and send do someone.  Instead of doing it one by one as we need the export to be of the form is there a way to export each incident as a PDF via a script?

1 ACCEPTED SOLUTION

Brian Lancaster
Tera Sage

I think I figured out something in ServiceNow so that it create the PDFs and attaches them to record I created in another table as we do not want them attached to the incident record.  I'm also able to set the name so it is not just incident.pdf.  Here is what I did.

1.  Created a custom table with only 1 string value called number.  (this will get the number from the incident table).

2.  Created 2 system properties with the admin user name and password.

3.  Created a scheduled that is set to  is run on demand with the following code. 

var gr = new GlideRecord('incident');
gr.addEncodedQuery('query string');
gr.query();
while (gr.next()){
	var incPDF = new GlideRecord ('u_incident_to_pdf');
	incPDF.initialize();
	incPDF.u_number = gr.number;
	var sysID = incPDF.insert();
	
	var rm = new sn_ws.RESTMessageV2();
	rm.setHttpMethod('GET');
	var url = gs.getProperty('glide.servlet.uri') + 'incident'+ '.do?PDF&sys_id=' + gr.sys_id;
	rm.setEndpoint(url);
	// Create new properties and save user id and password of an admin account
	rm.setBasicAuth(gs.getProperty('glide.user.userid'), gs.getProperty('glide.user.password'));
	rm.saveResponseBodyAsAttachment('u_incident_to_pdf',sysID,gr.number+'.pdf');
	var response = rm.execute();
}

Someone still has to go in and manually download each PDF but at least they do not have to rename them and then once they are done there is just a little clean up of deleting that attachment and the record in the table so it can be reused at a latter date and we are not clogging up ServiceNow with the no longer needed attachments.

View solution in original post

7 REPLIES 7

p_espinar
Kilo Guru

Hello,

for doing this you need some programming skills, but here you have the way:

Call URL export programmatically

Un saludo,
Pablo Espinar
Consultant at Econocom Spain

Please mark this response correct if I've answered your question. Thanks!

Thanks I will have to see if anybody in the company knows c#.

Brian Lancaster
Tera Sage

I think I figured out something in ServiceNow so that it create the PDFs and attaches them to record I created in another table as we do not want them attached to the incident record.  I'm also able to set the name so it is not just incident.pdf.  Here is what I did.

1.  Created a custom table with only 1 string value called number.  (this will get the number from the incident table).

2.  Created 2 system properties with the admin user name and password.

3.  Created a scheduled that is set to  is run on demand with the following code. 

var gr = new GlideRecord('incident');
gr.addEncodedQuery('query string');
gr.query();
while (gr.next()){
	var incPDF = new GlideRecord ('u_incident_to_pdf');
	incPDF.initialize();
	incPDF.u_number = gr.number;
	var sysID = incPDF.insert();
	
	var rm = new sn_ws.RESTMessageV2();
	rm.setHttpMethod('GET');
	var url = gs.getProperty('glide.servlet.uri') + 'incident'+ '.do?PDF&sys_id=' + gr.sys_id;
	rm.setEndpoint(url);
	// Create new properties and save user id and password of an admin account
	rm.setBasicAuth(gs.getProperty('glide.user.userid'), gs.getProperty('glide.user.password'));
	rm.saveResponseBodyAsAttachment('u_incident_to_pdf',sysID,gr.number+'.pdf');
	var response = rm.execute();
}

Someone still has to go in and manually download each PDF but at least they do not have to rename them and then once they are done there is just a little clean up of deleting that attachment and the record in the table so it can be reused at a latter date and we are not clogging up ServiceNow with the no longer needed attachments.