Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Want current table name in a BR ?

Chetan Sondhi
Kilo Sage

Hi

I have written a BR on sys_attachment table to remove existing attachment with same name and update with the latest one . I need this logic on all SN table's so i am looking to get the table name of the form i am creating attachment for .

Please find the code below :


var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', 'LIKE', 'incident');

      //gr.orderBy('table_sys_id');

      gr.orderByDesc('sys_created_on');

      gr.query();

      var lastID = 'not_a_match';

      var lastFile = 'not_a_match';

      while (gr.next()) {

              var isDuplicate = (lastID == gr.table_sys_id) && (lastFile == gr.file_name);

gs.log('Duplicate is' + isDuplicate );

              lastID = gr.table_sys_id;

              lastFile = gr.file_name;

gs.log('Last Id is' + lastID );
gs.log('last file name is' + lastFile );

              gs.log('task is '   + gr.table_sys_id + ' ' + gr.table_name + ' ' + gr.file_name + ' ' + gr.sys_created_on + ' ' + isDuplicate);

              if (isDuplicate)

                      gr.deleteRecord();

      }

I need the table name of the form (in place of incident as shown above) i am creating attachment for , but using current.getTableName() give me sys_attachment table for which this BR is written .

Thanks for your response in advance

Chetan

5 REPLIES 5

Ahmed Drar
Tera Guru

if you would like to run your script globally using for instance script include use the lines below to capture the table name


    var table = this.getTableName();



your code should be something similar to that



    if (table != null){


var gr = new GlideRecord('sys_attachment');


gr.addQuery('table_name',table   );


      //gr.orderBy('table_sys_id');


      gr.orderByDesc('sys_created_on');


      gr.query();



}



Thanks,


Ahmed