Beginner’s Guide: Solution for Auto-Populating data on the List View for Any Table in ServiceNow

Selva Arun
Mega Sage
Mega Sage

Beginner’s Guide: Solution for Auto-Populating data on the List View for Any Table in ServiceNow

Use Case: Solution for Auto-Populating “In Stock” Date on the alm_hardware Table (List View)

The Hardware Asset team had requested an enhancement to automatically populate the “In Stock” date (u_in_stock_date) field on the alm_hardware table whenever the state field changes to “In Stock” in the list view. This feature aims to achieve accurate tracking of hardware inventory and facilitate reporting.

selvarun_0-1713456565982.png

 

Implementation Details:

  1. onCellEdit() Client Script:

The onCellEdit() plays a crucial role in achieving this functionality. Let’s break down its logic:

selvarun_1-1713456565983.png

 

  • function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
  •     var saveAndClose = true;
  •     setTimeout(function() {
  •         this.window.location.reload();
  •     }, 300);
  •     callback(saveAndClose);
  • Function Purpose:
    • When a user edits the state field (e.g., changes it to “In Stock”), the onCellEdit()is triggered.
    • It performs the following steps:
      1. Sets saveAndClose to true.
      2. Schedules a page reload after a short delay (300 milliseconds).
      3. Invokes the provided callback function, indicating that the cell edit is complete.
  • Why Use onCellEdit()?
    • Real-time Update: By reloading the page, the function ensures that changes made to the state field are immediately reflected.
    • Improved User Experience: Users see accurate data without manual refreshes.
  1. Business Rule (BR) for Updating “In Stock” Date:

(function executeRule(current, previous /*null when async*/) {

 

    // Add your code here

    current.setValue("u_in_stock_date", new GlideDate());

 

})(current, previous);

selvarun_2-1713456565985.png

 

Before the actual update occurs, a business rule (BR) is executed. Here’s how it works:

  • Business Rule Purpose:
    • The BR triggers when the state field changes to “In Stock.”
    • It sets the value of the custom field u_in_stock_date to the current date using a GlideDate object.
  • Why Use a Business Rule?
    • Automation: Business rules allow us to automate actions based on specific conditions.
    • Data Consistency: By automatically updating the u_in_stock_date field, we ensure consistency and eliminate manual errors.

 

Summary

By combining the onCellEdit () and the business rule, we achieve seamless handling of state changes and accurate recording of “In Stock” dates. This solution enhances data reliability and streamlines inventory management. 🚀

 

0 REPLIES 0