We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Auto Attach document to one field once new record is created.

Kruthik M Shiva
Tera Contributor

Hi All,
I have a requirement to auto attach one document to each record of the table once it is created. Please provide a solution to how to do this and what all needs to be done for this. 
Thanks in advance.

1 ACCEPTED SOLUTION

@Kruthik M Shiva 

then do this

1) create system property of type string and attach your file to that system property

2) Use before/after insert BR on that table and use this script

AnkurBawiskar_0-1683178954996.png

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

	// Add your code here
	var gr = new GlideRecord("sys_properties");
	gr.addQuery("name", "static_file"); // name of property in query
	gr.query();
	if (gr.next()) { 
		new GlideSysAttachment().copy('sys_properties',gr.getUniqueValue() , current.sys_class_name, current.getUniqueValue());
	}

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

15 REPLIES 15

Sandeep Rajput
Tera Patron

@Kruthik M Shiva Create an OnAfter Insert Business rule on your table and use 

GlideSysAttachment.copy(String sourceTable, String sourceID, String targetTable, String targetID);

method to copy an attachment from one record to another.

 

Please refer to thread https://www.servicenow.com/community/developer-forum/copy-attachments-from-one-record-to-another/m-p... and check the solution provided by the author.

 

Hope this helps.

Harshal Aditya
Mega Sage

Hi @Kruthik M Shiva ,

 

Hope you are doing well, please refer below script you can run as an after BR

 

GlideSysAttachment.copy('kb_knowledge',"knowledge record sys_id",'your table name',current.sys_id);

 

In the above example I have saved the attachment to be moved in a kb_knowledge record. You can choose any table you want just replace the code with table name and sys_id. You  can also save the sys_id /table name in a property.

 

Please mark this response as correct or helpful if it assisted you with your question.

Regards,
Harshal

Amit Gujarathi
Giga Sage

Hi @Kruthik M Shiva ,
I trust you are doing great .

To automatically attach a document to each record of a table once it is created, you can follow these steps:

  1. Create a new table in ServiceNow to store the documents you want to attach.

  2. Add a new column to the table you want to attach documents to. This column will store the reference to the attached document.

  3. Write a business rule to trigger on the creation of a new record in the table. This rule should use the GlideSysAttachment class to create a new attachment record and associate it with the newly created record.

// Get the newly created record
var newRecord = current;

// Create a new attachment record
var attachment = new GlideSysAttachment();

// Set the attachment record's table name and record ID
attachment.setTableName(newRecord.getTableName());
attachment.setRecord(newRecord.sys_id);

// Set the attachment record's file name and content
attachment.write("document.pdf", "Hello World!");

// Set the reference to the attachment record on the new record
newRecord.document_attachment = attachment.sys_id;
newRecord.update();

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Ankur Bawiskar
Tera Patron

@Kruthik M Shiva 

where that file will be present?

You can copy using Attachment copy functionality

what script did you start with?

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader