
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2018 11:43 AM
I am trying to limit one particular catalog item to only once in a cart. I have written a before Insert Business rule on sc_cart_item, that is working fine in base view but in portal it is hanging for few minutes and affecting the system performance.
So, I made the business rule inactive and written a OnSubmit catalog client script on the item.The script is working fine in both base and portal view (not allowing duplicate item to the cart) but the issue is when we edit the existing item in the cart and try to update, the script is considering this item as duplicate and not allowing us to update the form in the cart(both base and portal view).
Catalog client script:
function onSubmit() {
//Type appropriate comment here, and begin script below
var req = g_user.userID;
if(window){
//alert("hii..: "+req);
//Always check if there is an New IS User LAN ID in the cart
var ga = new GlideAjax('GetCartItems');
ga.addParam('sysparm_name', 'checkCartItems');
ga.addParam('sysparm_fdt1', req);
ga.getXMLWait();
var the_answer = ga.getAnswer();
if (the_answer == 'true') {
alert("Action Not Allowed: It is not allowed to order more than one New IS User LAN ID as part of the same shopping cart. Please submit the current Request and place another Request for the second item");
return false;
}
}
//if Service Portal
else{
if(g_scratchpad.hasNoItem){
return true;
}
//Always check if there is an New IS User LAN ID in the cart
var ga1 = new GlideAjax('GetCartItems');
ga1.addParam('sysparm_name', 'checkCartItems');
ga1.addParam('sysparm_fdt1', req);
//Callback function to control stop submit asynchronously, if there exists more than one item in cart
ga1.getXML(itemCountCheck);
return false;
}
}
function itemCountCheck(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'true') {
alert("Action Not Allowed: It is not allowed to order more than one New IS Users LAN ID as part of the same shopping cart. Please submit current Request and place another Request for the second item");
return false;
}
g_scratchpad.hasNoItem = true;
g_form.submit();
}
Script Include:
var GetCartItems = Class.create();
GetCartItems.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkCartItems: function(){
var thisRequestor = this.getParameter('sysparm_fdt1');
//var thisRequestor = gs.getUserID();
var count = 0;
var orderGuide = false;
var cart = new GlideRecord('sc_cart');
cart.addQuery('user',thisRequestor);
cart.addQuery('name', 'DEFAULT');
cart.query();
//console.log(thisRequestor);
if (cart.next()) {
//gs.log("The cart is: "+cart.name, "Narendra");
var items = new GlideRecord('sc_cart_item');
items.addQuery('cart',cart.sys_id);
items.query();
while (items.next()) {
//gs.log("the item is: "+items.cat_item.name, "Narendra");
var check = new GlideRecord('sc_cat_item');
check.addQuery('sys_id', items.cat_item);
check.query();
if(check.next()){
//Items to check: New IS User LAN ID
if(check.sys_id == '50a741c20f0825003091918172050e73') {
count++;
//gs.log("the item is New IS USer: "+count, "Narendra");
//Check if this item in included by an order guide or not
if(items.order_guide != ''){
//gs.log("setting order guide to true");
orderGuide = true;
}
}
}
}
}
//Order Guide pre-loads the items in the Cart, therefore we have to check for it
//For regular use of the Catalog, we have to make sure there is no item (i.e., > 0)
if(orderGuide) {
if (count > 0) {
// gs.log("the count is from OG: "+count + "from if", "narendra");
return true;
}
else{
//gs.log("the count is from OG: "+count + "from else", "narendra");
return false;
}
}
else {
if (count > 0) {
//gs.
//("the count is: "+count + "from if", "narendra");
return true;
}
else
return false;
}
},
type: 'GetCartItems'
});
Any suggestions?,
Thanks.
Solved! Go to Solution.
- Labels:
-
Best Practices

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2018 06:02 AM
Hi,
To limit an item to only allow once in cart, I would look to something like this: https://community.servicenow.com/community?id=community_question&sys_id=f82b07eddb5cdbc01dcaf3231f96...
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2018 06:02 AM
Hi,
To limit an item to only allow once in cart, I would look to something like this: https://community.servicenow.com/community?id=community_question&sys_id=f82b07eddb5cdbc01dcaf3231f96...
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-31-2018 07:02 AM
(function executeRule(current, previous /*null when async*/) {
var id = gs.getUserName();
var count = 0;
var gr = new GlideRecord('sc_cart_item');
gr.addQuery('cart.sys_created_by', id);
gr.addQuery('cat_item','50a741c20f0825003091918172050e73');
gr.addQuery('cart.name','DEFAULT' );
gr.orderByDesc('sys_created_on');
gr.query();
while(gr.next()) {
//saving newest record (count=0), deleting the rest.
if(count == 0) {
gr.deleteRecord();
}
count++;