Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Asset Quantity Field

adam_cannon_23
Tera Contributor

I have got this script that should do the following-

 

Take the quantity field - asset tasks associated = stock remaining,

Does not currently working, is there any advise/suggestions on this

------------------------------------------------------------------------------------

 

function onAfter(current, previous) {
   // Replace 'asset' with the actual reference field name if different
   if (!current.asset) {
      gs.info("Asset reference is missing.");
      return;
   }

   var asset = new GlideRecord("asset");
   if (asset.get(current.asset)) {

      var remaining = parseInt(asset.getValue("u_stock_remaining"), 10);

      // Optional check: only decrement if remaining > 0
      if (remaining > 0) {
         asset.setValue("u_stock_remaining", remaining - 1);
         asset.update();
         gs.info("Asset stock decremented. New value: " + (remaining - 1));
      } else {
         gs.info("Stock is already 0 or negative. No action taken.");
      }

   } else {
      gs.info("Failed to find asset record for sys_id: " + current.asset);
   }
}
4 REPLIES 4

Voona Rohila
Tera Patron

Hi @adam_cannon_23 

 

Where are you using this logic?


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
5x ServiceNow MVP

This is running as a business rule on the asset task


Ankur Bawiskar
Tera Patron

@adam_cannon_23 

is this an after update business rule? on which table? share some screenshots

Did that business rule trigger?

try this

function onAfter(current, previous) {
    // Replace 'asset' with your actual reference field name if different
    if (current.asset.nil()) {
        gs.info("Asset reference is missing.");
        return;
    }

    var assetGR = new GlideRecord("alm_asset"); // Use your actual asset table
    if (assetGR.get(current.asset.toString())) {

        var remaining = parseInt(assetGR.getValue("u_stock_remaining"), 10) || 0;
        var qty = parseInt(current.quantity, 10) || 1; // Default to 1 if quantity not set

        // Only decrement if enough stock remains
        if (remaining >= qty) {
            assetGR.setValue("u_stock_remaining", remaining - qty);
            assetGR.update();
            gs.info("Asset stock decremented by " + qty + ". New value: " + (remaining - qty));
        } else {
            gs.info("Not enough stock to decrement. Current: " + remaining + ", Requested: " + qty);
        }

    } else {
        gs.info("Failed to find asset record for sys_id: " + current.asset);
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Please see attachd