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

Raghav Sharma24
Giga Patron

after update BR is the answer.

 

On "u'actual_times_detail" table write the after update BR and glide the another table and update the values.


Please mark the answer correct/helpful accordingly.

 

I have created a BR for 'after' in u'actual_times_tail, but I cannot obtain the sys_id for u'actual_times_tail.

shiz_0-1762159058557.png

 

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

    // 再帰呼び出しを防止するためのフラグ
    // このルールが自身を再トリガーするのを防ぐ
    var session = gs.getSession();
    var flag = 'sync.u_actual_times_detail.running';
    if (session.getProperty(flag) == 'true') {
        return;
    }

    // u_actual_times を参照するフィールド名のリスト
    var refFields = [
        'u_ref_sunday', 'u_ref_monday', 'u_ref_tuesday', 'u_ref_wednesday',
        'u_ref_thursday', 'u_ref_friday', 'u_ref_saturday'
    ];

    var actualTimesSysId = '';

    // 値が設定されている参照フィールドを検索
    for (var i = 0; i < refFields.length; i++) {
        if (current.getValue(refFields[i])) {
            actualTimesSysId = current.getValue(refFields[i]);
            break;
        }
    }

    if (!actualTimesSysId) {
        return; // 有効な参照がない場合は処理を終了
    }

    var actualTimesAssignedTo = '';
    var actualTimesGr = new GlideRecord('u_actual_times');
    if (actualTimesGr.get(actualTimesSysId)) {
        actualTimesAssignedTo = actualTimesGr.getValue('u_assigned_to');

        // 担当者が異なる場合のみ更新処理を実行
        if (current.getValue('u_assigned_to') !== actualTimesAssignedTo) {
            try {
                // 更新操作の前に再帰防止フラグを設定
                session.putProperty(flag, 'true');

                // Afterルールの為、直接currentを更新し、update()を呼び出す必要がある
                var detailGr = new GlideRecord('u_actual_times_detail');
                if (detailGr.get(current.getUniqueValue())) {
                    detailGr.setValue('u_assigned_to', actualTimesAssignedTo);
                    detailGr.update();
                }

            } finally {
                // 処理が完了または失敗した場合でも、必ずフラグを解除
                session.putProperty(flag, null);
            }
        }
    }
})(current, previous);
 
shiz_1-1762159173331.png

 

Ravi Gaurav
Giga Sage

Hi @shiz 

 

Below demo code will help :- 

Use an After Update Business Rule on the u_actuant_times table.
When u_assigned_to changes, update the same field in related records of u_actuant_times_details.

Example:

 

 
(function executeRule(current, previous) { if (current.u_assigned_to.changes()) { var gr = new GlideRecord('u_actuant_times_details'); gr.addQuery('u_ref_Monday', current.sys_id) .addOrCondition('u_ref_Tuesday', current.sys_id) .addOrCondition('u_ref_Wednesday', current.sys_id) .addOrCondition('u_ref_Thursday', current.sys_id) .addOrCondition('u_ref_Friday', current.sys_id) .addOrCondition('u_ref_Saturday', current.sys_id) .addOrCondition('u_ref_Sunday', current.sys_id); gr.query(); while (gr.next()) { gr.u_assigned_to = current.u_assigned_to; gr.update(); } } })(current, previous);

 

This keeps both tables in sync whenever the Assigned To changes in the main table.

Ankur Bawiskar
Tera Patron

@shiz 

how are both the tables linked?

what did you start with and where are you stuck?