catalog client script for a catalog item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2023 06:45 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2023 07:13 AM - edited 06-01-2023 07:14 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2023 07:14 AM
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
Shravan
Please mark this as helpful and correct answer, if this helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2023 12:11 PM
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
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2023 12:59 PM
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'); /
}
Regards,
Riya Verma