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 ,

As you already have the RITM number, you don't need to glide on sc_req_item table just for getting the sys_id.

 

You can directly glide on sc_task table with RITM number using dot walking on RITM number column. 

Use the below code in back-group script using the correct RITM number, and share the result

 

 

var taskList = [];
var sctask = new GlideRecord('sc_task');
      sctask.addQuery("request_item.number", "<RITM_Numer>" ); //  replace the correct RITM Number.
      sctask.query();
while(sctask.next()){

 // add sctask numbers in the arraylist  
 taskList.push(sctask.number); //use array's push method to add items, task numbers will add in array.
// add sctask glide resultset object in the arrayList
 taskList.push(sctask); //use array's push method to add result-ser object 

}

you can update this script, and comment either of taskList.push() line based on requirement sctask numbers or result-set objects.

 

 

 


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

Minh Huy Lam Qu
Giga Sage

It'll be better if you use JavaScript built-in function like the array.push().

 

You can review this one https://pepa.holla.cz/wp-content/uploads/2016/08/Effective-JavaScript.pdf

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(','));

 

Thanks @OlaN , for giving importance of pass by value & pass by reference. I recalled my Java/J2EE days.


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