Skip to page contentSkip to chat
ServiceNow support
    • Community
      Ask questions, give advice, and connect with fellow ServiceNow professionals.
      Developer
      Build, test, and deploy applications
      Documentation
      Find detailed information about ServiceNow products, apps, features, and releases.
      Impact
      Accelerate ROI and amplify your expertise.
      Learning
      Build skills with instructor-led and online training.
      Partner
      Grow your business with promotions, news, and marketing tools
      ServiceNow
      Learn about ServiceNow products & solutions.
      Store
      Download certified apps and integrations that complement ServiceNow.
      Support
      Manage your instances, access self-help, and get technical support.
Use addToCart() function from sn_sc.CartJS() to add multiple items into the cart. - Support and Troubleshooting
  • >
  • Knowledge Base
  • >
  • Support and Troubleshooting (Knowledge Base)
  • >
  • Use addToCart() function from sn_sc.CartJS() to add multiple items into the cart.
KB0721274

Use addToCart() function from sn_sc.CartJS() to add multiple items into the cart.


9455 Views Last updated : Apr 2, 2024 public Copy Permalink English (Original)
  • English (Original)
  • Japanese
KB Summary by Now Assist

Issue

This article shows the use of the addToCart() function to add multiple items into the current user's cart.

It is working as expected to call sn_sc.CartJS() multiple times in the script to be able to add multiple items.

Procedure

  1. Log in to any out-of-box instance as System Administrator (Or any user with admin role).
  2. Go to System Definition > Script - Background. And test the following script:
    //************************************************************ 
    var cart = new sn_sc.CartJS();
    for (i = 0; i < 2; i++) {
      gs.addInfoMessage('i: '+ i);
      var item = {
        'sysparm_id': '10a2f5dfc6112276018db58138c7a1e0', //sys_id of catalog item
        'sysparm_quantity': '1',
        'variables': {
        },
      };
      cart.addToCart(item);
    }
    //************************************************************
  3. We are expecting two items to be added to the user's cart, but if you check the sc_cart record for the current user, only one item got added.
    • (To find current user's sc_cart records, type sc_cart.list in the instance menu Filter navigator.
    • Filter the resulting list on the User column.)
  4. Delete all the sc_cart records for the current user.
  5. Test the following script in System Definition > Script - Background again:
    //************************************************************ 
    for (i = 0; i < 2; i++) {
      gs.addInfoMessage('i: '+ i);
      var cart = new sn_sc.CartJS();
      var item = {
        'sysparm_id': '10a2f5dfc6112276018db58138c7a1e0',
        'sysparm_quantity': '1',
        'variables': {
        },
      };
      cart.addToCart(item);
    }
    //************************************************************

    Note "var cart = new sn_sc.CartJS();" is inside the loop now. This script adds two items into the cart.

Release

London , Orlando

Related Links

  • CartJS (Developer docs)

The world works with ServiceNow.

Sign in for more! There's more content available only to authenticated users Sign in for more!
Did this KB article help you?
Did this KB article help you?

How would you rate your Now Support digital experience?

*

Very unsatisfied

Unsatisfied

Neutral

Satisfied

Very satisfied

Very unsatisfied

Unsatisfied

Neutral

Satisfied

Very satisfied

What can we improve? Please select all that apply.

What are we doing well? Please select all that apply.

Tell us more

*

Do you expect a response from this feedback?

  • Terms and conditions
  • Privacy statement
  • GDPR
  • Cookie policy
  • © 2025 ServiceNow. All rights reserved.