How to add to a current cart instead of new cart()

e_wilber
Tera Guru

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]+'');  
}  
}
}  

}

15 REPLIES 15

kkisz
Kilo Explorer

Could you please provide a little more information on how/where to use this?