Pushing values to Array is always returning the same value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2014 07:26 AM
Hi ,
I have issues in pushing and retriveing the Array values.
in below example i have 3 tasks with different state values (1,2,6)related to a RITM.
in the gs.log below , i am always getting only 6 printed.
can any one help .
var catask;
var cataskcount=0;
var cataskstate = new Array(0);
var request_item = new GlideRecord('sc_req_item');
request_item.addQuery('sys_id', current.request_item);
request_item.query();
while(request_item.next()) {
catask = new GlideRecord('sc_task');
catask.addQuery('request_item', request_item.sys_id);
catask.orderBy('state');
catask.query();
while(catask.next()) {
cataskstate.push(catask.state);
cataskcount++;
}
var statelen = cataskstate.split(",");//if i print stateln.length i am getting undefined
for(var i=0;i<cataskstate.length ;i++){
gs.log("cataskstate[i] state values"+cataskstate[i]);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2014 07:40 AM
Whenever you're looping and pushing data directly from a query, you need to string() it.
Change
cataskstate.push(catask.state);
to
cataskstate.push(catask.state.toString());
or
cataskstate.push(catask.state + '');
And see if that helps. Whatever is causing this problem I've seen it hundreds of times. If you're looping and you're using an object, if you don't string it you may end up with a list of the same thing (or worse- if you're doing an update on this field, you'll find yourself updating the same record numerous times). I always make sure to toString() or format the object so ServiceNow treats the data separately.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2014 07:53 AM
Hi Murthy,
I recreated your issue and can see that all the tasks are being returned in the same state. I have managed to fix this by amending the following line of code:
cataskstate.push(catask.state);
As the catask.state field contains an integer value, try converting it to string by replacing it with this line of code:
cataskstate.push(String(catask.state));
This worked for me, let me know if you have the same success.
Cheers, Adam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2014 08:04 AM
Thanks Aaron and Adam.
it worked for
cataskstate.push(catask.state.toString);
and one more query
from the below line of code is there a possible way to get the values in order.
like if the array holds the values as 1,5,3,8
i want to retrieve them first 1,next 3 ,next 5 and next 8
for(var i=0;i<cataskstate.length ;i++){
gs.log("cataskstate[i] state values"+cataskstate[i]);
}
tx
Murthy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2014 08:12 AM
Are you trying to sort the array?
You can use .sort();
In your situation, you'd use: cataskstate = cataskstate.sort();