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.
Using cartJS API to order item is not working as expected - Support and Troubleshooting
  • >
  • Knowledge Base
  • >
  • Support and Troubleshooting (Knowledge Base)
  • >
  • Using cartJS API to order item is not working as expected
KB0714209

Using cartJS API to order item is not working as expected


6557 Views Last updated : Apr 7, 2024 public Copy Permalink
KB Summary by Now Assist

Issue

Symptoms


Try to Order an item from custom cart orders but all items from default cart is being ordered

Release


London and below

 

Cause


This is a bug in the platform and it has been addressed in PRB1297366

Resolution


The OOB order_now API (api/sn_sc/servicecatalog/items/{sys_id}/order_now) is designed for ordering only a single item
 
For ordering multiple items, checkout(/api/sn_sc/servicecatalog/cart/checkout) or submit order (/api/sn_sc/servicecatalog/cart/submit_order) should be used.
 
But currently, there is a bug in the platform which orders all the items from the portal cart instead of the custom cart, if we order multiple items using checkout or submit order and a PRB1297366 was created for this issue and the Problem is in testing state.
 
So as a workaround, a custom web service can be created and can be used that in the apps outside of SN.
 
WORKAROUND:
1. Please change the current application scope to "Service Catalog REST API" (the same scope where the OOB order_now scripted API is present)
2. Type "Scripted REST APIs" in the filter navigator and select the "Scripted REST APIs" under the "System Web Services"
3. Open the record with the name "Service Catalog API"
4. Under the "Resources" section (at the bottom), Open the record with the name "Buy Item" in a separate tab for reference.
5. Go back to the "Resources" section and click "new"
6. Give the following values for the fields:
Name : <your desired name> (e.g) Custom Order now
API Version: v1
HTTP Method: POST
Relative path: /items/{sys_id}/<your desired name> (e.g) /items/{sys_id}/custom_order_now
7. Under the script section, paste the following code:
 
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
 
// implement resource here
var request_body = request.body.nextEntry();
var quantity = '' + request_body.sysparm_quantity;
var noValidation = (request_body.sysparm_no_validation == 'true');
var cartName = request_body.sysparm_cart_name;
 
if (!/^\+?([0-9]*)$/.test(quantity))
throw new sn_ws_err.BadRequestError("Invalid Quantity value");
else
request_body.sysparm_quantity = quantity;
 
var itemId = '' + request.pathParams.sys_id;
request_body.sysparm_id = itemId;
var catItem = new sn_sc.CatItem(itemId);
if (!catItem.canView())
throw new sn_ws_err.BadRequestError("Security constraints prevent ordering of Item");
 
if(!noValidation) {
var catUtil = new RestCatalogUtil();
if (!catUtil.checkMandatoryVariables(itemId, request_body.variables))
throw new sn_ws_err.BadRequestError('Mandatory Variables are required');
}
var cart;
//If cartName is undefined then it will be falsy
if(cartName){
cart = new sn_sc.CartJS(cartName);
}else{
cart = new sn_sc.CartJS("cart_" +itemId);
request_body.sysparm_cart_name = "cart_" +itemId;
}
try {
return cart.orderNow(request_body);
}catch(e) {
gs.debug(e);
throw new sn_ws_err.NotFoundError("Invalid Request");
}
 
})(request, response);
 
8. Save the record.
 
Now the custom scripted api can be found in the API list in the REST Explorer and it would order all the items from the custom cart.
 
Please make sure that this object {"sysparm_quantity":"1","get_portal_messages":"true","sysparm_cart_name":"JJ1234"} be included in the Request body.

 

 


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.