How to add to a current cart instead of new cart()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-09-2015 09:52 AM
I have a script that works great- it copies a previous request and tosses all the RITMs into the curent user's shopping cart. The problem is, the script is deleting everything in their cart with new cart = Cart();
I found some code on the wiki (below) but it doesn't work. In fact it stops my script from copying anything to the cart at all. I did a test print on the userID and it is finding my userID and my cart, it just doesn't add items to it.
var cart = getCart();
function getCart() {
var cart = new GlideRecord('sc_cart');
var userid = gs.getUserID();
cart.addQuery('user', userid);
cart.query();
if (cart.next()) {
// we already have a cart all is well
gs.log('found cart ' + userid);
}
else {
gs.log('new cart');
cart.initialize();
cart.user = userid;
cart.insert();
}
return cart;
}
Below is the actual code I'm trying to use. Anyone see why I'm not able to keep the items in my shopping cart?
var origReq = new GlideRecord('sc_request'); | |
origReq.addQuery('sys_id', rid); | |
origReq.query(); |
if(origReq.next()){ | ||
var cart = getCart(); | ||
gs.log(cart); | ||
cart.cart.update(); | ||
//cart.cart.special_instructions = 'PARENT:' + parentRequest.sys_id; | ||
//cart.cart.update(); |
var ritm = new GlideRecord('sc_req_item'); | ||||
ritm.addQuery('request', reqID); | ||||
ritm.query(); | ||||
while (ritm.next()) { | ||||
// start copying the RITM vars | ||||
var oldRITM = new GlideRecord('sc_req_item'); | ||||
oldRITM.get(ritm.sys_id); | ||||
var newRITM = cart.addItem(oldRITM.cat_item, 1); | ||||
for (var varName in oldRITM.variables) { | ||||
cart.setVariable(newRITM, varName, oldRITM.variables[varName]+''); | ||||
} | ||||
} | ||||
} |
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-09-2015 03:56 PM
Hi Eric,
I believe you are mixing two different methods/objects to update your cart.
Let me see if I can explain in a clear manner.
The "getCart()" script you found in the wiki is only part of a larger script where in the rest of the script it adds new items by creating a new GlideRecord object on the sc_cart_item table and defining the corresponding cart sys_id, the catalog item as well as quantity to that cart item through a custom function named "addToCart()". Then doing the same down to the options (variables) with a custom function named "addOptions()" .
Useful Catalog Scripts - ServiceNow Wiki (script found under the example named: Order a Service Catalog Item from a Script )
Now in your script yes you're almost doing the same except you are trying to add the item with a method (.addItem()) that is not available through the GlideRecord object. It's available through the Cart() object which is different from the GlideRecord.
That can be found here: Service Catalog Script API - ServiceNow Wiki
So if you compare the two ways that their "new cart" is created one is doing this
var cart = new GlideRecord('sc_cart'); //Glide Record object
(cart.addItem() method is not available here)
and the other is doing this:
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId); // NOT a Glide Record
(cart.addItem() is available in this object)
You should also notice that neither script examples are using an "cart.update()" method on the cart to update it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2015 05:51 AM
Hi Chris,
Thank you for your response.
Since my code was primarily using the cart.addItem() option I decided to continue that route and use
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
... if (origReq.next()) {
for (var varName in oldRITM.variables) {
cart.setVariable(newRITM, varName, oldRITM.variables[varName]+'');
}
Just like I was thinking- this is producing a brand new cart every time through. How can I continue this path but use the current cartId instead of create a new cart for the user? The end goal is to have all these items placed in the cart (but not ordered) so they have the ability to go back and update them. If it creates a brand new cart, they won't be able to access the items in this cart if they already have a Default one.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2015 06:38 AM
In order to add to the current cart you'll have to use the GlideRecord version. Remember that updating the cart isn't about trying to get into the cart itself and performing a "cart.update()". Use the GlideRecord version to find the sys_id of the existing cart.
From that then use the GlideRecord to create new cart items (sc_cart_item) and set the cart field to the cart sys_id found from above.
Then of course if there are options you'll need to set the options again using the information captured/created from doing the above on a GlideRecord object from the "item_option_new" table iterating and setting the desired options.
This is all captured in the wiki script.
Order a Service Catalog Item from a Script - ServiceNow Wiki
The step that they include is emptying the cart and then adding the new items which you don't necessarily have to do.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2015 10:31 AM
Hi Eric,
After pulling the properties of the Cart() object I believe you can add items an existing cart by creating a blank Cart() object and then getting the desired cart with the getCart method.
eg:
var cart_ID = 'the sys_id of your desired cart';
// create a blank cart object
var cart = new Cart();
// get existing cart
cart.getCart(cart_ID);
// now add item to your existing cart
cart.addItem('sys_id of your desired item');
Give that a try adding in the rest of your item options.
FYI - These are the available properties within the Cart() object
userID
cartName
cart
prepVariables
addItem
placeOrder
deleteCart
setVariable
clearCart
getCart
initialize