To achieve the functionality where selecting a dropdown (choice) value in a ServiceNow form populates another field automatically based on the choice, and both the dropdown values and the field value come from the same table record, you'll likely use a combination of a Reference field (for the dropdown) and Client Script or Business Rule (to auto-populate another field based on the selected value). This is a common requirement for offering a dynamic form experience in ServiceNow.
Step-by-step Implementation:
Step 1: Create/Set Up the Reference Field
First, ensure your dropdown field is set up as a Reference field pointing to the desired table where your dropdown values and the connected information reside. Reference fields inherently allow the selection of a record from a specific table, making them perfect for this use case.
- Go to the form where you want to add the dropdown.
- Configure the form layout to add a new field.
- Set the field type to Reference, and select the table that contains your dropdown values and the related record information.
Step 2: Implement a Client Script to Populate the Field
Next, create a Client Script to listen for changes on the dropdown (Reference field) and populate your target field based on the selected value.
- Navigate to the System Definition > Client Scripts.
- Click New to create a new Client Script.
- Set the Name, and choose the Table where your form belongs.
- In the Script field, enter your JavaScript code. Here’s a simple example assuming your Reference field is named
referenced_field, and you want to populate target_field with the description (or any relevant column) from the selected record on dropdown change:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Makes sure a value has been selected
if (newValue) {
// Perform a GlideAjax call to get the detail from the selected record
var ga = new GlideAjax('YourScriptInclude');
ga.addParam('sysparm_name', 'getRecordDetail');
ga.addParam('sysparm_record_sys_id', newValue); // Pass the sys_id of the selected record
ga.getXMLAnswer(function(response){
// Parse the response, and set the desired field's value
var result = response; // Assuming response is the value you want to set
g_form.setValue('target_field', result);
});
}
}
Step 3: Create a Script Include for Server-side Logic
Since Client Scripts run on the client side and cannot directly access server-side objects like GlideRecord for security reasons, you'll use a GlideAjax call to interact with a Script Include.
- Create a Script Include by navigating to System Definition > Script Includes.
- Set the Name (use YourScriptInclude or another name as referenced in your Client Script), and ensure it's Accessible from Client Scripts.
- In the script field, add your server-side logic to fetch the desired record and return the relevant information. Example:
var YourScriptInclude = Class.create();
YourScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getRecordDetail: function() {
var recordSysId = this.getParameter('sysparm_record_sys_id');
var result = '';
// Assuming 'your_table' is the table where your records are
var gr = new GlideRecord('your_table');
if(gr.get(recordSysId)) {
// Example: Extracting the 'description' field
result = gr.getValue('description');
}
return result;
},
type: 'YourScriptInclude'
});
Testing and Adjustments:
After implementing these components, test by selecting a value in your Reference field/dropdown on the form. The target field should automatically populate based on the selection. Ensure you adjust table names, field names, and any specific logic to fit your exact requirements and ServiceNow's best practices.