CartJS - Scoped

CartJS API enables you to access the shopping cart for a user. To use this class in a scoped application, use the sn_sc namespace identifier. The Service Catalog Scoped API plugin (ID: com.glideapp.servicecatalog.scoped.api) that is enabled by default is required to access the CartJS API.

Scoped CartJS - addToCart(Map request)

Adds the request for a catalog item to the current cart.

Table 1. Parameters
Name Type Description
request Map A JSON object that contains the details of the catalog item to be added to the cart.

The structure of the request object is:

{
'sysparm_id': item_id,
'sysparm_quantity': item_quantity,
'variables':{
'var_name': 'var_value',
...
}
}
  • item_id: sys_id of the item to be added to the cart
  • item_quantity: Number of items to be added. Default value is 1.
  • var_name: Name of the question.
  • var_value: Value of the answer (Not the display value).
Table 2. Returns
Type Description
JSON Structure of the current cart.
{
'subtotal': value,
'items':[
{
itemName:'',
quantity:'', 
price:'', 
recurring_price:''
} ...]
}

Example

var cart = new sn_sc.CartJS();
cart.get();
var item =
{
'sysparm_id': '0d08837237153000158bbfc8bcbe5d02',
'sysparm_quantity': '1',
'variables':{
'carrier': 'at_and_t_mobility',
'data_plan': '500MB',
'duration': 'eighteen_months',
'color': 'slate',
'storage': 'sixtyfour' 
};
var cartDetails = cart.addToCart(item);
gs.info(cartDetails);

Output

{ 
"cart_id":"35ec9e8947a13200e0ef563dbb9a7109",
"items":[ 
{ 
"cart_item_id":"35ec9e8947a13200e0ef563dbb9a710a",
"catalog_item_id":"0d08837237153000158bbfc8bcbe5d02",
"item_name":"Apple iPhone 5",
"localized_price":"$799.99",
"localized_recurring_price":"$30.00",
"price":"$799.99",
"quantity":"1",
"recurring_frequency":"Monthly",
"recurring_price":"$29.00"
}
]
"subtotal":"$799.99"
}

Scoped CartJS - CartJS()

Creates an instance of the CartJS class for the default cart of the user who is currently logged in.

Table 3. Parameters
Name Type Description
None
var cart = new sn_sc.CartJS();

Scoped CartJS - CartJS(String cartName)

Creates an instance of the CartJS class with the name of a defined cart for the user who is currently logged in.

Table 4. Parameters
Name Type Description
cartName String Name of a defined cart for the user who is currently logged in.
var cart = new sn_sc.CartJS(cart1);

Scoped CartJS - checkoutCart()

Performs the cart checkout. If the two-step checkout is enabled, returns the order summary. If the two-step checkout is disabled, the cart is submitted and details of the generated request are returned.

Table 5. Parameters
Name Type Description
None
Table 6. Returns
Type Description
JSON If the two-step checkout is enabled, the summary of the items in the cart is returned.
{ "subtotal_price":"",
  "subtotal_recurring_frequency":"",
  "delivery_address":"",
  "special_instructions":"",
  "total_title":"",
  "requested_for_user":"System Administrator",
  "requested_for":"6816f79cc0a8016401c5a33be04be441",
  “daily”: ["frequency_subtotal":"", "items":[{}, {}, ...], …],
  “monthly”:["frequency_subtotal":"", "items":[{}, {}, ...], …],
  “annually”:["frequency_subtotal":"", "items":[{}, {}, ...], …],
  "none":["frequency_subtotal":"", "items":[{}, {}, ...], …],
}
If the two-step checkout is disabled:
{
  'request_id' : "sys_id of the generated request",
  "request_number" : "Number of the generated request"
}

Example

var cart = new sn_sc.CartJS();
var checkoutInfo = cart.checkoutCart();
gs.info(checkoutInfo);

Output

If two step checkout enabled:
{"subtotal_price":"$2,748.49","subtotal_recurring_frequency":"","delivery_address":"","special_instructions":"","total_title":"Total","requested_for_user":"System Administrator","requested_for":"6816f79cc0a8016401c5a33be04be441","weekly":{"subtotal_price":"$399.50","subtotal_recurring_frequency":"Weekly","subtotal_recurring_price":"$0.00","total_title":"Total","items":[{"catalog_item_id":"e90a0f7237153000158bbfc8bcbe5d7f","variables":{},"quantity":"1","localized_price":"$399.50","price":"$399.50","recurring_frequency":"Weekly","localized_recurring_price":"$0.00","recurring_price":"$0.00","item_name":"Brother Network-Ready Color Laser Printer","cart_item_id":"f3f5c9b3c322320076173b0ac3d3ae00","delivery_time":"2 Days"}],"show_subtotal_price":"true","subtotal_title":"Subtotal"},"show_subtotal_price":"true","cart_id":"251c0562c326320076173b0ac3d3aeb4","subtotal_recurring_price":"$131.00","monthly":{"subtotal_price":"$849.99","subtotal_recurring_frequency":"Monthly","subtotal_recurring_price":"$31.00","total_title":"Total","items":[{"catalog_item_id":"d82ea08510247200964f77ffeec6c4ee","variables":{"Monthly data allowance":"500MB","Storage":"64GB","Color":"Space Gray","Is this a replacement for a lost or broken iPhone?":"No","What was the original phone number?":""},"quantity":"1","localized_price":"$799.99","price":"$849.99","recurring_frequency":"Monthly","localized_recurring_price":"$30.00","recurring_price":"$31.00","item_name":"Apple iPhone 6s Plus","cart_item_id":"18c509b3c322320076173b0ac3d3aef4","delivery_time":"2 Days"}],"show_subtotal_price":"true","subtotal_title":"Subtotal"},"yearly":{"subtotal_price":"$1,499.00","subtotal_recurring_frequency":"Annually","subtotal_recurring_price":"$100.00","total_title":"Total","items":[{"catalog_item_id":"774906834fbb4200086eeed18110c737","variables":{"Adobe Photoshop":"false","Adobe Acrobat":"false","Optional Software":"","Additional software requirements":"","Eclipse IDE":"true"},"quantity":"1","localized_price":"$1,499.00","price":"$1,499.00","recurring_frequency":"Annually","localized_recurring_price":"$100.00","recurring_price":"$100.00","item_name":"Developer Laptop (Mac)","cart_item_id":"6306c9b3c322320076173b0ac3d3ae89","delivery_time":"5 Days"}],"show_subtotal_price":"true","subtotal_title":"Subtotal"},"subtotal_title":"Subtotal"}
If two step checkout disabled:
{"request_number":"REQ0010001","request_id":"2f2789f3c322320076173b0ac3d3ae62"}

Scoped CartJS - empty()

Deletes the current cart.

Table 7. Parameters
Name Type Description
None
Table 8. Returns
Type Description
void
var cart = new sn_sc.CartJS();
      cart.empty();

Scoped CartJS - getCartID()

Returns the cart id of the current cart.

Table 9. Parameters
Name Type Description
None
Table 10. Returns
Type Description
String sys_id for the current cart.

Example

      var cart = new sn_sc.CartJS();
      cart.getCartID(); 
      gs.info(cartId);

Output

039c516237b1300054b6a3549dbe5dfc

Scoped CartJS - getCartItems()

Returns the GlideRecord for the cart item (sc_cart_item) in the current cart.

Table 11. Parameters
Name Type Description
None
Table 12. Returns
Type Description
GlideRecord GlideRecord pointing to cart items in the current cart.

Example

      var cart = new sn_sc.CartJS();
      cart.getCartItems(); 
      gs.info(CartItems)

Output

Apple iPhone 6s Plus
Apple iPhone 6s
Apple MacBook Pro 15"

Scoped CartJS - getDeliveryAddress()

Gets the delivery address for the current cart.

Table 13. Parameters
Name Type Description
None
Table 14. Returns
Type Description
String Delivery address for the current cart.

Example

      var cart = new sn_sc.CartJS();
      cart.setDeliveryAddress("Brasilia, Brasil");
      cart.getDeliveryAddress(); 
      gs.info(DeliveryAddress);

Output

Brasilia, Brasil

Scoped CartJS - getRequestedFor()

Gets the sys_id from the sys_user record of the user for whom the cart is requested.

Table 15. Parameters
Name Type Description
None
Table 16. Returns
Type Description
String sys_id from the sys_user record of the user for whom the cart is requested.

Example

var cart = new sn_sc.CartJS();
cart.setRequestedFor("039c516237b1300054b6a3549dbe5dfc")
cart.getRequestedFor(); 
gs.info(cartId);

Output

039c516237b1300054b6a3549dbe5dfc

Scoped CartJS - getRequestedForDisplayName()

Gets the name from the user record of the user for whom the current cart is requested.

Table 17. Parameters
Name Type Description
None
Table 18. Returns
Type Description
String Name from the user record of the user for whom the current cart is requested.

Example

      var cart = new sn_sc.CartJS();
      cart.getRequestedForDisplayName(); 
      gs.info(DisplayName);

Output

Abel Tutor

Scoped CartJS - getSpecialInstructions()

Gets the special instructions for the current cart.

Table 19. Parameters
Name Type Description
None
Table 20. Returns
Type Description
String Special instructions for the current cart.

Example

      var cart = new sn_sc.CartJS();
      cart.setSpecialInstructions("Delivery before 8 AM.");
      cart.getSpecialInstructions(); 
      gs.info(SpecialInstructions);

Output

Delivery before 8 AM.

Scoped CartJS - orderNow(Map request)

Orders a single item. If the two-step checkout is enabled, the item is added to the cart and the cart sys_id is returned. If the two-step checkout is disabled, the item is ordered and the generated request sys_id and number is returned.

Table 21. Parameters
Name Type Description
request Map A JSON object that contains details of the catalog item to be ordered.

The structure of the request object is:

{
  'sys_id' : item_id,
  'sysparm_quantity' : item_quantity,
  'sysparm_requested_for' : requested_for,
  'variables' : {
    'var_name' : 'var_value',
    ...
  }
}
  • item_id: sys_id of the item to be added to the cart. Required.
  • item_quantity: Number of items to be added. Default value is 1.
  • var_name: Name of the question.
  • var_value: Value of the answer (Not the display value).
Table 22. Returns
Type Description
JSON If the two-step checkout is enabled:
{
  'cart_id' : 'sys_id of cart that item has been added to'
}

If the two-step checkout is disabled:

{
  'request_id' : 'sys_id of the generated request',
  'request_number' : 'Number of the generated request'
}

Example

var cart = new sn_sc.CartJS();
cart.get();
var request =
{
'sysparm_id': '0d08837237153000158bbfc8bcbe5d02',
'sysparm_quantity': '1',
'variables':{
'carrier': 'at_and_t_mobility',
'data_plan': '500MB',
'duration': 'eighteen_months',
'color': 'slate',
'storage': 'sixtyfour'
}
}
var cartDetails = cart.orderNow(request);
gs.info(cartDetails);

Output

If the two-step checkout enabled:
{"cart_id":"55384df3c322320076173b0ac3d3aec5"}
If the two-step checkout disabled:
{"request_id":"4c690137c322320076173b0ac3d3ae03", "request_number": "REQ0010003"}

Scoped CartJS - setDeliveryAddress(String address)

Sets the delivery address for the current cart.

Table 23. Parameters
Name Type Description
address String Delivery address for the current cart.
Table 24. Returns
Type Description
void
      var cart = new sn_sc.CartJS();
      cart.setDeliveryAddress("Brasilia, Brasil"); 
      

Scoped CartJS - setSpecialInstructions(String specialInstructions)

Sets the special instructions for the current cart.

Table 25. Parameters
Name Type Description
specialInstructions String Special instructions for the current cart.
Table 26. Returns
Type Description
void
      var cart = new sn_sc.CartJS();
      cart.setSpecialInstructions("Delivery before 8 AM."); 
   

Scoped CartJS - submitOrder(Map request)

Updates special instructions, requested for, and delivery address from the request parameter and performs the cart checkout. Use this API to modify the mentioned parameters of the cart and perform the cart checkout simultaneously. Missing parameters in the request object will have their default value.

Table 27. Parameters
Name Type Description
request Map A JSON object that contains details of the cart to be submitted.

The structure of the request object is:

{
  'special_instructions' : 'instructions',
  'sysparm_requested_for' : requested_for,
  'delivery_address' : 'address'
}
  • instructions: Special instructions for the request.
  • requested_for : sys_id of the requested_for user.
  • address: Delivery address for the request.
Table 28. Returns
Type Description
JSON Structure of the cart.
{
  'request_id' : 'sys_id of the generated Request',
  'request_number' : 'Number of the generated Request'
}

Example

var cart = new sn_sc.CartJS();
var request =
{
  'special_instructions' : 'Delivery only in working hours',
  'requested_for' : '62826bf03710200044e0bfc8bcbe5df1',
  'delivery_address' : "Brasilia, Brasil",
};
var requestDetails = cart.submitOrder(request);
gs.info(requestDetails);

Output

{"request_id":"6eed229047801200e0ef563dbb9a71c2", "request_number": "REQ0000001"}

Scoped CartJS - updateItem(Map request, String cart_item_id)

Updates an item in the cart.

Table 29. Parameters
Name Type Description
request Map A JSON object that contains details of the catalog item to be updated.

The structure of the request object is:

{
    'sysparm_quantity' : item_quantity,
  'sysparm_requested_for' : requested_for,
  'variables' : {
    'var_name' : 'var_value',
    ...
  }
}
  • item_quantity: Number of items to be added. Default value is 1.
  • var_name: Name of the question.
  • var_value: Value of the answer (Not the display value).
cart_item_id String sys_id of the cart item to be modified.
Table 30. Returns
Type Description
JSON Details of the cart.
{
  'subtotal': value,
  'items':[
    {
    itemName:'',
    quantity:'',
    price:'',
    recurring_price:''
    }
    ...],
  ...
}

Example

var cart = new sn_sc.CartJS();
cart.get();
var request =
{
  'sysparm_quantity': '1',
  'variables':{
    'carrier': 'at_and_t_mobility',
    'data_plan': '500MB',
    'duration': 'eighteen_months',
    'color': 'slate',
    'storage': 'sixtyfour'
  }
};
var cart_item_id = "4d69b672c322320076173b0ac3d3ae79";
var cartDetails = cart.updateItem(request, cart_item_id);
gs.info(cartDetails);

Output

{ "cart_id":"35ec9e8947a13200e0ef563dbb9a7109", "items":[ { "cart_item_id":"35ec9e8947a13200e0ef563dbb9a710a", "catalog_item_id":"0d08837237153000158bbfc8bcbe5d02", "item_name":"Apple iPhone 5", "localized_price":"$799.99", "localized_recurring_price":"$30.00", "price":"$799.99", "quantity":"1", "recurring_frequency":"Monthly", "recurring_price":"$29.00" } ] "subtotal":"$799.99" }