catalog client script for a catalog item

HemaLatha Koyya
Tera Contributor

 

Hi,

Create a catalog client script to any catalog item to display, hide fields, add option to choice fields, and set default values to any fields presented in that particular catalog item. 

4 REPLIES 4

Manmohan K
Tera Sage

Hi @HemaLatha Koyya 

 

You can create a catalog client script referencing below code for the use cases mentioned

 

 

g_form.setDisplay("variable_name","true");  // to show fields

g_form.setDisplay("variable_name","false");  //to hide fields

g_form.addOption('variable_name', 'option value', 'option Label');  //to add aoption to choice field

g_form.setValue("variable_name","value");   //to set value

 

 

Sai Shravan
Mega Sage

Hi @HemaLatha Koyya ,

 

Here's an example of a catalog client script in ServiceNow that demonstrates how to display/hide fields, add options to choice fields, and set default values for fields in a specific catalog item. You can customize this script to match your specific catalog item and field names:

function onLoad() {
  // Field display and visibility
  g_form.setDisplay('field_name', false); // Hide a field
  g_form.setDisplay('field_name', true); // Show a field
  
  // Add options to a choice field
  var choiceField = g_form.getControl('choice_field_name');
  choiceField.addOption('option_value', 'Option Label'); // Add a new option
  
  // Set default values for fields
  g_form.setValue('field_name', 'default_value'); // Set a default value
}

Replace 'field_name' with the actual name of the field you want to display, hide, or set a default value for. Similarly, replace 'choice_field_name' with the name of the choice field you want to modify.You can add multiple lines of code to modify different fields as needed. Make sure to include this client script in the catalog item where you want these changes to be applied.

 

Note that field names in ServiceNow are case-sensitive, so ensure you use the correct field names as they appear in the catalog item form.

 

Regards,

Shravan

Please mark this as helpful and correct answer, if this helps you

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Karan Chhabra6
Mega Sage
Mega Sage

Hi @HemaLatha Koyya ,

 

You can set the default value of any catalog variable by specifying it in the default value filed on the variable record

KaranChhabra6_0-1685646511624.png

 

 

You can show/hide fields via UI policies as well.

 

If my answer has helped with your question, please mark it as correct and helpful

 

Thanks!

Riya Verma
Kilo Sage
Kilo Sage

Hi @HemaLatha Koyya ,

 

Hope you are doing great.

 

Try Using below script using your field and options for achieving requirement:

function onChange() {
  // Display or hide fields
  g_form.setDisplay('field_name', condition); 

  // Add options to choice fields
  var field = g_form.getControl('choice_field_name'); 
  field.addOption(value, label);

  // Set default values ot field
  g_form.setValue('field_name', 'default_value'); /
}
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma