To Fetch sys_id of the catalog item which is present in cart

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-25-2023 10:12 AM
Hi Team ,
I would like to know what code should be written in the server script of widget to get the "sys_id" of the catalog item that is present in the cart.
I have queried the sc_item_option table and i am trying to match the "cart_item" field with sys_id of the catalog item that is available in the cart but unable to do so.
Is there any way to achieve this ?Kindly let me know
If any clarification needed pls revert.
I will paste the screenshot below.
Thanks,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-25-2023 03:58 PM
Hi @diNesh_M have you tried using the sn_cs.CartJS API ? Here is example code to get list of sys_ids
function getCartIds() {
var cart = new sn_sc.CartJS();
var cartItems = cart.getCartItems();
var itemIds = [];
while (cartItems.next()) {
itemIds.push(cartItems.getUniqueValue());
}
return itemIds.join(',');
}
You can find more API details here --> https://developer.servicenow.com/dev.do#!/reference/api/vancouver/server/sn_sc-namespace/c_CartJSSco...
hope that helps
--
Bala G

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-26-2023 05:05 AM
Hi Bala,
Thank you for the response.
I will tell the exact requirement that i am trying to satisfy.
I have a catalog item called "Whitelist IP " and according to the requirement this particular catalog item alone should cascade few variables like Requested for ,What is the business purpose ,etc to the shopping cart widget whenever the user click on "Add to Cart " button in the Sp portal.
So i took a clone copy of the shopping cart widget and made necessary code changes in the Server side and the HTML template for which i have attached the screenshot below.
The reason why i queried "sc_item_option" table is because only in this table all the variable values of the catalog item is getting stored but my problem is since the sys_id is dynamic for the cart_item field i could not match the sys_id in the server script of the widget.
Even i tried the script you posted but i still find it difficult to get the sys_id of the cart item that is present currently .
Any other script would u recommend to achieve this requirement?
(NOTE: I still have not started to work on the hiding part of the requirement ,will be useful if u provide input on this aswell)
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-26-2023 08:04 AM
Hi BalaG,
Thanks for your script as it helped me to cascade the variables from catalog form to Shopping cart widget.
And i do have another part that has be accomplished along with this ,which is just to display the Requested for variable only for a particular catalog item not for all.
Hope some script needs to be written in HTML Template and Server script to achieve .
I have attached the screenshot below which has the yellow part which should be hidden for all catalog items except for one (Whitelist IP ).
Can you provide me your input on this to write the script ?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-25-2023 08:33 PM
Hi @diNesh_M
You can match the current User with the User column in the Shopping Carts [sc_cart] table.
sc_cart is the table which maintains your cart information. Every user will have an entry in the sc_cart table if he has added to the cart at least once
Sample.
//Get Items in Cart of current user
function getAddedItemsFromUserCart() {
var items = [];
var grCart = new GlideRecord('sc_cart_item');
grCart.addQuery('cart.user', gs.getUserID());
grCart.query();
while(grCart.next()){
var itemID = grCart.getValue('cat_item');
if(items.indexOf(itemID) >= 0){
continue;
}
items.push(itemID);
}
return items;
}
Cheers,
Tai Vu