Need a script to validate the current catalog item is already in the cart list
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2015 08:20 AM
I have selected a catalog item and clicked on "Add to cart" twice. catalog item was added twice to the request.In order to avoid this i need to validate on click on "add to cart item" this should throw a pop-up like "Catalog item is already selected do you want to continue?"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2015 08:22 AM
This is a bit tricky but doable. But my question: What if the user wants to add the Catalog Item twice?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2015 08:23 AM
we will allow him.just we need to remind him that he was selecting again...bcoz sometimes user by mistake clicked on the button twice and it will be created as request for two items..

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-20-2016 01:26 PM
Cart items also have a quantity.... you could set the quantity=2 rather than selecting an item twice. In fact, if you're using this script to catch when they select an already-added item, you could set the quantity instead for them.
-Brian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2015 09:37 AM
Here is the code : catalog_cart_modified.xml
If you want to abstract what's happening, you can just create a new UI Macro called catalog_cart_modified and then paste in the code. Then go to your catalog item and change the CART to point to the new UI macro you created. It will work just fine.
If you want to know what's really happening, read on... At the end there is a very important caveat about performance which I suggest you to modify.
We will start with the question, what really happens when you click on Add to Catalog button. For this you will have to open catalog_cart_default, and arrive at the following lines:
<tr>
<td colspan="2" class="order_buttons">
<a class="request_catalog_button_with_icon" id="add_to_cart_button" href="#" onclick="if ($$('.order_buttons .disabled_order_button').length == 0) { addToCart(); } else { alert('${gs.getMessage('Please wait - price being updated')}'); } return false;" title="${gs.getMessage('Add to Cart')}">
<table><tr><td>
<img src="images/button_cart.gifx"/>
</td><td class="text_cell">
${gs.getMessage('Add to Cart')}
</td></tr></table>
</a>
</td>
</tr>
You will see that the onClick is on a function called `addToCart`, that's where you will have to add the validation to see if there is already a Cart Item present. If there is already one Cart Item then given an alert to the user. Here is the code that does that.
var cart = -1;
var item_sysID = gel("sysparm_id").value;
//get the user's cart.
var cg = new GlideRecord("sc_cart");
cg.addQuery('user', g_user.userID);
cg.query();
if (cg.next()) {
cart = cg.sys_id;
alert(cart);
alert(guid);
}
if (cart != -1) {
var gr = new GlideRecord('sc_cart_item');
gr.addQuery('cart', cart);
gr.addQuery('cat_item', item_sysID);
gr.query();
if (gr.next()) {
var result = confirm('You already have an item of same type on the form. Do you want to continue?');
if (!result) {
return false;
}
}
}
There is one important thing that you notice about this line of code: `get("sysparm_id").value`. This gives the sys_id of the Catalog Item you are on.
If you have any other questions about this, please do post here.
Now to the important caveat :
You had already noticed that I have used two Glides in a Client side code, which is a bad bad bad way of doing things. I had to do what I had to do, because I'm short on time.
So the suggestion that I have for you is, to convert these Glides into a Script Include, and invoke the Script Include using GlideAjax : GlideAjax - ServiceNow Wiki
I hope that helps with what you are trying to do. Also the code is working in demo013.service-now.com/login.do?