Whats wrong with my script's if condition?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2024 09:10 AM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2024 09:21 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2024 09:22 AM
Hi @dvelloriy
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2024 09:31 AM
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