Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Auto populate the data in one of the fields in a form from the related list of that form.

ParikshitY
Tera Contributor

In my form there is one field in that field data should be auto populate from the field in a related list of that form.so if user add more than one records in the related list. So from one of field in that related list I need to populate all the data showing in one of the columns(field) to the field which is in that form.

2 REPLIES 2

Community Alums
Not applicable

Hi @ParikshitY ,

 

  1. Determine the Parent and Related Table

    • Identify the parent table (e.g., parent_table) and the related list table (e.g., related_table).
    • Ensure the related list table has a field referencing the parent table (e.g., parent).
  2. Write a Business Rule on the Related Table

    • Use a Business Rule to update the field on the parent form whenever a record is added, updated, or deleted in the related list.

Business Rule Configuration

  • Table: related_table (the related list table).
  • When to Run: After Insert, After Update, and After Delete.
  • Advanced: Check "Advanced" to use a script.

Example Script

 

javascript
CopyEdit
(function executeRule(current, previous /*null when async*/) { // Get the parent record var parentRecord = new GlideRecord('parent_table'); // Replace 'parent_table' with your table name if (parentRecord.get(current.parent)) { // Assuming 'parent' is the reference field to the parent table // Query the related list var relatedList = new GlideRecord('related_table'); // Replace with the related list table name relatedList.addQuery('parent', current.parent); relatedList.query(); // Aggregate data from the related list field (e.g., 'u_related_field') var aggregatedData = []; while (relatedList.next()) { if (!JSUtil.nil(relatedList.u_related_field)) { aggregatedData.push(relatedList.u_related_field.toString()); } } // Update the field on the parent form parentRecord.u_aggregated_data = aggregatedData.join(', '); // Combine with commas or other delimiter parentRecord.update(); // Save the changes } })();
 

  1. Test the Business Rule
    • Add, update, or delete records in the related list.
    • Confirm that the form field (u_aggregated_data) auto-populates with the data from the related list field (u_related_field).

Considerations

  • Performance: If the related list contains a large number of records, the join operation might impact performance. Optimize the script or use pagination if necessary.
  • Access Control: Ensure the script has access to the parent and related tables.
  • Field Data Type: Ensure the data type of u_aggregated_data matches the aggregated content (e.g., String for concatenated values).

Community Alums
Not applicable

Hi @ParikshitY ,

Steps to Implement

  1. Determine the Parent and Related Table

    • Identify the parent table (e.g., parent_table) and the related list table (e.g., related_table).
    • Ensure the related list table has a field referencing the parent table (e.g., parent).
  2. Write a Business Rule on the Related Table

    • Use a Business Rule to update the field on the parent form whenever a record is added, updated, or deleted in the related list.

Business Rule Configuration

  • Table: related_table (the related list table).
  • When to Run: After Insert, After Update, and After Delete.
  • Advanced: Check "Advanced" to use a script.

 

(function executeRule(current, previous /*null when async*/) {
    // Get the parent record
    var parentRecord = new GlideRecord('parent_table'); // Replace 'parent_table' with your table name
    if (parentRecord.get(current.parent)) { // Assuming 'parent' is the reference field to the parent table
        
        // Query the related list
        var relatedList = new GlideRecord('related_table'); // Replace with the related list table name
        relatedList.addQuery('parent', current.parent);
        relatedList.query();

        // Aggregate data from the related list field (e.g., 'u_related_field')
        var aggregatedData = [];
        while (relatedList.next()) {
            if (!JSUtil.nil(relatedList.u_related_field)) {
                aggregatedData.push(relatedList.u_related_field.toString());
            }
        }

        // Update the field on the parent form
        parentRecord.u_aggregated_data = aggregatedData.join(', '); // Combine with commas or other delimiter
        parentRecord.update(); // Save the changes
    }
})();

 

 

  1. Test the Business Rule
    • Add, update, or delete records in the related list.
    • Confirm that the form field (u_aggregated_data) auto-populates with the data from the related list field (u_related_field).

Considerations

  • Performance: If the related list contains a large number of records, the join operation might impact performance. Optimize the script or use pagination if necessary.
  • Access Control: Ensure the script has access to the parent and related tables.
  • Field Data Type: Ensure the data type of u_aggregated_data matches the aggregated content (e.g., String for concatenated values).