Clone Requested Items
| For Service Catalog Items with many fields, it could be helpful to re-order an existing Requested Item by prefilling the new request form with the same values of the RITM to be cloned. This is always useful, for example, if you do not want to enter everything again manually. There are several approaches to this challenge, and my article describes one of them. | | Table of Contents Approach Limitations Solution |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | --------------------------------------------------------------------------------------------- |
Approach
On RITM records we need a button (UI Action) "Clone" which pulls all variable values and put them into a session object for a transfer to the catalog form. Of course, it would also be possible to fetch all the data on client-side with an GlideAjax call, but transferring the data from the server to the client via a session object is more elegant, secure and error-prone.
On the Catalog Item an additional Client Script is required to recognize the variable value transfer and to fill that values into the form. That Client Script is contained in a Variable Set. In this way, the Variable Set can be easily embedded in each Catalog Item, which should support prefilling of the form elements.
Limitations
That approach only works for requests that consist of a single catalog element and therefore do not represent a bundle or an order guide. In addition, the "Clone" button is only available in the UI16 and therefore not in the Service Portal or in a Workspace.
Solution
Script Include
The Script Include CatalogUtils provides a method getVariables() that returns all variables of a given RITM as stringified array of objects. Each of these objects corresponds to a respective Catalog Variable and has the following properties:
strType: integer value of the variable type or "MVRS" in case the Catalog Item contains Multirow Variable Sets
strName: variable name
strQuestionText:label text for a variable
strValue:the underlying variable value, for example for a reference variable it would be the Sys ID of the target record
strDisplayValue:
the text which is rendered for the variable, for example for a reference variable pointing to the sys_user table it would be the complete name of the referenced user
You can go to https://github.com/mskoddow/sn-scripts/blob/master/CatalogUtils.js for loading the source code of that Script Include.
UI Action
Create a new UI Action with the following parameters:
| Name | Clone |
|---|---|
| Table | Requested Item (sc_req_item) |
| Form Button | (checked) |
| Show update | (checked) |
| Condition | (define an appropriate condition here to make the UI action available only for authorized users) |
| Script | //store all variable values in a session object gs.getSession().putClientData( 'variable_values', new CatalogUtils().getVariables(current) ); //redirect to the catalog form //URL parameter sysparm_clone indicates the request for prefilling gs.setRedirect( 'com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=' + current.cat_item + '&sysparm_clone=true' ); |
Variable Set
Create a Single-Row Variable Set:
and name it "Basic Initialization" (or choose another name).
This Variable Set will only contain the following Catalog Client Script.
Catalog Client Script
Go to Related List "Catalog Client Scripts" and create a new record:
with the following parameters:
| Name | Prefill form with variable values | |||||
|---|---|---|---|---|---|---|
| Applies to | Variable Set | |||||
| UI Type | Desktop | |||||
| Type | onLoad | |||||
| Variable Set | Common Initialization | |||||
| Applies on a Catalog Item view | (checked) | |||||
| Script | function onLoad() { var objUrl = new GlideURL(); objUrl.setFromCurrent(); var isCloneMode = objUrl.getParam("sysparm_clone") \ | ''; var strVariables = g_user.getClientData('variable_values') | ''; if (isCloneMode == 'true' && strVariables.length > 0) { var arrVariables = JSON.parse(strVariables); for (var i = 0; i < arrVariables.length; i++) { var strType = arrVariables[i].strType | |||
| Screenshot |
Add Variable Set to Catalog Items
The last step is adding the Variable Set to all Catalog Items, which should be able to prefill the form.
For testing purposes, you can pick the OOTB Catalog Item "Sample Item". Order one item and then clone it. In the order form for the clone, all fields should be filled correctly:
A good practice for Catalog Management is to always include such a universal Variable Set into each Catalog Item. That way, you can implement or add common behaviors later without touching hundreds of Catalog Items.
https://www.servicenow.com/community/now-platform-articles/clone-requested-items/ta-p/2323321
