Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to created related attachment related list on change request table that should contain the attachment attached on approval request

rupali18
Kilo Contributor

I want to create the related list(Relationship) on change table as approval-attachment and it should shows the attachments attached while approving and rejecting the change request. 




1 ACCEPTED SOLUTION

this worked for me

CHG -> has 1 file

Approval record has 1 file

Total 2 records shown in related list

(function refineQuery(current, parent) {

	// Add your code here, such as current.addQuery(field, value);

	// get the approval records for this CHG
	var app = [];
	var gr = new GlideRecord("sysapproval_approver");
	gr.addQuery("sysapproval", parent.getUniqueValue());
	gr.addQuery("source_table", "change_request");
	gr.query();
	while(gr.next()) {
		app.push(gr.getUniqueValue());
	}

	app.push(parent.sys_id.toString());
	current.addQuery("table_sys_id" ,"IN", app.toString());

})(current, parent);

find_real_file.png

find_real_file.png

 

find_real_file.png

Please mark my response as correct and close the thread

Regards
Ankur

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

View solution in original post

16 REPLIES 16

Sunny Khandelwa
Kilo Guru

Hi Rupali,

 

You can create a new Relationship for the same.

Applies to table: Change Request

Quieres from table: Attachment

:

var sysIds = '';
var query = 'sysapproval=' + parent.sys_id;
var grApp = new GlideRecord('sysapproval_approver');
grApp.addEncodedQuery(query);
grApp.query();
while (grApp.next()) {
sysIds += grApp.sys_id + ',';
}
current.addQuery("table_sys_id", "IN", sysIds);

 

Below screenshot for reference

find_real_file.png

 

Regards,

Sunny Khandelwal

 

Hey Thanks Sunny,
Related list issue is solved.

I have another one doubt or issue,
if we use the glidesysattachment.copy method to copy attachment it creates the duplicate records so we prefer the related list solution. 
Earlier
I was trying to copy the attachment using glidesysattachment.copy() and then I am comparing with the attachment table , but in the code if we provide specific file name then only the code gets run otherwise not. Please help me out.

Below is the code I am running on approval table:

If I comment the line - gr.addQuery('file_name','testdata.txt'); then  creates the duplicate file on change record

find_real_file.png