Requirement to add an order guide to a manager's cart

prithaMajumdar
Tera Contributor

I need to use the Cart API to put an item in a manager's cart before the new employee starts: 

 

var cart = new sn_sc.CartJS();
var item =
{
'sysparm_id': '6c07f44fdb70a70020c06c16ca9619f7',
'sysparm_quantity': '1',
};
cart.setRequestedFor("940ec417db2aa450f0659605f39619ef");
var userId = cart.getRequestedFor();
gs.info(userId);

var cartDetails = cart.addToCart(item);


gs.info(JSON.stringify(cartDetails));

 

I looked at the cart API and changed the cart.setRequestFor to the manager's sysid but the items keep getting added to my cart. I need a way to access the other user's cart. 

4 REPLIES 4

Anand Kumar P
Giga Patron
Giga Patron

Hi @prithaMajumdar ,
Make sure to add correct sys_id of manager the script looks good.

var cart = new sn_sc.CartJS();
var item = {
    'sysparm_id': '6c07f44fdb70a70020c06c16ca9619f7',
    'sysparm_quantity': '1'
};
cart.setRequestedFor('manager_sys_id'); 
var cartDetails = cart.addToCart(item);
gs.info(JSON.stringify(cartDetails));

 

Thanks,

Anand

Hi Anand, 

 

It is correct sys_id, I am running this as background but the item is getting added to my cart. not the manager's...

When i add item through this script and retrieve from cart in portal is not showing next button in order guide. Also how to set order guide variables through this?

prithaMajumdar
Tera Contributor

Wrote my own code to fix this: 

 

// Create a new GlideRecord object for the sc_cart table
var cartRecord = new GlideRecord('sc_cart');
var cardId = '';
// Set values for the fields
cartRecord.addQuery('user', '940ec417db2aa450f0659605f39619ef'); // Replace 'user_sys_id_here' with the actual user's sys_id
cartRecord.query();
if(cartRecord.next()){
    cardId = cartRecord.sys_id.toString();
}

var cartItm = new GlideRecord('sc_cart_item');
cartItm.initialize();
cartItm.cat_item = '1462a49087c2f9106bfe657f8bbb35dc';
cartItm.cart = cardId; 
var newItm = cartItm.insert();
gs.log(newItm);