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.

How to get sys id of the records in the array.

Shyna1
Tera Contributor

Hi ,

I have declared an array which contains the task numbers .

I want to push these records into another array which will contain its respective sys id.

And the list of all the records will be shown when i click the link present in the worknotes.

Unfortunately the code is redirecting to Record not found page upon clicking the link.

 

 var taskMissingDetails = []; 

var count= new GlideRecord('fm_expense_line')

count.addEncodedQuery("task.sys_class_name=sc_task^sys_created_onONLast 3 months@javascript:gs.beginningOfLast3Months()@javascript:gs.endOfLast3Months()");
   if ((access == '' || company_id == '' || employee == '') ) {
             taskMissingDetails.push(count.task.number.toString());
              var uniqueTask = new ArrayUtil().unique(taskMissingDetails);// This is to get the unique records.

 if (uniqueTask.length > 0) {
        
        var cat_item = '97ba964b5de873b80f37f7c28bf56488f'; // Submit a request catalog item
        var cart = new Cart();
        var item = cart.addItem(cat_item);
        cart.setVariable(item, 'requested_for', '1b3ef5440f3942345c1c66d8b1050dfr');
        cart.setVariable(item, 'cmdb_ci', '545d10411eea8a006ee822d8b1051f45'); 
cart.setVariable(item, 'description', tasks are missing attributes Access, Company id.  \n\nList of Tasks - ' + uniqueTask);
 
        var rc = cart.placeOrder();
        var req = rc.sys_id;
        var taskMissingSysId = [];
        taskMissingSysId.push(uniqueTask.sys_id.toString());
        var taskMissing = '[code]<a href="sysparm_query=sys_idIN"+ taskMissingSysId target="_blank">Missing attributes for tasks</a>[/code]';-------> Here i need to create a link which will redirect the user to the list of those tasks.
 var gr = new GlideRecord('sc_req_item');
        gr.addQuery('request', req);
        gr.query();
        while (gr.next()) {
            gr.work_notes = taskMissing; // Update the worknotes with the link to the list of tasks with missing attributes.
            gr.update();
}
 
Any help on this is highly appreciated.
3 REPLIES 3

Vishal Savajia1
Kilo Sage

Hi Shyna, 

Please see below Script  adding sysID,number and random string in single line. Be careful such customization can hammer performance. 

var arrTask = [];
var sysID = []
var task = new GlideRecord('task');
task.query();
while(task.next()) 
{
 
arrTask.push(task.number.toString()); 
}

gs.print(arrTask);

for (i=0;i<arrTask.length;i++)
{
 var Temp =  arrTask[i].toString();
var task12 = new GlideRecord('task');
task12.addQuery('number',Temp)
task12.query();
while(task12.next())

sysID.push(task12.sys_id); 
gs.print(sysID[i].toString() + 'MYLINK' + arrTask[i].toString());
}
}
gs.print(sysID);

John Dahl
Kilo Sage

When given a list of values and you want to retrieve different data for those records, I have found GlideQuery to be exceptionally handy and expressive. In the samples below, given a set of record numbers, I am able to retrieve not only the extra fields from the provided records, but I can dot-walk to related records, gather display values, etc... all without having to script additional queries:

var numbers = [ 'INC0010004', 'INC0010005', 'INC0009009' ];

var arr1 = [],
    arrIDs = new GlideQuery( 'incident' )
  .where( 'number', 'IN',  numbers )
    .select( 'number', 'sys_created_on', 'assigned_to$DISPLAY', 'assigned_to.department$DISPLAY' )
  .reduce( function( arr, inc ){
    arr1.push( inc );
    arr.push( inc.sys_id );
    return arr;
  }, [] );

 

After running this, I get the two arrays shown in the attachments:

  • arrIDs contains only the sys_ids.
  • arr1 contains all selected fields.

 

 

 

 

For some reason, the editor would not allow me to save the screenshots inline.