How to submit cart items along with manually created RITM under one Request in Portal?

Subhashree Sub2
Tera Contributor

Hi Team,

 

I am trying to raise multiple RITMs under one Request. The requirement is multiple RITMs will be created based on the number of Assets assigned to user and all those RITMs will belong to one Request. Suppose an user is having 3 assets, then 3 separate RITMs will be created belonging to 1 Request.

This functionality works by user submitting one RITM manually by filling out the catalog item form and then there is one onSubmit script executes that runs a Script include to add items to the Cart for creating RITMs depending on the asset count.

The cart items are getting added in the Employee center portal, but is not getting submitted along with manually submitted RITM.

Please let me know how can I submit all the items present in the Cart as well as manually created RITM under one Request in Employee center Portal.

 

PFA.

 

Looking forward to your suggestions.

 

Thanks & Regards,

Subhashree

3 REPLIES 3

Vishal Jaswal
Giga Sage

Hello @Subhashree Sub2 

Sample Script Include (glide ajax enabled):

var CreateRITMsForAssets = Class.create();
CreateRITMsForAssets.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  
  createRITMs: function(requestId, userId) {
    var assetGR = new GlideRecord('alm_asset');
    assetGR.addQuery('assigned_to', userId); 
    assetGR.query();

    // Create RITM for each asset assigned to the user
    while (assetGR.next()) {
      var ritmGR = new GlideRecord('sc_req_item');
      ritmGR.initialize();
      ritmGR.request = requestId; // Assign this RITM to the same Request
      ritmGR.cat_item = 'your_catalog_item'; 
      ritmGR.requested_for = userId;
      ritmGR.short_description = 'Asset request for ' + assetGR.name; 
      ritmGR.insert();
    }
  }
});


onSubmit logic:

var userId = g_user.userID;
var requestId = current.request; // Get the Request ID of the manually submitted RITM

var ritmCreator = new CreateRITMsForAssets();
ritmCreator.createRITMs(requestId, userId);

If the cart is not getting submitted automatically with the manual RITM, you can try submitting the cart after user submits the form via:

var cart = new GlideRecord('sc_cart');
cart.addQuery('user', userId); 
cart.query();
if (cart.next()) {
    // Ensure that all cart items are associated with the correct Request
    var cartItemGR = new GlideRecord('sc_cart_item');
    cartItemGR.addQuery('cart', cart.sys_id);
    cartItemGR.query();
    while (cartItemGR.next()) {
        cartItemGR.request = requestId; // Set the same Request ID for each item
        cartItemGR.update();
    }
    
    // Submit the cart items under the same Request
    var req = new GlideRecord('sc_request');
    req.get(requestId);
    req.state = 'submitted'; // set the appropriate state
    req.update();
}

 


Hope that helps!

Hi @Vishal Jaswal ,

 

Thank you for your suggestion. I tried the above logic by tweaking a little bit as per my requirement. It is working fine for ADMIN users by fetching correct cart and cart items however, for Non-Admin users, the script is not working correctly. Correct Cart and Item details are not fetched. 

Please let me know how this can be fixed so that correct cart items can be fetched when Non-Admin users are submitting request.

 

I am not sure if there is any limitation by ServiceNow on this.

 

Best Regards,

Subhashree

Ankur Bawiskar
Tera Patron
Tera Patron

@Subhashree Sub2 

why not do this?

1) submit single RITM with the asset info

2) on that catalog item, have your flow and use "Submit Catalog Item Request" flow action to create those many RITMs, update the REQ of newly created RITMs with the REQ of manually submitted Request.

Share your current script here.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader