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

@Kruthik M Shiva 

did you check BR ran?

Did you check the query ran?

try these lines

if (gr.next()) {
var attachment = new GlideSysAttachment();
attachment.copy('sys_properties',gr.getUniqueValue() , current.sys_class_name, current.getUniqueValue());
}

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

Where do I check whether BR and query ran or not? Also, I tried those mentioned lines still it is not attaching the file.

@Kruthik M Shiva 

add gs.info() statements and check whether those came in system logs or not

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

Hi @Ankur Bawiskar ,
The solution worked did slight changes in your script.
Thank you

Hi Kruthik,

 

Could please share the changes you have performed.

Regards,

Kailash.S