Copy RITM in portal - Variables not passing to new catalog item from UI Button widget

Earl Begley
Tera Guru

I have a requirement to allow a customer to:

  • copy a RITM to a new catalog item from the portal
  • Display the new catalog item in the portal
  • review and update the variables of the catalog item
  • Submit the catalog item with the updated variables

 

My script (see below) is opening the new cat_item and the URL is showing the variables, but the form fields are blank.
Any ideas? All help greatly appreciated.

(function() {

// Get table & sys_id
data.table = input.table || $sp.getParameter("table");
data.sys_id = input.sys_id || $sp.getParameter("sys_id");

// Validate table and sys_id
var gr = new GlideRecord(data.table);
if (!gr.isValid()) {
data.error = "Invalid table specified.";
return;
}

if (!gr.get(data.sys_id)) {
data.error = "Invalid sys_id or record not found.";
return;
}

try {
var catalogItem = gr.cat_item; // Get associated catalog item

// Handle cases where cat_item is blank
if (!catalogItem) {
data.error = "No associated catalog item found for this RITM.";
return;
}

// Prepare the base URL for the catalog item form
var url = "/ohd?id=sc_cat_item&sys_id=" + catalogItem;

// Add variables from the original RITM as a query string
var variables = gr.variables.getElements();
var sysparmQuery = [];
for (var i = 0; i < variables.length; i++) {
var question = variables[i].getQuestion();
var name = question.getName();
if (name) {
var value = gr.variables[name] ? gr.variables[name].toString() : ""; // Safely retrieve variable value
value = encodeURIComponent(value); // Encode the value for URL
sysparmQuery.push(name + "=" + value); // Append variable as a key-value pair
}
}

// Add sysparm_query to the URL
if (sysparmQuery.length > 0) {
url += "&sysparm_query=" + sysparmQuery.join("^");
}

// Return the constructed URL
data.url = url;
} catch (ex) {
gs.error("Error while copying RITM to catalog item: " + ex.message);
data.error = "An unexpected error occurred.";
}

})();

 

 

1 ACCEPTED SOLUTION

Earl Begley
Tera Guru

I have updated the server script code in the widget to
// Copy all variables from the original RITM to the new one
var originalVars = new GlideRecord('sc_item_option_mtom');
originalVars.addQuery('request_item', ritmSysId);
originalVars.query();
while (originalVars.next()) {
var newVar = new GlideRecord('sc_item_option_mtom');
newVar.initialize();
newVar.request_item = newRitmSysId;
newVar.sc_item_option = originalVars.sc_item_option;
newVar.item_option_new = originalVars.item_option_new;
newVar.value = originalVars.value;
newVar.insert();
}

All variables are now appearing

View solution in original post

5 REPLIES 5

Earl Begley
Tera Guru

I have updated the server script code in the widget to
// Copy all variables from the original RITM to the new one
var originalVars = new GlideRecord('sc_item_option_mtom');
originalVars.addQuery('request_item', ritmSysId);
originalVars.query();
while (originalVars.next()) {
var newVar = new GlideRecord('sc_item_option_mtom');
newVar.initialize();
newVar.request_item = newRitmSysId;
newVar.sc_item_option = originalVars.sc_item_option;
newVar.item_option_new = originalVars.item_option_new;
newVar.value = originalVars.value;
newVar.insert();
}

All variables are now appearing