How to show catalog variables based on selection of a catalog item names in a select box

srinureddy1642
Tera Contributor

I have total 17 catalog items. Now I want to merge all those into 1 catalog item(dont need order guide).

I have created 1 select box and i added 17 catalog names. Based on the selection catalog item i want to show related variables of that catalog item dynamically without creating all the variables.

7 REPLIES 7

PritamG
Mega Guru

Create a single parent catalog item with a Lookup Select Box listing 17 catalog items. Use a Catalog Client Script with GlideAjax to dynamically fetch and show variables from the selected catalog item using sc_item_option_mtom and sc_item_option tables. This avoids creating separate variables for each catalog item.

use this scripts:
client script
 

 

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue === '') {
    return;
  }

  var ga = new GlideAjax('DynamicCatalogVariablesLoader');  // Script Include name
  ga.addParam('sysparm_catalog_item', newValue);  // Pass selected catalog item ID
  ga.getXMLAnswer(function(response) {
    var variableData = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
    displayVariables(variableData);  // Function to handle variable display
  });
}

function displayVariables(variableData) {
  // Clear previously loaded variables
  g_form.clearMessages();
  
  // Display variables dynamically
  variableData.forEach(function(variable) {
    g_form.setDisplay(variable.name, true);  // Set the visibility of relevant variables
  });
}

 

Script Include

 

var DynamicCatalogVariablesLoader = Class.create();
DynamicCatalogVariablesLoader.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  getVariables: function() {
    var catalogItem = this.getParameter('sysparm_catalog_item');
    var result = [];
    
    var gr = new GlideRecord('sc_item_option_mtom');
    gr.addQuery('sc_cat_item', catalogItem);
    gr.query();
    
    while (gr.next()) {
      var variableName = gr.sc_item_option.name.toString();
      result.push({ name: variableName });
    }
    
    return JSON.stringify(result);
  }
});

 

yuvarajkate
Giga Guru

An Order Guide would be the best option for this problem but since you do not need it to be an Order guide but a single catalog item you can try and work around this approach.

To create multiple RITMs under a single request without using an order guide, you can design a single catalog item with a select box listing the 17 catalog items. When a user selects an option, server-side logic dynamically handles the creation of the RITMs. A script include can query the sc_cat_item table for the selected catalog item and retrieve its related variables from the sc_item_option table. This approach eliminates the need for creating all variables upfront and provides a dynamic, scalable solution.

On form submission, the server-side script generates individual RITMs for each selected catalog item. This can be achieved by scripting the RITM creation manually. Each RITM is populated with the relevant variables and values associated with the selected catalog item, ensuring that the generated records accurately reflect the user's choices. The script then ensures all the created RITMs are grouped under a single request, allowing users to see a consolidated request for their selections.

This design replicates the behavior of an order guide but operates within a single catalog item. 

See if it works.

Ravi Gaurav
Giga Sage
Giga Sage

Hi @srinureddy1642 

Its not straight forward.. once you choose the catalog item from the drop down... we need place holder to show variables.. as per your request we no need to create all variables.. then my suggestion will be create a UI page link your variable there and use macro type on catalog and that should be working..

if you need sample script I can give you 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

Hi Ravi,

 

Can you please provide me the sample code. if possible please send snapshots how it look loike

 

Thanks,

Sreenivasa Reddy