How do i auto populate a singleLine Text based on chosen Reference Field?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2023 02:53 AM
I am working with a form, where the user is supposed to choose from a reference field. Based on the choice, I want a string to show up with details about billing adress (which is data that can be derived from that same reference field). Is that possiblen and how?
Greetings and thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2023 02:56 AM - edited 07-07-2023 02:56 AM
Hi there,
You could use a Client Script + Script Include to populate the value on change.
- 2020-01-10 - Article - Client Side Scripting: Go for GlideAjax (with getXMLAnswer)!
Perhaps you could also show a dotwalked field on the form, since you are mentioning that the data can be derived from the reference field.
Kind regards,
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2023 03:57 AM
HI @ronro2 ,
I trust you are doing great.
Here's a step-by-step solution:
Identify the reference field on your form where the user selects the value.
- Let's assume the reference field is named "chosen_reference".
Create a new Business Rule that triggers when the "chosen_reference" field is changed.
- Navigate to "Business Rules" in the ServiceNow Studio.
- Click on "New" to create a new Business Rule.
- Provide a name for the Business Rule, such as "Display Billing Address Details."
- Set the "Table" field to the table that contains the reference field.
- In the "Advanced" tab, enter the following script in the "Script" section:
(function executeRule(current, previous /*, g*/) {
// Get the sys_id of the selected reference value
var referenceValue = current.chosen_reference;
// Query the reference table to fetch the billing address details
var referenceTable = new GlideRecord('reference_table_name');
if (referenceTable.get(referenceValue)) {
// Retrieve the billing address fields from the reference table
var billingAddress = referenceTable.billing_address;
var city = referenceTable.city;
var state = referenceTable.state;
// Set the billing address details in the form field
current.billing_address_details = billingAddress + ', ' + city + ', ' + state;
}
})(current, previous);