How to loop through records, but also perform action when there are no records
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2018 12:26 AM
Hi all,
I feel I may just be confusing myself.
I have a glide record query I need to check a table called u_challenge. what I am trying to do is check if there are any challenges for an asset. If there are none, create one. If there are, I have some further queries I need checked. So basically, I need: if(challenge exisits){
loop through records and check further criteria.
}
else{
create new challenge
}
How do I go about implementing this?
This is my script:
var asset = [];
var new_challenge;
var assetGR= new GlideRecord('alm_hardware');
var qc = assetGR.addQuery('substatus','Disabled');
qc.addOrCondition('substatus','Active');
assetGR.addQuery('owned_by' ,'!=', '');
assetGR.addEncodedQuery('model_category.name=Access Token');
assetGR.addQuery('u_last_login','!=','');
assetGR.addEncodedQuery('u_last_loginBETWEENjavascript:gs.beginningOfLast90Days()@javascript:gs.beginningOfLast60Days()');
assetGR.setLimit(40);
assetGR.query();
while(assetGR.next()){
var challengeGR = new GlideRecord('u_challenge');
challengeGR.addQuery('u_asset_tag', assetGR.sys_id);
challengeGR.setLimit(10);
challengeGR.query();
if(challengeGR.next()){
while(challengeGR.next()) {
if(challengeGR.u_challenge_type == 'Use It or Lose It'){
if(challengeGR.sys_created_on > gs.beginningOfLast60Days()){
gs.log('UIOLI challenge created within last 60 days ' + assetGR.u_token_id);
new_challenge = false;
}
else{
new_challenge = true;
gs.log('No UIOLI challenges within last 60 days for ' + assetGR.u_token_id);
}
}
else{
if(challengeGR.u_challenge_status !='Cancelled' && challengeGR.u_challenge_status !='Completed'){
new_challenge = false;
gs.log('No UIOLI challenges within last 60 days, but already exisiting open challenge (not UIOLI) for ' + assetGR.u_token_id + gs.beginningOfLast30Days());
}
else{
new_challenge = true;
gs.log('No UIOLI challenges within last 60 days also, no open non UIOLI challenges for ' + assetGR.u_token_id);
}
}
}
}
else{
new_challenge = true;
gs.log('No challenge exists for ' + assetGR.u_token_id);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2018 03:22 PM
The problem with this
if(challengeGR.next()){
while(challengeGR.next()) {
is that you're stepping into the first record in the if condition and then skipping to the second record in the while condition.
You could do this instead:
...
challengeGR.query();
if(challengeGR.getRowCount() > 0){ // getRowCount returns the number of results without stepping into the records
while(challengeGR.next()) {
...
}
}
else{
...
}