How to implement value association between different fields of two tables?

shiz
Tera Contributor

1.I have two tables, u_actuant_times and u_actuant_times_details, both of which have the u_assigend_to field. 2.Among them, the associated fields of u_actuant_times_details and u_actuant_times are u_ ref Monday, u_ ref Tuesday... u_ ref Sunday.

3.Now I want to implement the following function: when I modify the value of the u_assigend_to field in u_actuant_times, I also modify the value of the u_assigend_to field in the u_actuant_times_details table.

How should the above functions be implemented?

 

shiz_0-1762157837993.png

The above figure shows the u_ actual times table

 

shiz_1-1762157880710.png

 

The red box shows the structure of the u_actuant_times_tail table

 

shiz_2-1762157923895.png

I want to change the value of 'Assigned to' in u_actual_times in real-time after modifying the 'assigned to' in u'actual_times_detail. How to achieve it?

1 ACCEPTED SOLUTION

G Ponsekar
Tera Guru

Hi @shiz ,

 

Write After Business Rule in u_actuant_times table.

Update checkbox checked

Filter Condition: [u_assigned_to] [changes]

 

Script like:

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

    // Get the sys_id of the current (parent) record
    var parentSysId = current.getUniqueValue();
    // Get the new value of the u_assigned_to field
    var newAssignedTo = current.u_assigned_to.toString();

    // Query the u_actuant_times_details table for associated records
    var detailGr = new GlideRecord('u_actuant_times_details');
    // The details table has fields referencing the parent table (Monday, Tuesday, etc.)
    // We need to find records where any of these reference fields match the parent's sys_id.
    
    // Use an encoded query or multiple addQuery/addOrCondition statements    detailGr.addQuery('u_ref_monday', parentSysId);
    detailGr.addOrCondition('u_ref_tuesday', parentSysId);
    detailGr.addOrCondition('u_ref_wednesday', parentSysId);
    detailGr.addOrCondition('u_ref_thursday', parentSysId);
    detailGr.addOrCondition('u_ref_friday', parentSysId);
    detailGr.addOrCondition('u_ref_saturday', parentSysId);
    detailGr.addOrCondition('u_ref_sunday', parentSysId);
    
    detailGr.query();

    // Loop through all found detail records and update their u_assigned_to field
    while (detailGr.next()) {
        detailGr.u_assigned_to = newAssignedTo;
        detailGr.setWorkflow(false); // Prevents the update from triggering other business rules unnecessarily        detailGr.update();
    }

})(current, previous);

 

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks, GP

View solution in original post

6 REPLIES 6

G Ponsekar
Tera Guru

Hi @shiz ,

 

Write After Business Rule in u_actuant_times table.

Update checkbox checked

Filter Condition: [u_assigned_to] [changes]

 

Script like:

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

    // Get the sys_id of the current (parent) record
    var parentSysId = current.getUniqueValue();
    // Get the new value of the u_assigned_to field
    var newAssignedTo = current.u_assigned_to.toString();

    // Query the u_actuant_times_details table for associated records
    var detailGr = new GlideRecord('u_actuant_times_details');
    // The details table has fields referencing the parent table (Monday, Tuesday, etc.)
    // We need to find records where any of these reference fields match the parent's sys_id.
    
    // Use an encoded query or multiple addQuery/addOrCondition statements    detailGr.addQuery('u_ref_monday', parentSysId);
    detailGr.addOrCondition('u_ref_tuesday', parentSysId);
    detailGr.addOrCondition('u_ref_wednesday', parentSysId);
    detailGr.addOrCondition('u_ref_thursday', parentSysId);
    detailGr.addOrCondition('u_ref_friday', parentSysId);
    detailGr.addOrCondition('u_ref_saturday', parentSysId);
    detailGr.addOrCondition('u_ref_sunday', parentSysId);
    
    detailGr.query();

    // Loop through all found detail records and update their u_assigned_to field
    while (detailGr.next()) {
        detailGr.u_assigned_to = newAssignedTo;
        detailGr.setWorkflow(false); // Prevents the update from triggering other business rules unnecessarily        detailGr.update();
    }

})(current, previous);

 

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks, GP

Ravi Gaurav
Giga Sage
Giga Sage

@shiz  can you tell me what wrong with my Answer.
You can accept more than one answer !!

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/