The CreatorCon Call for Content is officially open! Get started here.

Retrieve the sys_id of a Filename in sys_attachment from Script ?

Nico12
Mega Sage

Hi,

 

I was wondering if it is possible to retrieve the sys_id of the file name of an attachment record and not the sys_id of the record.

Like when you right-click on it but from a script ?

I need to get it dynamically.

 

Capture d’écran 2023-03-11 à 20.10.09.png

 

Regards,

1 ACCEPTED SOLUTION

Saurabh Gupta
Kilo Patron

Hi,
You can find it using gliderecord api on sys_attachment table.

 

 


Thanks and Regards,

Saurabh Gupta

View solution in original post

2 REPLIES 2

Saurabh Gupta
Kilo Patron

Hi,
You can find it using gliderecord api on sys_attachment table.

 

 


Thanks and Regards,

Saurabh Gupta

Paul Deonarine3
Tera Expert

Yes, it is possible to retrieve the sys_id of an attachment file name in ServiceNow using scripting. You can use the GlideRecord API to query the sys_attachment table and retrieve the sys_id of the attachment based on the file name.

 

// Define the file name of the attachment
var fileName = "example.txt";

// Query the sys_attachment table to retrieve the sys_id of the attachment
var gr = new GlideRecord('sys_attachment');
gr.addQuery('file_name', fileName);
gr.query();
if (gr.next()) {
  var sysId = gr.getValue('sys_id');
  gs.info("Attachment sys_id for file " + fileName + " is " + sysId);
} else {
  gs.warn("No attachment found with file name " + fileName);
}

 

In this example, we define the file name of the attachment we want to retrieve the sys_id for. We then create a new GlideRecord object for the sys_attachment table and add a query to find the record with a matching file name. We then execute the query and check if any records were returned. If a record is found, we retrieve the sys_id field value and log it to the console. If no records are found, we log a warning message.