
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-06-2018 04:25 PM
I am getting the below error while calling a cart API.
Cannot find function addItem in object [object CartJS].
TypeError: Cannot find function addItem in object [object CartJS].
Here are my scripts:
Run Script :
OpenRequestedItem();
function OpenRequestedItem()
{
var oldritmid = current.sys_id;
gs.info('runscript oldritmid is:' +oldritmid);
var x = new commonSI(); // calling Script Include
x.openCleanUpRitm1(oldritmid); // calling function
}
Script Include Code:
openCleanUpRitm1:function(oldRitmId)
{/* This is to open a Catalog item at run tim*/
try{
gs.info('script include oldRitmId is:' +oldRitmId);
var cart = new sn_sc.CartJS();
cart.addItem('d3fc15afdb9a8780c536f3f7bf9619a2'); // sys id of the catalog item
cart.setVariable(item,'u_ritm',oldRitmId);
var rc = cart.placeOrder();
return(rc);
}
catch(err)
{
gs.addErrorMessage(err.message);
gs.addErrorMessage(err);
}
}
I am doing all this in a scoped application.
Let me know where i am doing incorrectly.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-06-2018 05:26 PM
Can you have your variables like,
'variables':{
'u_ritm': oldRitmId.toString(),
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-06-2018 04:43 PM
Please check if this helps in try block,
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('d3fc15afdb9a8780c536f3f7bf9619a2'); // sys id of the catalog item
cart.setVariable(item,'u_ritm',oldRitmId);
var rc = cart.placeOrder();
return(rc);
Reference: Service catalog script API

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-06-2018 05:01 PM
Hi Srivastava,
I tried it.
Got the below error.
java.lang.SecurityException: GlideGuid is not allowed in scoped applications
JavaException: java.lang.SecurityException: GlideGuid is not allowed in scoped applications
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-22-2018 08:28 AM
You need to pass in a null guid to call new cart().
I think the frustrating thing in this thread is that folks are putting code for Cart when you are asking about CartJS. I can't even easily find the real difference between Cart and CartJS in SN documentation--seems like a mess.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-06-2018 05:05 PM
I have tried to modify the script include code as below :
But got another error:
org.json.simple.JSONObject cannot be cast to java.lang.String
java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to java.lang.String
/**code
openCleanUpRitm1:function(oldRitmId){
try{
var cart = new sn_sc.CartJS();
var item =
{
'sysparm_id': 'd3fc15afdb9a8780c536f3f7bf9619a2',
'sysparm_quantity': '1',
'variables':
{
'u_ritm': oldRitmId
}
};
var cartDetails = cart.addToCart(item);
var checkoutInfo = cart.checkoutCart();
gs.addInfoMessage('Cleanup request ' + checkoutInfo.request_number + ' has been raised.');
return(checkoutInfo.request_number);
}catch(err)
{
gs.addErrorMessage(err.message);
gs.addErrorMessage(err);
}
}
can somebody please help me out!
Thanks