Whats wrong with my script's if condition?

dvelloriy
Kilo Sage

Hello Dev's

 

Can someone please tell me about the issue with below code snippet?

If condition is not getting executed.

 

var astGr = new GlideRecord('alm_asset');

astGr.addQuery('parent',cartonGr.sys_id);

astGr.query();

if(astGr.model_category == '188323293743100044e0bfc8bcbe45f6') {

    assetCount = astGr.quantity;

} else {

assetCount = astGr.getRowCount();

}

 

 

3 REPLIES 3

Jerick I
Kilo Sage
Kilo Sage

 

Hi Dvelloriy, I am assuming that you have another code above this since you are using cartonGr.sys_id as your unique identifier. Can you add + "" to your code sample below?

 

cartonGr.sys_id+""

astGr.model_category+"" == '188323293743100044e0bfc8bcbe45f6'

 

Note: Using astGr.getRowCount() is not the best practice. It is recommended to use GlideAggregate for this part. I suggest using GlideAggregate rather than directly using getRowCount.

 

Regards, 

Jeff 

Hope this helps. Please mark the answer as correct/helpful based on impact. 

Krushna R Birla
Kilo Sage

Hi @dvelloriy 

 

Can you try this,

 

var astGr = new GlideRecord('alm_asset');
astGr.addQuery('parent', cartonGr.sys_id);
astGr.query();
if (astGr.next()) {
    if (astGr.model_category == '188323293743100044e0bfc8bcbe45f6') {
        assetCount = astGr.quantity;
    } else {
        assetCount = astGr.getRowCount();
    }
}​

 

 

If this solution resolves your query, kindly mark it as the accepted solution and give it a thumbs up.

 

Best Regards,
Krushna Birla

Robbie
Kilo Patron
Kilo Patron

Hi @dvelloriy,

 

Reviewing the script and testing on a PDI to confirm, you're missing a critical line to check whether you have a record - see below with an additional line 4: if(astGr.next()){ as well as an ending brace }:

 

var astGr = new GlideRecord('alm_asset');
astGr.addQuery('parent',cartonGr.sys_id);
astGr.query();
if(astGr.next()){
    if(astGr.model_category == '188323293743100044e0bfc8bcbe45f6') {
        assetCount = astGr.quantity;
    } else {
        assetCount = astGr.getRowCount();
    }
}

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.


Thanks, Robbie