in servicenow, in catalog item , i have a reference field but when i select it and…
- User
in servicenow, in catalog item , i have a reference field but when i select it and submit, it is showing the sys_id in the RITM variables section
- Assistant
In ServiceNow, if you have a reference field in a catalog item and upon submission you are seeing the
sys_idinstead of the display value, it can be corrected by adjusting the way the reference field is configured or how the displayed values are handled in the record.Here are a few steps you can take to ensure that the display values are saved and shown correctly in the Requested Item (RITM) variables:
-
Check the Variable Configuration:
- Go to Service Catalog > Catalog Definition > Variables.
- Find the variable that's configured as a reference field.
- Ensure the correct table is being referenced. The reference field should point to a valid table that contains the data you expect to see.
-
Set Display Value:
- In your variable, make sure that the checkbox for "Display value" is enabled. This ensures that the display value is stored when the item is submitted.
-
Review the Catalog Item & Variable Types:
- Go to Service Catalog > Catalog Definition > Maintain Items. Open your catalog item and check if the variable type is correctly set to Reference.
- If the variable is correctly set as a reference variable, verify that there’s no additional client script or business rule altering the behavior of the variable after submission.
-
Look for Client Scripts:
- Review any client scripts attached to the catalog item that might be manipulating the variable's value before submission. If there are scripts that are changing or assigning the value to the
sys_id, they may be causing the issue.
- Review any client scripts attached to the catalog item that might be manipulating the variable's value before submission. If there are scripts that are changing or assigning the value to the
-
Business Rules:
- Check for any business rules after the submission of the RITM that could alter or misinterpret the reference field’s data. Ensure that no business rule is forcing the system to store only the
sys_id.
- Check for any business rules after the submission of the RITM that could alter or misinterpret the reference field’s data. Ensure that no business rule is forcing the system to store only the
-
Use GlideAjax for Custom Logic:
- If you need to display a specific value from the selected reference table in the RITM variables, you might consider using a
GlideAjaxcall to fetch the display values after selection via client script and update a hidden field accordingly.
- If you need to display a specific value from the selected reference table in the RITM variables, you might consider using a
-
Testing:
- Create a test catalog item with just the reference field and see if the issue persists. This helps isolate whether the problem is specific to your implementation.
After ensuring these configurations, submit the catalog item again and check if the
sys_idstill appears instead of the display value. If you continue to face issues, it may be useful to consult the ServiceNow documentation or reach out to your ServiceNow administrator for more personalized assistance. -
- User
it is showing the names on catalog item form, but in ritm it is showing the sys_id
- Assistant
If the reference field in your ServiceNow catalog item is showing the correct display name on the form but storing the
sys_idin the Requested Item (RITM) variables after submission, it typically means that the variable is defined correctly to display the text but is not configured to save the display value in the RITM.Here are steps to troubleshoot and resolve this issue:
1. Verify Variable Configuration
-
Check the Variable Type:
- Navigate to Service Catalog > Catalog Definition > Variables.
- Locate the variable and ensure it is set as a Reference type variable.
-
Check for 'Display Value' Option:
- Confirm whether the variable has an option for "Store Display Value" or similar (if available in your version).
- Ensure that this option is checked if it exists. If it isn’t an option, ServiceNow will default to storing the
sys_id.
2. Use a Script to Store Display Values
If the platform doesn’t allow for setup to automatically store the display value, you can use a client script or business rule to achieve this:
Option A: Client Script
You can create a client script that runs on submit to copy the display value to another variable. Here's a basic version:
-
Create a New String Variable:
- Add a new variable of type String to the catalog item to store the display value.
-
Client Script Example:
- Create a new Client Script with the following configuration:
- Type: onSubmit
- Script:
function onSubmit() { var refValue = g_form.getValue('your_reference_variable'); // replace with your reference field name var refDisplayValue = g_form.getDisplayBox('your_reference_variable').value; // get the display value g_form.setValue('new_string_variable_name', refDisplayValue); // replace with the new string variable name }
- This script copies the display value into a new string variable before the form is submitted.
- Create a new Client Script with the following configuration:
Option B: Business Rule
Alternatively, you could use a business rule to set the variable value based on the selected reference:
-
Create a Business Rule:
- Navigate to System Definition > Business Rules.
- Create a new business rule that fires after the RITM is created.
- Add a condition to ensure it only runs when your catalog item is being ordered.
-
Business Rule Script:
(function executeRule(current, previous /*null when async*/) { var refGr = new GlideRecord('your_reference_table'); // replace with your reference table if (refGr.get(current.variables.your_reference_variable)) { // replace with your reference variable name current.variables.new_string_variable_name = refGr.name; // replace with your display name field } })(current, previous);
3. Confirm Output in RITM
After implementing the above changes, submit a new request for your catalog item and check the RITM variables again. The field you've set up to store the display value should reflect the display name rather than the
sys_id.4. Possible Limitation
Keep in mind that these operations may introduce some additional complexity and potential performance implications if not managed properly. Always test scripts and business rules in a development instance before applying changes to production systems.
If the problem persists after these adjustments, please check with your ServiceNow administrator or consult ServiceNow support, as there might be customizations or specific configurations in your instance that are contributing to this behavior.
-
- User
i wrote a script include that label:depgr.name.toString(); value: depgr.sys_id.toString();
- Assistant
It looks like you are trying to structure a script include within ServiceNow to return values for a scripted reference field or to use in a GlideAjax call. To give you the correct guidance, let's clarify the context and ensure the script is effectively retrieving and returning the values you want.
Example of Script Include
Below, I'll provide a simple example of a Script Include that fetches a reference field's
nameandsys_id. Make sure to follow the naming conventions and settings for the Script Include to ensure it works correctly.Step 1: Creating the Script Include
- Navigate to System Definition > Script Includes in ServiceNow.
- Click New to create a new Script Include.
- Use the following code as a template:
var DepGrHelper = Class.create(); DepGrHelper.prototype = { initialize: function() { }, getDepGrNameAndID: function(depGrSysId) { var depGr = new GlideRecord('your_table_name'); // replace 'your_table_name' with the actual table name if (depGr.get(depGrSysId)) { return { name: depGr.name.toString(), sys_id: depGr.sys_id.toString() }; } return null; // Return null or an appropriate response if the record is not found }, type: 'DepGrHelper' };Step 2: Using GlideAjax to Call the Script Include
If you're calling this Script Include from a client script (e.g., when a user changes a value on the catalog item), you can use GlideAjax as follows:
- Create a Client Script on your catalog item or wherever it is appropriate.
- Use the following example code:
function getDepGrInfo(depGrSysId) { var ga = new GlideAjax('DepGrHelper'); ga.addParam('sysparm_name', 'getDepGrNameAndID'); ga.addParam('sysparm_depgr_sys_id', depGrSysId); ga.getXMLAnswer(function(response) { var answer = JSON.parse(response); if (answer) { // Do something with answer.name and answer.sys_id // For example, set these values in other fields g_form.setValue('another_field_name', answer.name); // Set a field with the name // You could also store the sys_id if needed } }); }Step 3: Make Sure of the Following
-
Replace Placeholder Values: Change
your_table_nameto the actual table where you are retrieving data from, and replace other field names (name,another_field_name, etc.) accordingly. -
Client Script Trigger Point: Ensure you call
getDepGrInfo(depGrSysId)appropriately, e.g., in anonChangescript for the reference field. -
Return JSON Correctly: Ensure that in your Script Include, you are returning data in a JSON-compatible format so it can be parsed easily on the client side.
Additional Notes
- Testing: Always test in a development environment to ensure everything works as expected, checking for any errors in the browser console or logs.
- Permissions: Ensure that the correct permissions are set on both the Script Include and the tables you're accessing; otherwise, you might encounter access issues.
By following the above guidelines, you should be able to correctly retrieve and display the values you're looking for from your reference field in ServiceNow. Let me know if you need any more help!
- User
i wrote this script include and calling it in catalog client script to populate the field, but in RITM variables it is showing sys_id, i need to show the name:
var GetDepartmentsByCompany = Class.create();
GetDepartmentsByCompany.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDepartments: function() { var companySysId = this.getParameter('sysparm_company'); // Get the company sys_id from the client script var businessUnits = []; var departments = []; // Fetch all business units for the given company if (companySysId) { var buGr = new GlideRecord('business_unit'); buGr.addQuery('company', companySysId); buGr.query(); while (buGr.next()) { businessUnits.push(buGr.sys_id.toString()); // Store business unit sys_ids } }// Fetch all departments for the fetched business units
if (businessUnits.length > 0) { var deptGr = new GlideRecord('cmn_department'); deptGr.addQuery('business_unit', 'IN', businessUnits); deptGr.query(); while (deptGr.next()) { departments.push({ label:deptGr.name.toString(), value: deptGr.sys_id.toString() // Assuming "name" is the department name field }); } } // Return the departments as a JSON string for the client script return JSON.stringify(departments) || '[]'; }, type: 'GetDepartmentsByCompany'});
- Assistant
It looks like you are already on the right track with your Script Include for fetching departments based on a selected company's Sys ID. The Script Include itself seems correct in how it retrieves and formats the department names and sys_ids for the client script. However, if the Requested Item (RITM) variables are still showing the
sys_idinstead of the names after selection by the user, the issue is likely related to how you're handling the selected values in the client script.Steps to Improve the Client-Side Handling
- Modify Your Client Script:
Ensure that in your Client Script, you're correctly using the returned
label(name of the department) when you set the field value to the form. If you want to display the name instead of thesys_id, make sure that you are correctly assigning the label to the appropriate variable in the form, if needed.
Here’s an example of how your client script could look when integrated with your Script Include:
function onChange(control, oldValue, newValue, isLoading) { // Check if the control that triggered the change is the company field if (isLoading || newValue == '') { return; } // Start the GlideAjax call to retrieve departments var ga = new GlideAjax('GetDepartmentsByCompany'); ga.addParam('sysparm_name', 'getDepartments'); ga.addParam('sysparm_company', newValue); // Company Sys ID ga.getXMLAnswer(function(response) { var departments = JSON.parse(response); var departmentDropdown = g_form.getField('your_department_variable_name'); // Replace with your field name // Clear the existing options in the reference field / dropdown departmentDropdown.options.length = 0; // Populate the dropdown with the fetched departments for (var i = 0; i < departments.length; i++) { var option = new Option(departments[i].label, departments[i].value); departmentDropdown.add(option); } }); }2. Set Up the Variable Correctly
Make sure that the variable used to store the department selection is set up as a Reference field to the
cmn_departmenttable in your catalog item. When the user selects an item from the dropdown after the retrieval, it should correctly point back to the sys_id of the selected department in thecmn_departmenttable.3. Ensure Display Value Configuration
Sometimes, the default behavior of ServiceNow is to store and retrieve sys_ids rather than names. Ensure that the variable associated with the department in the catalog item is configured to display a name instead of a sys_id.
- Variable Configuration:
- Navigate to Service Catalog > Catalog Definition > Variables.
- Find your variable for the department and ensure it is of type Reference.
- Enable any options that allow storing/displaying the display value as needed.
Example of Variable:
In your catalog item, the variable should be something like this:
- Type: Reference
- Reference Table: cmn_department
- Name: department_variable (this should be the field name you're using in client script).
4. Review the RITM Creation Logic
If you're still seeing the
sys_idin the RITM variables after the selection, double-check any other scripts (like Business Rules or additional Client Scripts) that might be affecting how the field is saved on submission or changing values post-selection.Conclusion
By following these steps and ensuring that all parts of your implementation work together correctly, the department names should correctly display in the RITM variables instead of the sys_ids. If you've implemented the above suggestions correctly and are still facing issues, consider checking the instance logs for any errors during form submission or any other related events that might help troubleshoot the problem further.
- Modify Your Client Script:
Ensure that in your Client Script, you're correctly using the returned
- User
i hvae this catalog client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') { return; // Skip execution if the form is loading or the field is empty } // Get the selected company value var companySysId = g_form.getValue('which_company_is_this_for'); // Replace with the actual variable name // If the company is selected if (companySysId) { // Call the Script Include via GlideAjax to get departments var ga = new GlideAjax('GetDepartmentsByCompany'); ga.addParam('sysparm_name', 'getDepartments'); ga.addParam('sysparm_company', companySysId); ga.getXMLAnswer(function(response) { // Parse the response (JSON string) into an array var departmentChoices = JSON.parse(response || '[]'); // Clear existing options in the department variable g_form.clearOptions('which_department_is_this_for'); //// Add a default "Select a department" option
g_form.addOption('which_department_is_this_for', '', '--Select a department--'); // Add each department as an option in the dropdown departmentChoices.forEach(function(choice) { g_form.addOption('which_department_is_this_for', choice.value, choice.label); }); // If a department was selected, retrieve the department name using GlideRecord if (g_form.getValue('which_department_is_this_for')) { var departmentSysId = g_form.getValue('which_department_is_this_for'); // Get selected department sys_id// Direct GlideRecord query to fetch department name
var deptGr = new GlideRecord('cmn_department'); if (deptGr.get(departmentSysId)) { var departmentName = deptGr.name; // Get department name g_form.setValue('dummy_variable', departmentName); // Set department name in the hidden field } else { g_form.setValue('dummy_variable', ''); // Clear if not found } } else { g_form.setValue('dummy_variable', ''); // Clear if no department is selected } }); } else { // Clear the department dropdown if no company is selected g_form.clearOptions('which_department_is_this_for'); g_form.addOption('which_department_is_this_for', '', '--Select a department--'); // Clear the hidden field g_form.setValue('dummy_variable', ''); }}
- Assistant
Your catalog client script looks well-structured and is designed to update the department options based on the selected company. However, it's important to note how you are managing the department selection and what is shown in the RITM variables section. Here are some important points to ensure that the department names are correctly displayed and stored in the RITM:
Key Considerations and Improvements
-
Understanding Reference Variables:
- In ServiceNow, when you select a reference field (like your department field), it typically stores the
sys_idof the selected record. This is standard behavior since the reference field needs to link directly to a specific record in thecmn_departmenttable.
- In ServiceNow, when you select a reference field (like your department field), it typically stores the
-
Using Display Values:
- For display purposes, you will need to ensure that any "display" or "label" is fetched or set correctly in the appropriate fields. However, remember that the actual storage will still be based on the
sys_id. The name should be fetched to a separate field if needed, like in your script to the hiddendummy_variable.
- For display purposes, you will need to ensure that any "display" or "label" is fetched or set correctly in the appropriate fields. However, remember that the actual storage will still be based on the
-
Setting Up the Dummy Variable:
- If you want the department name to show in the RITM without using
dummy_variable, you need to think about how you want to handle the actual output being saved. If the goal is to see both the name and sys_id somehow, you might have to store the name in another field that's visible on the RITM instead of a "dummy" variable.
- If you want the department name to show in the RITM without using
Suggested Modifications
-
Ensure Proper Logic for Adding Options: – Your option addition logic is correct. However, ensure the field you are adding options to (
which_department_is_this_for) is a reference field pointing tocmn_department. -
Adjust onChange Logic for Selected Department:
- In the section where you get the selected department, it may be beneficial to separate setting the department name in a different variable that can be used to display to users.
Here is an improved snippet of your
onChangefunction where it can directly set the department name, while still storing thesys_idbehind the scenes.Revised Client Script
function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue === '') { return; // Skip execution if the form is loading or the field is empty } // Get the selected company value var companySysId = g_form.getValue('which_company_is_this_for'); // Replace with your actual variable name // If the company is selected if (companySysId) { var ga = new GlideAjax('GetDepartmentsByCompany'); ga.addParam('sysparm_name', 'getDepartments'); ga.addParam('sysparm_company', companySysId); ga.getXMLAnswer(function(response) { var departmentChoices = JSON.parse(response || '[]'); // Clear existing options in the department variable g_form.clearOptions('which_department_is_this_for'); g_form.addOption('which_department_is_this_for', '', '--Select a department--'); // Add each department as an option in the dropdown departmentChoices.forEach(function(choice) { g_form.addOption('which_department_is_this_for', choice.value, choice.label); }); }); } else { // Clear the department dropdown if no company is selected g_form.clearOptions('which_department_is_this_for'); g_form.addOption('which_department_is_this_for', '', '--Select a department--'); g_form.setValue('dummy_variable', ''); // Clear the hidden field } } // onChange handler for the department selection field if you want to update dummy_variable function onDepartmentChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue === '') { return; // Skip execution if the form is loading or the field is empty } var departmentSysId = newValue; // Use the new value (sys_id) var deptGr = new GlideRecord('cmn_department'); if (deptGr.get(departmentSysId)) { var departmentName = deptGr.name; // Get the department name g_form.setValue('dummy_variable', departmentName); // Set department name in a hidden field } else { g_form.setValue('dummy_variable', ''); // Clear if not found } }Key Changes Made:
- Separated Logic for Department Selection: I've broken out the logic to set the department name into a separate
onDepartmentChangefunction. You can then link this directly to your department field onChange handler. - Reference Field Behavior: Remember that the reference field (
which_department_is_this_for) continues to store thesys_id, as is standard behavior in ServiceNow.
RITM Data Display:
- To see the display value in the RITM, refer to the hidden field (
dummy_variable). If you ever want the department name to live somewhere for business logic, use that field for processing or displaying in the RITM.
Final Note:
Ultimately, if the requirement is to visually show the department name in the RITM, ensure that any necessary fields are used and displayed correctly. The saving of the real reference (
sys_id) is how ServiceNow maintains data integrity. -