MaxMixali
Mega Sage

ServiceNow – Create a RITM on Case insert (use server-side Cart API, not CartJS)

Problem
You’re calling sn_sc.CartJS() from a Business Rule / Script Include to create a RITM when a Case is inserted. You see the first gs.info but not the final one. Root causes:
1) CartJS is a client-side (portal) facade; in server-side code use sn_sc.Cart().
2) Minor syntax issue in your sample (missing closing quotes in gs.info lines).
3) Variable values must be passed as strings/sys_ids appropriate to the variable type.

Correct approach (server-side)
Use the server-side Cart API: sn_sc.Cart(). This creates REQ/RITM in one transaction.

When to run
- Business Rule on sn_customerservice_case (or your case table)
- When: AFTER INSERT (or AFTER INSERT/UPDATE with guard conditions)
- Synchronous or Async? Prefer Async for external calls; for straight creation, After Insert is fine.

Script Include function (drop-in)
---------------------------------
createRitmFromCase: function(caseGr) {
// 1) Resolve catalog item by your mapping table
var catItemId = '';
var catSrch = new GlideRecord('sn_customerservice_case_itsm_decision');
catSrch.addQuery('u_category', caseGr.getValue('category'));
catSrch.setLimit(1);
catSrch.query();
if (catSrch.next()) {
catItemId = catSrch.getValue('u_catalog_item'); // sc_cat_item.sys_id
}
if (!catItemId) {
gs.warn('No catalog item mapping found for category=' + caseGr.getDisplayValue('category'));
return;
}

// 2) Build variable hash (keys must be variable "Name" values on the cat item)
var vars = {};
// Example variable "customer_name" expects a reference to sys_user or a string (adjust to your variable type)
// If it is a reference to sys_user and caseGr.contact is a reference, pass sys_id:
vars.customer_name = caseGr.getValue('contact'); // sys_id
// Add more variables as needed, e.g.:
// vars.case_number = caseGr.getValue('number');
// vars.case_short_description = caseGr.getValue('short_description');

gs.info('Creating RITM from Case ' + caseGr.getDisplayValue('number') + ' for item ' + catItemId);

// 3) Create item under a new Request (Cart API always creates a new REQ)
var cart = new sn_sc.Cart();
var itemSysId = cart.addItem(catItemId, 1, vars);

// (Optional) set Requested For / special fields by updating the RITM before checkout
// var ritm = new GlideRecord('sc_req_item');
// if (ritm.get(itemSysId)) {
// ritm.requested_for = caseGr.getValue('contact') || caseGr.getValue('opened_by');
// ritm.short_description = 'Auto-created for Case ' + caseGr.getValue('number');
// ritm.update();
// }

var reqSysId = cart.placeOrder();

gs.info('RITM created from Case ' + caseGr.getValue('number') + ' → REQ=' + reqSysId + ', RITM item_sys_id=' + itemSysId);
}

Business Rule (After Insert on Case)
------------------------------------
Table: sn_customerservice_case (or your extension)
When: after insert
Filter condition: (category IS NOT EMPTY) AND (whatever your business condition is)

Script:
(function executeRule(current, previous /*null when async*/) {
try {
new x_your_scope.YourUtils().createRitmFromCase(current);
} catch (e) {
gs.error('Failed to create RITM from Case ' + current.getValue('number') + ': ' + e);
}
})(current, previous);

Important details & tips
------------------------
1) Use sn_sc.Cart() (server-side). CartJS is for client/portal via GlideAjax.
2) Variable keys must match the variable “Name” on the target catalog item (not the label).
3) Pass correct value types:
- Reference variable → pass sys_id string
- Choice variable → pass the underlying value (not display)
- Checkbox → 'true'/'false' (string) or true/false (boolean)
- Date/Time → GlideDateTime-compatible string
4) User permissions: the executing user must have rights to order that item. In BR context, the system runs as that session user. If needed, wrap in gs.getSession().impersonate('some_user') or use GlideSystem elevated privileges (with caution).
5) Availability: ensure the sc_cat_item is Active, visible to the user, and in an orderable catalog/category.
6) If you must attach the new RITM to an existing REQ (e.g., one REQ per Case), the Cart API cannot do that; you’d have to create sc_req_item yourself and populate variable rows (sc_item_option_mtom). Cart always creates a new REQ on checkout.
7) Logging: ensure gs.info lines have closing quotes (your sample had syntax errors):
gs.info('Testing, item created');
gs.info('Testing, RITM created');

Troubleshooting checklist
-------------------------
- Error in system logs about “CartJS” or missing class → switch to sn_sc.Cart().
- No RITM created: check item availability & user permissions; verify mapping table returns a valid cat item sys_id.
- Variables blank: ensure variable names match and values are valid types; verify in sc_item_option_mtom after order.
- Multiple items per case: call cart.addItem repeatedly before cart.placeOrder() to create a single REQ with multiple RITMs.

TL;DR
-----
Don’t use CartJS on the server. Use the server-side **sn_sc.Cart()** to add the item + variables and **placeOrder()**. Ensure variable names/values match your catalog item definitions and that the executing context has permission to order the item.