Bidirectional System Tag Sync Between Incident and Custom Table - Sync Failing After Multiple Update

harshalpatil
Tera Contributor

Hi expert, need your help.

I'm implementing a bidirectional sync for a "System Tag" field between the Incident table and a custom Basket table. The sync works initially but fails when the same incident is updated multiple times. I need help identifying why the synchronisation stops working after the first update.

Business Requirement

  • System Tag field exists on both Incident and Basket tables (same field type, same choice values)
  • Serial Number field on Incident is a reference field pointing to the Basket table
  • Bidirectional sync needed:
    • When an agent updates System Tag on an Incident → sync to the linked Basket record
    • When System Tag is updated on a Basket record → sync to all linked active Incidents
  • Sync should work continuously until the Incident is closed/cancelled

    refer this script 

     

    Business Rule 1: Incident → Basket Sync

    • Table: Incident
    • When: After Insert, After Update
    • Filter Conditions: Serial Number changes OR System Tag changes
    • Script:(current, previous) { var LOG_SOURCE = 'System Tag Sync (Inc -> Contract)'; try { // Exit if incident is inactive or closed/cancelled if (!current.active || current.state == 7 || current.state == 8) { return; } var systemTag = current.getValue('u_system_tag'); // Exit if no system tag or no serial number reference if (!systemTag || current.u_serial_number.nil()) { return; } // Get referenced contract item var contractItem = current.u_serial_number.getRefRecord(); if (!contractItem || !contractItem.isValidRecord()) { gs.info('System Tag Sync: Contract Item not found for Incident ' + current.getValue('number'), LOG_SOURCE); return; } gs.info('System Tag Sync: Incident = ' + current.getValue('number') + ', Contract Item Sys ID = ' + contractItem.getUniqueValue() + ', System Tag = ' + systemTag, LOG_SOURCE); contractItem.setValue('u_systemtag', systemTag); contractItem.setWorkflow(false); contractItem.update(); gs.info('System Tag Sync: Successfully updated Contract Item ' + contractItem.getUniqueValue() + ' with System Tag ' + systemTag, LOG_SOURCE); } catch (e) { gs.info('System Tag Sync ERROR for Incident ' + current.getValue('number') + ': ' + e.message, LOG_SOURCE); } })(current, previous);

     

     

    Business Rule 2: Basket → Incident Sync

    • Table: Basket (custom table)
    • When: After Update
    • Filter Conditions: System Tag changes
    • Script:
    (function executeRule(current, previous) {
        var contractSysid = current.getUniqueValue();
        var systemTag = current.getValue('u_systemtag');
        gs.log("Running BR - Sync System tag Contract to Incident: " + contractSysid, "SMOD");
    
        try {
            // Exit if no system tag
            if (!systemTag) {
                return;
            }
    
            // Find open incidents linked to this contract item
            var inc = new GlideRecord('incident');
            inc.addQuery('u_serial_number', contractSysid);
            inc.addActiveQuery();
            inc.addQuery('u_system_tag', '!=', systemTag);
            inc.query();
    
            var count = 0;
            while (inc.next()) {
                inc.setValue('u_system_tag', systemTag);
                inc.u_system_tag_sync_source = "System Tag is updated from Contract item";
                inc.setWorkflow(false);
                inc.update();
                count++;
            }
    
            gs.info('System Tag Sync: Contract Item ' + contractSysid + 
                    ' -> updated ' + count + ' incident(s) with System Tag ' + systemTag, "SMOD");
    
        } catch (e) {
            gs.info('System Tag Sync ERROR for Contract Item ' + contractSysid + 
                    ': ' + e.message, "SMOD");
        }
    })(current, previous)





    Scenario:

    1. Incident 1 is created and System Tag is set to "Chiku" → Basket record updates to "Chiku" ✅
    2. Incident 2 is created (linked to same Basket record) and System Tag is set to "Mango" → Basket record updates to "Mango" ✅
    3. Incident 1 System Tag is changed again (e.g., to "Banana") → Basket record updates ✅
    4. Incident 1 System Tag is changed again (e.g., to "Apple") → Basket record does NOT update ❌

    Observed Behaviour:

    • The sync works for the first update on each incident
    • Subsequent updates on the same incident fail to sync to the Basket table
    • The Business Rule appears to execute (based on logs), but the Basket record value doesn't change
    • Only one incident seems to "own" the sync after a certain point



      #ServiceNow #Business Rule 
2 REPLIES 2

yashkamde
Giga Sage

Hello @harshalpatil ,

 

Might be BR 1 updates the Basket with setWorkflow(false), which stops BR 2 from firing. So the other linked incidents are never synced, and the Basket and incidents drift apart.

 

Remove setWorkflow(false) in BR 1, and add a check so each rule only updates when the value actually differs (and a flag so BR 1 ignores updates made by BR 2). That prevents the loop and keeps all three records in sync.

 

If my response helped mark as helpful and accept the solution.

@yashkamde Could you please share where do i need to change exact.