Auto populate field

Helio-matos
Tera Contributor

How to auto populate "location" field based on "company" field in alm_hardware table in servicenow?

2 REPLIES 2

DUGGI
Giga Guru

@Helio-matos 

 

 

 

To auto-populate the "location" field based on the "company" field in the alm_hardware table in ServiceNow, you can use a Business Rule to set the location when a new hardware record is created or updated.

Follow these steps to create a Business Rule:

  1. Navigate to "System Definition > Business Rules" in ServiceNow.
  2. Click "New" to create a new Business Rule.
  3. Enter the following details:
    • Name: "Auto Populate Location based on Company"
    • Table: "alm_hardware"
    • When: "before"
    • Insert: Check the box
    • Update: Check the box
  4. In the "Advanced" tab, paste the following code into the "Script" field:
jsCopy code
(function executeRule(current, previous /*null when async*/) {
  if (current.company.nil()) {
    return;
  }

  // Replace this with the actual logic to determine the location based on the company
  var location = getLocationBasedOnCompany(current.company);

  if (location) {
    current.location = location;
  }
})();

function getLocationBasedOnCompany(company) {
  // Add your logic here to get the location based on the company.
  // This is just a simple example, you may need to use GlideRecord to query the location data
  var location;

  if (company == 'company_a_sys_id') {
    location = 'location_a_sys_id';
  } else if (company == 'company_b_sys_id') {
    location = 'location_b_sys_id';
  }

  return location;
}

  1. Click "Submit" to save the Business Rule.

Replace the example code in the getLocationBasedOnCompany function with your own logic to determine the appropriate location based on the company. You may need to use GlideRecord queries to fetch location data from the cmn_location table or any other table that stores your location data.

Now, whenever a new hardware record is created or updated in the alm_hardware table, the "location" field will be auto-populated based on the "company" field.

Karan Chhabra6
Mega Sage
Mega Sage

Hi @Helio-matos ,

 

OOB, The location field on the alm_hardware  form gets autopopulated from the 'assigned_to'  field.
Please search for this client script : Set Loc/CC/Dep/Com from assigned to

 

The company field references to the 'core_company' table which does not have the location field.

 

If my answer has helped with your question, please mark it as helpful

 

Thanks!!