how to get current sysid of the attachment in the change form

Manikandan Subr
Kilo Guru

Hi,

We have list of users in the CR form. We would attach an excel file with the list of Users in the first column. The requirement is to copy all the users in the attached file to the u_user column which is a list field. 

I am writing the below code in Before Insert business rule of Change Request table. When i test this code in Scripts background it is showing the correct list of users from the excel file. But i would need to specify the sys_id of the attachment. 

1. How would i get the sys_id of the attachment? 

2. As this is an array, this will only set the first value how can set all the values in to the u_user list field?

 

var cr = new GlideRecord('change_request');
cr.addQuery('sys_id','06449478db427300f8d59fd2ca96197a');
cr.query();
while(cr.next()){
var parser = new sn_impex.GlideExcelParser(); 
var attachment = new GlideSysAttachment();
// use attachment sys id of an excel file
var attachmentStream = attachment.getContentStream('4cd2acbcdbc27300f8d59fd2ca96197a');

parser.parse(attachmentStream); 

//retrieve the column headers
var headers = parser.getColumnHeaders();  
var header1 = headers[0]; 
//var header2 = headers[1]; 

//print headers
//gs.print(header1); 

while(parser.next()) { 
  var row = parser.getRow(); 
  //print row value for both columns   
  //gs.print(row[header1]) 

cr.u_user = row[header1];
gs.print(cr.u_user.getDisplayValue());
}
}
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Manikandan,

Are you referring to getting the sys_id '4cd2acbcdbc27300f8d59fd2ca96197a' instead of hardcoding?

if yes then query sys_attachment table with current record sys id and you would get the attachment record sys id

var attachmentRecSysId = '';

var gr = new GlideRecord('sys_attachment');

gr.addQuery('table_sys_id', current.sys_id);

gr.query();

if(gr.next()){

attachmentRecSysId = gr.sys_id;

}

Is this not getting all the sys_ids which you want to push to that field?

row[header1];

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Manikandan,

Are you referring to getting the sys_id '4cd2acbcdbc27300f8d59fd2ca96197a' instead of hardcoding?

if yes then query sys_attachment table with current record sys id and you would get the attachment record sys id

var attachmentRecSysId = '';

var gr = new GlideRecord('sys_attachment');

gr.addQuery('table_sys_id', current.sys_id);

gr.query();

if(gr.next()){

attachmentRecSysId = gr.sys_id;

}

Is this not getting all the sys_ids which you want to push to that field?

row[header1];

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

khushboo roy
Tera Contributor

Instead of writing script.

Right click on the record and click on "Copy sys id".

find_real_file.png