Help needed with Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2024 01:25 PM
Hello Dev's
I have below script which I am using in a business rule (on insert and update when Parent changes) on alm_hardware table.
Requirement: We have created a new field "Asset Count" on custom "Carton" table which stores the count of assets loaded on a carton. Assets have a field called "Parent" which stores the carton name.
Carton are of 2 types: Computer and Consumables.
With computer cartons, asset count is coming accurately. So if we are loading 7 computer assets on a computer carton, asset count comes up as 7 based on script below. If we are moving assets from 1 carton to another, that calculation is also happening fine.
Issue is with Consumable carton. So for consumables, we dont have asset tag or serial number. Like Monitors, or docking stations. They have quantity. So a consumable carton can have 7 monitors, however asset record just shows 1 record, however the quantity field is 7. But in my asset count calculation, it is coming as 1, instead of 7.
How can i modify the below BR to include this logic in real time? So if quantity on asset changes, check the model category of asset, if its consumable, then asset count = quantity..
---------------------------------
(function executeRule(current, previous /*null when async*/) {
function updateAssetCount(cartonId) {
var assetCount = 0;
var astGr = new GlideRecord('alm_asset');
astGr.addQuery('parent', cartonId);
astGr.query();
assetCount = astGr.getRowCount();
var cartonGr = new GlideRecord('x_hamp_carton');
if (cartonGr.get(cartonId)) {
cartonGr.asset_count = assetCount;
cartonGr.update();
}
}
// Decrement count on previous parent pallet if exists
if (previous.parent) {
updateAssetCount(previous.parent);
}
// Increment count on current parent pallet if exists and is not blank
if (current.parent) {
updateAssetCount(current.parent);
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2024 01:34 PM
Hi @dvelloriy ,
Can you please try the following:
(function executeRule(current, previous /*null when async*/) {
function updateAssetCount(cartonId) {
var assetCount = 0;
var astGr = new GlideRecord('alm_asset');
astGr.addQuery('parent', cartonId);
astGr.query();
while (astGr.next()) {
// Check if the asset is a consumable
if (astGr.model_category == 'Consumable') {
assetCount += parseInt(astGr.quantity);
} else {
assetCount += 1; // Count as 1 for non-consumables
}
}
var cartonGr = new GlideRecord('x_hamp_carton');
if (cartonGr.get(cartonId)) {
cartonGr.asset_count = assetCount;
cartonGr.update();
}
}
// Decrement count on previous parent pallet if exists
if (previous.parent) {
updateAssetCount(previous.parent);
}
// Increment count on current parent pallet if exists and is not blank
if (current.parent) {
updateAssetCount(current.parent);
}
})(current, previous);
If my response helped you, please click on "Accept as solution" and mark it as helpful.
- Saloni
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2024 12:58 PM
Hi Saloni
Can we use glideaggregate for sum function? It is recommended for such requirements.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2024 01:24 PM
Hi @dvelloriy
yes you can use . Below is the updated code:
function updateAssetCount(cartonId) {
var assetCount = 0;
// Using GlideAggregate to sum quantities of consumables
var astGr = new GlideAggregate('alm_asset');
astGr.addQuery('parent', cartonId);
astGr.addQuery('model_category', 'Consumable');
astGr.addAggregate('SUM', 'quantity');
astGr.query();
while (astGr.next()) {
assetCount += parseInt(astGr.getAggregate('SUM', 'quantity'));
}
// Count non-consumables as 1 each
var nonConsumableGr = new GlideAggregate('alm_asset');
nonConsumableGr.addQuery('parent', cartonId);
nonConsumableGr.addQuery('model_category', '!=', 'Consumable');
nonConsumableGr.addAggregate('COUNT');
nonConsumableGr.query();
if (nonConsumableGr.next()) {
assetCount += parseInt(nonConsumableGr.getAggregate('COUNT'));
}
// Update the asset_count field in x_hamp_carton record
var cartonGr = new GlideRecord('x_hamp_carton');
if (cartonGr.get(cartonId)) {
cartonGr.asset_count = assetCount;
cartonGr.update();
}
}
———————————————-
Please consider marking my reply as Helpful👍 and/or Accept Solution☑️, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2024 01:37 PM
Hi @dvelloriy,
please check below script:
(function executeRule(current, previous /*null when async*/) {
function updateAssetCount(cartonId) {
var assetCount = 0;
var astGr = new GlideRecord('alm_asset');
astGr.addQuery('parent', cartonId);
astGr.query();
while (astGr.next()) {
if (astGr.category.getDisplayValue() === 'Consumable') {
assetCount += parseInt(astGr.quantity);
} else {
assetCount++;
}
}
var cartonGr = new GlideRecord('x_hamp_carton');
if (cartonGr.get(cartonId)) {
cartonGr.asset_count = assetCount;
cartonGr.update();
}
}
// Decrement count on previous parent pallet if exists
if (previous.parent) {
updateAssetCount(previous.parent);
}
// Increment count on current parent pallet if exists and is not blank
if (current.parent) {
updateAssetCount(current.parent);
}
})(current, previous);
Thank you, please make helpful if you accept the solution.