Cart() vs CartJS() in ServiceNow - Clear Comparison & When to Use What

maliksneha9
Mega Sage

When working with ServiceNow catalog automation, one of the most confusing topics is choosing between the legacy Cart() API and the modern CartJS() API. Both are used to programmatically submit catalog items, but they behave very differently.

 

This article breaks it down in simple terms, with practical guidance and real-world usage.

 

  1. What is Cart()?

Cart() is the legacy (older) API used to interact with the Service Catalog.

 

Key Characteristics:

  • Works in Global scope only
  • Session-dependent
  • Designed for UI-driven interactions
  • Mimics how a user adds items to a cart and checks out

Example:

var cart = new Cart();

var item = cart.addItem('catalog_item_sys_id', 1);

cart.setVariable(item, 'variable_name', 'value');

var rc = cart.placeOrder();

 

Limitations of Cart()

 

Session-dependent

It requires:

  • A logged-in user
  • An active session
  • A valid cart context

If you run it in:

  • Transform Map
  • Scheduled Job
  • Background Script

It may fail because:

“There is no user/cart session available”

 

Not reliable for automation

Common issues:

  • Cart not found
  • Item not available
  • Inconsistent behavior in backend scripts

Global only

  • Cannot be used in Scoped Applications
  • Not future-friendly

 

  1. What is CartJS()?

CartJS() is the modern, recommended API available under the sn_sc namespace.

 

Key Characteristics:

  • Scoped API
  • Session-independent
  • Works fully server-side
  • Designed for automation use cases

 

Example:

var cart = new sn_sc.CartJS();

cart.setRequestedFor(userSysId);

 

var item = {

    sysparm_id: 'catalog_item_sys_id',

    sysparm_quantity: '1',

    variables: {

        variable_name: 'value'

    }

};

 

var response = cart.orderNow(item);

 

Using CartJS() in Global Applications

Even though CartJS() is part of the sn_sc (scoped API), you can still use it in Global.

 

How to use CartJS() in Global

 

Simply call it like this:

var cart = new sn_sc.CartJS();

✔ Works in Global scripts
✔ No extra setup required (in most cases)

0 REPLIES 0