Latest attachment not visible on the request after removing ZZ_YY from attachment name

Sampathr382
Tera Contributor

Hi Everyone,

I have a requirement of making attachment mandatory based on the value(Select-box) in one of the catalog item. So, instead of generic attachment(paper clip icon) I have used attachment variable in catalog item. Please refer below image for the variable

Sampathr382_0-1739890772039.png

 

 


Whenever I upload the attachment and submit the form and the attachment would be named with prefix "ZZ_YY".
So, to avoid this I have used After insert BR and wrote a script like this

Script:

(function executeRule(current, previous /*null when async*/) {

    // Add your code here

    var gr = new GlideRecord("sys_attachment");

    gr.addQuery("table_name""ZZ_YY" + current.getTableName());

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

    gr.query();

    if (gr.next()) {

        gr.table_name = current.getTableName();

        gr.update();

       

    }

})(current, previous);


There will be no issue if I attach/upload a proper file for the first attempt in the variable but incase If I want to update the file in attachment variable using upload option and submit the form.

Sampathr382_1-1739890772077.png

 

 

I am able to see the previously attached file not the latest one in request form.

Can anyone please help on this issue?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Sampathr382 

use this -> orderByDesc()

(function executeRule(current, previous /*null when async*/) {

    // Add your code here

    var gr = new GlideRecord("sys_attachment");

    gr.addQuery("table_name", "ZZ_YY" + current.getTableName());
    gr.orderByDesc('sys_created_on');
    gr.addQuery("table_sys_id", current.sys_id);

    gr.query();

    if (gr.next()) {

        gr.table_name = current.getTableName();

        gr.update();

       

    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

3 REPLIES 3

Sampathr382
Tera Contributor

@Ankur Bawiskar  Any help on this issue?

Ankur Bawiskar
Tera Patron
Tera Patron

@Sampathr382 

use this -> orderByDesc()

(function executeRule(current, previous /*null when async*/) {

    // Add your code here

    var gr = new GlideRecord("sys_attachment");

    gr.addQuery("table_name", "ZZ_YY" + current.getTableName());
    gr.orderByDesc('sys_created_on');
    gr.addQuery("table_sys_id", current.sys_id);

    gr.query();

    if (gr.next()) {

        gr.table_name = current.getTableName();

        gr.update();

       

    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Sampathr382
Tera Contributor

Thank You @Ankur Bawiskar it worked.