Configure Catalog SLA for the maximum quantity per business days

anuragkumbh
Tera Contributor

Please suggest how we can configure the catalog sla for maximum quantity per business days.

 

SLA for the catalog items are already fixed but unable to configure the catalog items for maximum quantity per business days.

 

Please provide the logic or steps to fix this.

1 REPLY 1

Shubham_Jain
Mega Sage

@anuragkumbh

 

 

  • Create a Custom Field on the Catalog Item Record:

    • Add a custom field to track the number of days within which the maximum quantity applies (e.g., Max Quantity per Day).
    • You can create a field called max_quantity_per_day on the catalog item form (sc_cat_item table).
  • Use a Business Rule or Script Include:

    • To limit the quantity ordered within a certain number of business days, you will need to write logic that checks whether the current user has already ordered the maximum quantity within the set timeframe.
  • Script Logic for Checking Past Orders:

    • In a Business Rule (set to run before the catalog item request is submitted), you can query the sc_req_item table to see if the user has exceeded the maximum allowed quantity within the defined business days.

    Here's an example of a script that you could use in a Business Rule:

 

// Get catalog item, requested for user, and max quantity
var catalogItem = current.cat_item;
var requestedFor = current.requested_for;
var maxQuantity = catalogItem.max_quantity_per_day; // Ensure you have this field on the catalog item

// Get the date N business days ago (replace N with your desired number of business days)
var nBusinessDays = 5; // Example for 5 business days
var glideDate = new GlideDateTime();
glideDate.addBusinessDaysUTC(-nBusinessDays);

// Query sc_req_item table to check past orders
var reqItemGR = new GlideRecord('sc_req_item');
reqItemGR.addQuery('cat_item', catalogItem);
reqItemGR.addQuery('requested_for', requestedFor);
reqItemGR.addQuery('sys_created_on', '>=', glideDate);
reqItemGR.query();

var totalQuantity = 0;
while (reqItemGR.next()) {
    totalQuantity += parseInt(reqItemGR.quantity, 10);
}

// Check if the total quantity exceeds the max allowed
if (totalQuantity + parseInt(current.quantity, 10) > maxQuantity) {
    gs.addErrorMessage('You have exceeded the maximum allowed quantity for this item within the past ' + nBusinessDays + ' business days.');
    current.setAbortAction(true);
}

 

 

Client Script (Optional):

  • You can also add a Catalog Client Script to check the quantity before the form is submitted, so the user gets feedback on the form itself rather than after submission.

Example:

 

 

function onSubmit() {
   var maxQuantity = g_form.getValue('max_quantity_per_day'); // Pull the max quantity
   var quantity = g_form.getValue('quantity'); // Get the quantity entered by the user

   if (parseInt(quantity) > parseInt(maxQuantity)) {
      alert('You are exceeding the maximum quantity allowed per business day!');
      return false; // Prevent form submission
   }

   return true; // Allow submission if the quantity is within the limit
}

 

 

✔️ If this solves your issue, please mark it as Correct.


✔️ If you found it helpful, please mark it as Helpful.



Shubham Jain