How do i auto populate a singleLine Text based on chosen Reference Field?

ronro2
Tera Contributor

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! 

2 REPLIES 2

Mark Roethof
Tera Patron

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

Amit Gujarathi
Giga Sage

HI @ronro2 ,
I trust you are doing great.

Here's a step-by-step solution:

  1. Identify the reference field on your form where the user selects the value.

    • Let's assume the reference field is named "chosen_reference".
  2. 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);