Notifications

599 views

Description


This KB demonstrates 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. Login 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. And filter the list on the User column.)

  4. Now, 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.

Applicable Versions


London

Additional Information


Also see more on our developer website:

https://developer.servicenow.com/app.do#!/api_doc?v=london&id=r_CJSS-CartJS

Article Information

Last Updated:2019-05-21 11:51:16
Published:2019-01-30