Copy RITM Widget for Service Portal

Jorge Fernandez
Tera Expert

We have the requirement to create a 'Copy Request Item' widget that uses a previously submitted one. We add a new item into the cart and then copy over almost all the values from the previous RITM, at the end we load the cart page.

 

However we would like to end in a different page. We would like to open the cart item at the end to give it a final look before submitting, but I'm having trouble reaching that point. I don't find a way to access the sys id of the newly created item.

 

Any ideas?

1 ACCEPTED SOLUTION

Paul Deonarine3
Tera Expert

To open the newly created cart item in the cart page after copying over values from a previously submitted RITM, you can use the addVariable method to pass the sys_id of the new cart item to the cart page as a URL parameter. Here's an example script you can use in your widget:

 

function copyRequestItem() {
  // Get the sys_id of the RITM to be copied
  var originalRITM = g_form.getValue('original_ritm_field'); // replace with the actual field name

  // Copy over values to the new cart item
  // ...

  // Save the cart item
  var cart = new sn_sc.CatalogCart();
  cart.save();

  // Get the sys_id of the new cart item
  var newCartItemId = cart.request_item.sys_id;

  // Open the cart page with the new cart item
  var url = '/sc_cart.do?sys_id=-1&sysparm_item=' + newCartItemId;
  g_navigation.open(url);
}

 

In this script, original_ritm_field is the field on the form that contains the sys_id of the RITM to be copied. You'll need to replace this with the actual field name on your form.

The newCartItemId variable gets the sys_id of the newly created cart item from the request_item property of the CatalogCart object. This is then passed to the cart page URL as a URL parameter using the sysparm_item parameter.

Finally, the g_navigation.open method is used to open the cart page with the new cart item.

 

View solution in original post

5 REPLIES 5

Paul Deonarine3
Tera Expert

To open the newly created cart item in the cart page after copying over values from a previously submitted RITM, you can use the addVariable method to pass the sys_id of the new cart item to the cart page as a URL parameter. Here's an example script you can use in your widget:

 

function copyRequestItem() {
  // Get the sys_id of the RITM to be copied
  var originalRITM = g_form.getValue('original_ritm_field'); // replace with the actual field name

  // Copy over values to the new cart item
  // ...

  // Save the cart item
  var cart = new sn_sc.CatalogCart();
  cart.save();

  // Get the sys_id of the new cart item
  var newCartItemId = cart.request_item.sys_id;

  // Open the cart page with the new cart item
  var url = '/sc_cart.do?sys_id=-1&sysparm_item=' + newCartItemId;
  g_navigation.open(url);
}

 

In this script, original_ritm_field is the field on the form that contains the sys_id of the RITM to be copied. You'll need to replace this with the actual field name on your form.

The newCartItemId variable gets the sys_id of the newly created cart item from the request_item property of the CatalogCart object. This is then passed to the cart page URL as a URL parameter using the sysparm_item parameter.

Finally, the g_navigation.open method is used to open the cart page with the new cart item.

 

Hi Paul

is the above script to be written in Server side in Widget and that's all required or if you can give details from first point like how to copy to cart and then read from cart , edit variable values and submit new RITM.

priceless
Tera Expert

Hi @Paul Deonarine3 @Jorge Fernandez 

May I get the details from beginning. I tried to write in Server side script to get values from RITM variables etc and then save in cart and Open there itself so that user should edit values as per need and submit. But its not working. Script which I had written as beow:

 
(function() {
// Get table & sys_id
data.table = input.table || $sp.getParameter("table");
data.sys_id = input.sys_id || $sp.getParameter("sys_id");
 
// Valid GlideRecord
gr = new GlideRecord(data.table);
if (!gr.isValid())
return;
 
// Valid sys_id
if (!gr.get(data.sys_id))
return;
 
try{
 
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
//add your requested item to the cart by sys_id of the catalog item
var item = cart.addItem(gr.cat_item, 1);
 
// iterate over the variables
var variables = gr.variables.getElements(); 
}
// var rc = cart.placeOrder();
// var cart = new sn_sc.CatalogCart();
  cart.save();
 
  // Get the sys_id of the new cart item
  var newCartItemId = cart.request_item.sys_id;
 
  // Open the cart page with the new cart item
  g_navigation.open(url);
}
catch(ex){
gs.info(ex);
}
 
})();

 

You get the cartItemID at the Server script and then you pass the value to the Client Controller script to open the cart page