how to fetch URL of an attachment

Shantanu K
Tera Contributor

Hello Expert,

 

I tried using below script but no luck.

 

 gs.info('attachment test ');
    
    var filename;
 
    var att = new GlideRecord('sys_attachment'); // Correct the GlideRecord instantiation
    att.addQuery('table_name', 'incident');
    att.addQuery('table_sys_id', 'passing_sys_id');
    att.query();
 
 
    if (att.next()) {
       
filename = att.file.toString(); // Use the array you created, and fix the variable name
        gs.info("Attachment URL: " + filename);
    }
1 ACCEPTED SOLUTION

manjusha_
Kilo Sage

@Shantanu K 

 

On sys_attachment table fileName is field whose backend name is file_name 

In your code ,you used fatt.file.toString() instead of att.file_name.toString(); and due to which your code is not working 

 

Just use correct backend name of field Filename as given above .

 

By making above changes your code will give only the file name eg-text.pdf or text2.png

 

To get link of the attachment record use below code

var id;

var att = new GlideRecord('sys_attachment'); // Correct the GlideRecord instantiation
    att.addQuery('table_name', 'incident');
    att.addQuery('table_sys_id', 'passing_sys_id');//pass sys_id of the incident record
    att.query();
    if (att.next()) {
       id = att.sys_id.toString();
            }
Create Link-

<a href="https://<your_instance>.service-now.com/sys_attachment.do?sys_id=id&view=true" target="_blank">My Link</a>

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact

Thanks,

Manjusha Bangale

 

View solution in original post

3 REPLIES 3

Nayan  Dhamane
Kilo Sage
Kilo Sage

Hello @Shantanu K ,

Please try the below script in your query

 

filename = 'https://yourinstancename.service-now.com/nav_to.do?uri= .do?sys_id=' + att.sys_id.toString();

 

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

@Shantanu K If my answer helped you can mark my solution correct as well as servicenow allows multiple correct answers.

 

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

manjusha_
Kilo Sage

@Shantanu K 

 

On sys_attachment table fileName is field whose backend name is file_name 

In your code ,you used fatt.file.toString() instead of att.file_name.toString(); and due to which your code is not working 

 

Just use correct backend name of field Filename as given above .

 

By making above changes your code will give only the file name eg-text.pdf or text2.png

 

To get link of the attachment record use below code

var id;

var att = new GlideRecord('sys_attachment'); // Correct the GlideRecord instantiation
    att.addQuery('table_name', 'incident');
    att.addQuery('table_sys_id', 'passing_sys_id');//pass sys_id of the incident record
    att.query();
    if (att.next()) {
       id = att.sys_id.toString();
            }
Create Link-

<a href="https://<your_instance>.service-now.com/sys_attachment.do?sys_id=id&view=true" target="_blank">My Link</a>

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact

Thanks,

Manjusha Bangale