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

Grab individual attachment names from a record as variables

-Andrew-
Kilo Sage

Hello fellow developers!

I'm working on some interesting tasks around attachments but am stumped on how to get the attachments associated with a record as individual variables. For example;

Incident INC12345 contains two attachements

var firstValue = first attachment found

var secondValue = second attachment found

 

Initially i thought best to use a loop to iterate over each attachment found and add the sys_id of each attachment to the list using the push() method. Then, assign the variables the elements of the list respectively using array indexing:

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', 'TABLENAME');
gr.addQuery('table_sys_id', 'SYSID'); 
gr.query();

var list = [];
while (gr.next()){
    list.push(gr.sys_id);
}
var firstValue = list[0];
var secondValue = list[1];
gs.info("First value found in the list: " + firstValue);
gs.info("Second value found in the list: " + secondValue);

This returns the first sys_id in both variables

 

I then tried joining the elements of the list, which works:

gs.info(list.join('\n'));

 

I then tried with a third attachment, using the next() method to the third attachment and retrieve it's name:

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', 'TABLENAME'); 
gr.addQuery('table_sys_id', 'SYSID'); 
gr.query();

if (gr.next()) {
    var firstAttachment = gr.getValue('file_name');
    gs.info("First Attachment: " + firstAttachment);
}

if (gr.next()) {
    gr.next();
    if (gr.next()) {
        var thirdAttachment = gr.getValue('file_name');
        gs.info("Third Attachment: " + thirdAttachment);
    }
}

 

 

Despite my efforts i'm still unable to achieve the desired results.

 

Background:

I have a custom table that we use to quality check attachments on records. On said table there is a reference field to the incident table. Once this field is populated it should then get the attachment names and populate the respective string field (Attachment name 1, Attachment name 2, Attachment name 3, etc.)

 

Has anyone got any ideas?

 

Thank you!

1 ACCEPTED SOLUTION

BharathChintala
Mega Sage

@-Andrew- 

expected mistake always use .toString while pushing values to a array or else all time same sysid will push.

 

list.push(gr.sys_id.toString());

  Thanks,

Bharath

If my inputs have helped with your question, please mark my answer as accepted solution, and give a thumb up.
Bharath Chintala

View solution in original post

2 REPLIES 2

BharathChintala
Mega Sage

@-Andrew- 

expected mistake always use .toString while pushing values to a array or else all time same sysid will push.

 

list.push(gr.sys_id.toString());

  Thanks,

Bharath

If my inputs have helped with your question, please mark my answer as accepted solution, and give a thumb up.
Bharath Chintala

Awesome! Thank you very much! Understood.