Difficulty in using CartJS in Business Rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Hi,
I am trying to submit a catalog item request through a business rule. I came across the CartJS documentation and followed the sample code there. However, when using cart.addToCart() or the other submission calls, I got the following error:
Root cause of JavaScriptException: java.lang.ClassCastException
: java.lang.ClassCastException: class org.json.simple.JSONObject cannot be cast to class java.lang.String (org.json.simple.JSONObject is in unnamed module of loader com.snc.orbit.container.tomcat8.ParallelOrbitTomcat8ClassLoader @29fe1f17; java.lang.String is in module java.base of loader 'bootstrap'): com.glideapp.servicecatalog.CatalogUtil.jsonToMap(CatalogUtil.java:619)
Below is my code:
const cart = new sn_sc.CartJS(gs.generateGUID());
const request = {
sysparm_id: routineCreationCatalogItem.sys_id,
sysparm_quantity: '1',
variables:{
routine_name: `Daily Routine - ${businessApp.name}`,
activity: dailyActivity.sys_id,
business_application: businessApp.sys_id
}
};
cart.addToCart(request);
In the example code provided in the documentation, there was no need to do JSON.stringify() on the request. This is the first issue.
Subsequently I tried using JSON.stringify() on the request and there was no error thrown anymore. However, nothing was returned and I am not able to log out the cart details so I cannot do any troubleshooting.
Is anyone facing the same issues as me?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
50m ago
try this
// 1) Instantiate CartJS (no GUID needed in most recent docs)
var cart = new sn_sc.CartJS();
// 2) Build the item payload as a JS object
var itemOrderDetails = {
sysparm_id: routineCreationCatalogItem.sys_id, // catalog item sys_id
sysparm_quantity: '1',
variables: {
routine_name: 'Daily Routine - ' + businessApp.name,
activity: dailyActivity.sys_id,
business_application: businessApp.sys_id
}
};
// 3) Convert to JSON string before passing to addToCart
var itemJson = JSON.stringify(itemOrderDetails);
// 4) Add to cart
var addToCartResult = cart.addToCart(itemJson);
// Optional: log result for troubleshooting
gs.info('addToCart result: ' + JSON.stringify(addToCartResult));
// 5) Checkout
var checkoutResult = cart.checkoutCart();
gs.info('checkoutCart result: ' + JSON.stringify(checkoutResult));
// 6) Use the result
if (checkoutResult && checkoutResult.request_number) {
gs.info('Order submitted. Request: ' + checkoutResult.request_number);
// If you need sys_id, you may need to query sc_request by number
} else {
gs.error('Failed to submit order: ' + JSON.stringify(checkoutResult));
}
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
