Restrict "Add to Cart" adding same Item into cart for same requested for

Piyush27
Tera Contributor

Hi,

 

I have a requirement where I have to restrict the Add to Cart option -

 

- If the Requested For person's USER ID is already added to Cart, in that case, an error message should display saying "Item already added into cart".

- Restrict Add to cart limit upto 10

 

Thanks,

Piyush Kumar

3 REPLIES 3

Rao Vamshi
Kilo Guru

Hi @Piyush27 

  • To achieve the first requirement of restricting the "Add to Cart" option if the requested user's ID is already added to the cart, you can create a business rule.

1. Go to "Business Rules" in the ServiceNow menu and click on "New".

2. Give the rule a name such as "Prevent duplicate items in cart".

3. Set the "When to run" field to "Before" and select the "Insert" and "Update" options.

4. In the "Advanced" section, add the following script:

VamsheedharRao_0-1682328500556.png


5. Save the business rule.

This script will check if the requested user's ID is already in the cart and prevent the item from being added to the cart if it is. It will also show an error message to the user.

  • Likewise, to achieve the second requirement of restricting the "Add to Cart" limit up to 10, you can create another business rule in ServiceNow.

1. Go to "Business Rules" in the ServiceNow menu and click on "New".

2. Give the rule a name such as "Limit items in cart".

3. Set the "When to run" field to "Before" and select the "Insert" option.

4. In the "Advanced" section, add the following script:

VamsheedharRao_1-1682328619023.png

5. Save the business rule.

This script will count the number of items in the cart and prevent the current item from being added to the cart if the cart already has 10 items. It will also show an error message to the user.


Please mark correct/helpful, if your query is addressed and/or answered.

Thanks,
Vamshi

 

Hi @Rao Vamshi , On which table you wrote business rule. Can you plz mention here. Thank you

AyanshN
Tera Contributor

@Rao Vamshi Thanks it is really helpful, but can we approach any another way actually i don't want to use business rule as of now. Is there any another way?