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

Eric,



Were you able to give my last response a try? In case I didn't explain correctly here is what is causing it to create a new cart instead of update the existing cart:



var cartId = GlideGuid.generate(null); // this creates a new sys_id for a brand new cart. You don't need this


var cart = new Cart(cartId); // don't put an id as a parameter not even the existing cart




... if (origReq.next()) {


  for (var varName in oldRITM.variables) {


  cart.setVariable(newRITM, varName, oldRITM.variables[varName]+'');


  }




The correct solution would be to get the sys_id of the current cart with the glide record object then create an object using the new Cart() object and use the "getCart()" method available inside that object (not the custom getCart() function as in the wiki) and place the sys_id of the existing cart as a parameter. Example:



var existingCart = new GlideRecord('sc_cart');


existingCart.addQuery('user', gs.getUserID());


existingCart.query();




if(existingCart.next()){


      var cart = new Cart(); // using "new Cart()" object without parameters


      cart.getCart(existingCart.sys_id);   // here is where you grab the existing cart. this is not the "getCart()" function from the wiki


}



Now you're able to use cart.addItem() and cart.setVariables() the way you want to and update an existing cart.


Hi Chris,



Thank you for the followup. I was out of the office for a few days so I apologize for the late response.



I am using the below but it is still clearing out what's already in the shopping cart before adding new items to it. It still works if I'm copying a single request with one or more RITMS,   but if I'm trying to copy RITMS from multiple requests it just stores whatever RITMS were in the last request.



The log file is printing out that it found the existing cart, so we know it's finding my proper cart. I'm wondering if there's something simply built-in that is clearing out the cart when it's being populated via a script.



EDIT: The wiki itself says every call to new Cart() clears the current cart. So I guess it's just a limitation then unless there is a way to NOT call new Cart() at all and still have access to the object.




var origReq = new GlideRecord('sc_request');
origReq.addQuery('sys_id', rid);
origReq.query();



if(origReq.next()){
var existingCart = new GlideRecord('sc_cart');
existingCart.addQuery('user', gs.getUserID());
existingCart.query();



if(existingCart.next()){
var cart = new Cart(); // using "new Cart()" object without parameters
cart.getCart(existingCart.sys_id);   // here is where you grab the existing cart. this is not the "getCart()" function from the wiki
gs.log('found existing cart');

Hi Eric,


did you manage to achieve this functionality? As we are also having the same requirement.



Thanks,


Ganesh


I am trying essentially the same thing after insert on the sc_req_item table (adding more items to the existing request) and I seem to be hitting a different issue than you. Variables on items other than the one added to the request are not getting stored.



Adding items to Request after insert clears the variables on other preexisting items... help?


britwill
ServiceNow Employee
ServiceNow Employee

Hi Chris-



Do you have a contact email? I'd like to ask a question offline.



Regards,


British