i want to create a one PDF file for incident record and that pdf file will attach to same incident record.

Sanjay Bagri1
Tera Guru

Hi 

i want to create a one PDF file for incident record and that pdf file will  attach to same incident record. Is there any way to do.

Please can you suggest me where i am wrong,

This is the Script Which i am Using in Workflow after insert business rules: 

(function executeRule(current, previous /*null when async*/) {

	// 	attachment.write('tablename', recordsysid, fileName, content_type, fileDate);

	// "attachment" is the object variable of Attachment() type

	// "tablename" is string variable that contains the name of the table where you want to attach the file.

	// "recordsysid" is the sys id of the record where you want to attach the file

	// "filename" is the file name of the attachment

	// "content_type" is the type of content in the file (usually for pdf it is 'text/text')

	// "fileData" contains the data that you want to put in the file.

	// Example: Business Rule to run after insert

	var tableName = "incident"; //incident Item Table
	gs.addInfoMessage("Table Name :"+tableName);
	var recordsysid = current.sys_id; //sys id of the record
	gs.addInfoMessage("RecordSysID :"+recordsysid.getDisplayValue()   +"   ==  "+recordsysid);
	var fileName = current.number+" FormData.pdf";
	gs.addInfoMessage("file Name :"+fileName);
	var content_type = 'text/pdf';
	gs.addInfoMessage("content_type :"+content_type);


	var fileData=""; // Declare an empty string 

	fileData+=current.number+" "+current.short_description+"</ b>";           // current access the varibles in the record

	//fileData+=current.variables.u_requested_for.getDisplayValue()+"</ b>";    // current.variables access the variables or fields in the form or catalog Item
	fileData+=current.state+"</ b>";
	fileData+=current.variables.u_department.getDisplayValue();

	gs.addInfoMessage("File Data  :"+fileData);

	var attachment = new Attachment();

	attachment.write('tablename', recordsysid, fileName, content_type, fileDate);


})(current, previous);

I am getting these values but not attaching the Attachments in the records : 

find_real_file.png

I am getting some help from one thread but its not working

https://community.servicenow.com/community?id=community_article&sys_id=d947860cdbbe1380e0e80b55ca961...

Thanks in advance.

1 ACCEPTED SOLUTION

rest message should also fulfill this requirement. 

 

try with below code. 

Script include:

 

var CreateAttachments = Class.create();
CreateAttachments.prototype = {
	initialize: function() {
	},

	creatAttch: function(name,contentType, data){



		var attCreator = new GlideRecord('ecc_queue');
		attCreator.agent = "AttachmentCreator";
		attCreator.topic = "AttachmentCreator";
		attCreator.name =   "pdf" + ":" + contentType;
		attCreator.source = current.getTableName()+":"+current.sys_id;
		attCreator.payload = data;
		attCreator.insert();
	},



	type: 'CreateAttachments'
};

 

Business rule:

 

(function executeRule(current, previous /*null when async*/) {



	var fileName = current.number+" FormData.pdf";
	var fileData=""; // Declare an empty string 

	fileData+=current.state+"</ b>";
	fileData+=current.short_description.getDisplayValue();
	var ab = new CreateAttachments().creatAttch(fileName,'application/pdf', fileData);


})(current, previous);

 

 

View solution in original post

17 REPLIES 17

Thanks Harsh for quick response.

i tried to by ui action its creating pdf file with full information after click operation but its not attaching the file in same record. 

function createPDFESS() {
var sysparm_table = g_form.getTableName();
var sysparm_sys_id = g_form.getUniqueValue().toString();
var instanceName='https://'+location.hostname+'/';
    var url = instanceName+sysparm_table + '.do?PDF&sys_id=' + sysparm_sys_id;

g_navigation.openPopup(url);
//attachment.write(sysparm_table,sysparm_sys_id);
    
    
    

this above code i written in ui action .

Hi @Harshvardhan ,

My thread : https://community.servicenow.com/community?id=community_question&sys_id=2a87bfd9dbb459100d48db85ca96198e

I m trying to get Survey user response in PDF. I have tried below REST code.

PDF was attached but there was no data in pdf. given attachment screenshot and attachment please check it once.

Please suggest  and provide me some solution.

var gr = new GlideRecord('asmt_assessment_instance');
    if (gr.get('982a2c44d7211100158ba6859e6103a4')) {
        var rm = new sn_ws.RESTMessageV2();
        rm.setHttpMethod('GET');
        var url = gs.getProperty("glide.servlet.uri") + 'assessment_take2' + '.do?PDF&' + 'sys_id=' + gr.sys_id + 'sysparm_assessable_sysid=' + gr.sys_id + '&sysparm_assessable_type=' + gr.getValue('metric_type') + '&sysparm_reader_view=true';
        rm.setEndpoint(url);
        gs.addInfoMessage(gs.getMessage("URL :" + url));
        rm.setBasicAuth(gs.getProperty('glide.user.userid'), gs.getProperty('glide.user.password'));
        rm.saveResponseBodyAsAttachment(current.getTableName(), current.sys_id, current.number + ".pdf");
        var response = rm.execute();
        var httpResponseStatus = response.getStatusCode();

        gs.addInfoMessage(gs.getMessage("Response :" + response));
        gs.addInfoMessage(gs.getMessage("Status :" + httpResponseStatus));


    }
 

Sateesh Kumar 2
Kilo Guru
Hello, Try the below attachment.write(tableName, recordsysid, fileName, content_type, fileDate); Instead of the line attachment.write('tablename', recordsysid, fileName, content_type, fileDate);

Omkar Mone
Mega Sage

Hi Sanjay,

 

Hope you're doing well, i see in the attachment.write function, in the first parameter you have passed 'tablename' as a string, it should be the object withouth ''.

 

Try this once - 

attachment.write(tablename, recordsysid, fileName, content_type, fileDate);

Its attaching the pdf file but when i am opening the file then its showing this error:

find_real_file.png