Requery results from the beginning after completed gr.next()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-19-2013 01:55 PM
I am trying to find a method of going back to the beginning of a gr.query() after I have already looped through it using while(gr.next()).
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('sysapproval', current.sys_id);
gr.query();
var approved = false;
while(gr.next()) {
if(gr.state == 'approved') {
approved = true;
break;
}
}
//Want to go back to the beginning of same query again here.
while(gr.next() && approved == false) {
if(gr.state == 'requested' && current.state == 3) {
gr.state = 'not_required';
gr.comments = current.number + ' completed before approvals were completed.';
gr.update();
}
}
I came across restoreLocation() but am unsure where I would implement this in this specific example to go through the query of approval records again IF no approval records are in the approved state. I think this might be my solution but am unsure.
Any thoughts on how to cleanly and efficiently do this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-20-2013 12:20 PM
I suggest the following.
Since you will do nothing if the current.state is not 3, put that into your BR conditions.
condition: current.state == 3
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('sysapproval', current.sys_id);
gr.addQuery('state','approved');
gr.query();
if (gr.hasNext()) {
gr = new GlideRecord('sysapproval_approver');
gr.addQuery('sysapproval', current.sys_id);
gr.addQuery('state','requested');
gr.query();
while (gr.next()) {
gr.state = 'not_required';
gr.comments = current.number + ' completed before approvals were completed.';
gr.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-21-2013 08:32 AM
Sorry to say, but I'm actually doing this as the last step in a workflow the conditions are all in the script.
So whats happening in your improved script is saying (gr.hasNext() ) if there is a record found that is in the approved state, find all approval records for the item that are in the requested state and if found set them to not required.
To say the logic that is desired:
If there is an approval record that is found to be approved (this means that all other attached approval records from the approval group are in the "not_required" state), then do nothing because approvals have been approved and completed.
If they are in the "requested" state this means that approvals have not been completed and no one yet from the group has approved or rejected the item yet. Therefore, set them to no longer required if the current state of the approval records is found to be "requested" and the state of the item/task is "closed".
So this means for the last loop, if there is an approval record to be found that is approved, skip setting the other approval records (approved == false).
I hope this is more understandable now.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-21-2013 09:59 AM
If I understand the desired logic, then maybe I was just missing a "!" in the initial query, and we can wrap the whole thing in a test for closed:
var CLOSED = 3;
if (current.state == CLOSED) {
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('sysapproval', current.sys_id);
gr.addQuery('state','approved');
gr.query();
// Are any approved?
if (!gr.hasNext()) {
// No. Set all requested -> not_required
gr = new GlideRecord('sysapproval_approver');
gr.addQuery('sysapproval', current.sys_id);
gr.addQuery('state','requested');
gr.query();
while (gr.next()) {
gr.state = 'not_required';
gr.comments = current.number + ' completed before approvals were completed.';
gr.update();
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-21-2013 12:43 PM
You are correct so now there is no reason to use
or re-query if you are using
restoreLocation();
(!gr.hasNext())
Thanks!