getting all the sctask objects of a RITM

deepika46
Tera Contributor

Hello Experts,

 

For a RITM and i want to get all the SCTASK objects the RITM will have. Below is the script i am using. Please suggest me if it is OK or please suggest the best practice way to get it.

 
 
var i=0;
var arr=[];
 

var reqitm=new GlideRecord('sc_req_item');
reqitm.addQuery('number','RITM1788235');
reqitm.query();
while(reqitm.next()) {

var sctask = new GlideRecord('sc_task');
sctask.addEncodedQuery('request_item='+reqitm.sys_id);
sctask.query();
while(sctask.next()){

arr[i]=sctask;
i++;
}
}

gs.print(arr);

1 ACCEPTED SOLUTION

OlaN
Giga Sage
Giga Sage

Hi,

This way of adding data to the array will be causing the classic Pass-by-Reference issue, where you expect different data to be added to the array, but it will add the same data over and over again, since the data is read from memory (a reference to a record) rather than the actual value.

Same thing will happen, in both this script, and also the suggestions given by using the array.push() method.

Both will give the same value over and over again in the given scenario.

The simplest way to rectify this, is to store primitive values (strings, integers) in the array.

Here is an example on how I would do, change it according to your requirements.

 

var arrayData = [];

var scTaskGR = new GlideRecord('sc_task');
scTaskGR.addQuery('request_item', some_sys_id_of_existing_RITM_here); // change as needed
scTaskGR.query();
while (scTaskGR.next()){
    arrayData.push(scTaskGR.getValue('number')); // change if you want to store some other value of the task
}

gs.info('Array data: ' + arrayData.join(','));

 

View solution in original post

5 REPLIES 5

AshishKM
Kilo Patron
Kilo Patron

Hi @deepika46 ,

 

Please "accept solution" to close this thread for others. Hope your issue has been resolved. 

Note: you can select more than one reply as solution.

 

 

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution