pawel_staszewsk
Giga Guru

Hi all.

It took me a bit to find out how to make script for New York which can be used in scoped application and I would like to share it with you, so maybe you will not lose your time. I am using this script in Web Services Operation (REST Scripted API), so partners does not need to know service-now technical details; also I may pre-process request and respond with error code/message accordingly. Have fun reusing it.

Regards,

// This script will order catalog item
// Specify userId - the user for who it is requested
//   and catalog item id
// Recommended is to test this using "Scripts - Background" (in Navigator under System Definition)
//   and later embed it into IIFE script, which e.g. can be scheduled
try {
   var userId ='59e07d68db...'; // User sys_id who has access to the catalog item
   var itemId = '7e858d66dbc...'; // Catalog Item sys_id
   var userGr = new GlideRecord('sys_user');
   userGr.get(userId);
   // create new cart
   var cartId = gs.generateGUID() + ':for:' + userGr.getValue('user_name');
   var cartGr = new sn_sc.CartJS(cartId); // Glide Record
   gs.info("Cart [" + cartId + "] has system id: " + cartGr.getCartID());
   // looks like these two are required
   cartGr.setDeliveryAddress("IT intenal");
   cartGr.setSpecialInstructions("Automated delivery.");
   // create item object for order
   var request =
   {
      'sysparm_id': itemId,
      'sysparm_quantity': '1',
      'variables':{ 
         'comment': 'This is test request', // here list of variables for this item
         'user': userId                     // comment and user are examples
      }
   };
   // add item to cart
   var cartDetails = cartGr.addToCart(request); // JS object
   for (var i in cartDetails) {
      gs.info("Cart Details: " + i + "=" + cartDetails[i]);
   }
   // check what is in the cart
   cartItems = cartGr.getCartItems(); // Glide Record
   // checkout cart
   var checkoutInfo = cartGr.checkoutCart(); // JS Object
   // check checkout response
   for (var k in checkoutInfo) {
      gs.info("CheckoutInfo: " + k + "=" + checkoutInfo[k]);
   }
   // Update Catalog Request
   var reqGr = new GlideRecord('sc_request');
   reqGr.get(checkoutInfo.request_id);
   reqGr.setValue('description', 'Automatically created by script.');
   reqGr.update();
   // Update ordered items tasks (RITMs)
   var itemGr = new GlideRecord('sc_req_item');
   while (cartItems.next()) {
      gs.info("Cart Item: " + cartItems.getValue('sys_id'));
      itemGr.initialize();
      itemGr.addQuery('request', checkoutInfo.request_id);
      itemGr.query();
      itemGr.setValue('priority', '2');
      itemGr.updateMultiple();  
   }
   //That's All Folks
}
catch (ex) {
   gs.error("Exception: " + ex);
}

 -

Comments
ah16
Mega Expert

Do you know where we can find the code for CartJS? I'm getting null pointer exception and was trying to debug.

 

pawel_staszewsk
Giga Guru

I have not found it.

I suppose it sould be kind of Script Includes, but it looks like it is internal java servlet.

Cheers.

Version history
Last update:
‎03-02-2020 11:29 PM
Updated by: