Limit only upto 5 RITMS to be added into the cart and proceed to checkout

SriramT
Tera Contributor

I have a requirement, where a user should be able to add only 5 RITMS into cart and then proceed to checkout,

I have written a Before Insert business rule for this in my PDI and the business rule is working fine as expected.

But the same business rule is not working in another PDI.

Business rule:

Before

Insert

table: sc_cart_item

conditions: if cart is not empty

 

script:

(function executeRule(current, previous /*null when async*/ ) {
if (!current.cart) {
gs.addErrorMessage("Cart field is null or empty in the current record.");
} else {
gs.addInfoMessage("Current.cart " + current.cart);
}
 
 
var ritmCount = 1; // Start with the current item's quantity
var cartItemsGR = new GlideRecord('sc_cart_item');
// Get the cart items
var crt = current.cart;
gs.addInfoMessage("cart value " + crt);
cartItemsGR.addQuery('cart', crt);
cartItemsGR.query();
 
while (cartItemsGR.next()) {
ritmCount += parseInt(cartItemsGR.quantity || 0);
}
 
// Check if the total RITMs exceed 5
if (ritmCount > 5) {
 
 
// Block adding items to the cart
gs.addErrorMessage("You cannot add more than 5 requests into the cart at the same time.");
current.setAbortAction(true); // Prevent the item from being added
}
 
})(current, previous);
 
In another PDI i am unable to get the value of current.cart.

 

0 REPLIES 0