How to copy submitted Request item in Service Portal?

Aki17
Kilo Guru

As the attached image below, I would like to copy Request items by clicking on UI Action button.
After clicking on it, all the variable values should be auto-populated into newly created(but not submitted) form of Request item in Service Portal.
After that, the requester will confirm the copied values and modify some of them as necessary, and then submit new Request item.

find_real_file.png

 

I believe the article below is a good example to create the UI action, but 'm not sure how to make the script work to copy the variable values in Server script of the widget.

https://serviceportal.io/create-custom-action-buttons-service-portal/


Please kindly give me some advice.

Best Regards,

Aki

16 REPLIES 16

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can get the current RITM sys_id in the server side and then use Cart API to create RITM and REQ and set variable data using Cart API

Regards
Ankur

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

Hi @Ankur Bawiskar,

Thank you for your reply.

Could you please give me the sample script? I'm not very familiar with scripting..

Hi,

sample script in server side of widget

(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);

		//fill in the variables on the request item form
		cart.setVariable(item, "variable1", gr.variables.variable1);
		cart.setVariable(item, "variable2", gr.variables.variable2);
		var rc = cart.placeOrder();
		gs.info(rc.number);

	}
	catch(ex){
		gs.info(ex);
	}



})();

Regards
Ankur

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

Hi @Ankur Bawiskar ,

Thank you so much for the sample script.

 

However, each catalog item has different variables, so I think it's difficult to set fixed variable names in the UI Action script.

*We will have almost 60 items.

Do you have any ideas to get all variables in accordance with the type of catalog item dynamically?