Asset Record Quantity Update
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2025 02:26 AM
Hi All
We are using assets with ServiceNow and need to achieve the following for an asset that has the quantity of x20
I have 2 fields - 1 is Quantity and the other 1 is Stock Remaining,
What I need to be able to do is on an asset calculate Quantity - Asset Tasks assigned to this asset = Stock Remaining
Ideally we would also be able to apply this only to Assets with the Model Category of Peripheral
This is a rough script in a business rule that doesn't work
function onAfter(current, previous) {
// Only proceed if state has changed to "Allocated" (value = '3')
if (current.state == '3' && previous.state != '3') {
gs.info("Task moved to Allocated state. Processing assets...");
// u_assets_used is a Glide List → split into array of sys_ids
var assetIds = current.u_assets_used.toString().split(',');
for (var i = 0; i < assetIds.length; i++) {
var assetId = assetIds[i].trim();
if (!assetId) continue;
var asset = new GlideRecord('alm_asset');
if (asset.get(assetId)) {
var remainingQty = parseInt(asset.getValue('u_stock_remaining'), 10);
if (!isNaN(remainingQty) && remainingQty > 0) {
asset.setValue('u_stock_remaining', remainingQty - 1);
asset.update();
gs.info("Decremented asset " + asset.name + " to " + (remainingQty - 1));
} else {
gs.info("Asset " + asset.name + " has no remaining stock or invalid quantity.");
}
} else {
gs.info("Asset not found with sys_id: " + assetId);
}
}
} else {
gs.info("Task state is not newly set to Allocated (3), skipping.");
}
}
0 REPLIES 0