Duplicate records created through Business Rule and Script Action

sachinaykara
Tera Contributor

On 'cmdb_ci' table I have a Business Rule that uses a script action to create a record on 'u_cmdb_ci_monitoring' table. Every time a record is created on 'cmdb_ci' through integration based on the conditions given it should either update the existing CI Monitoring record or create a new one. The issue I am currently facing is that for a few CI records its creating 2 CI Monitoring records(duplicate records). The BR, CI and CI monitoring record images are attached. Another thing to note is that this issue was seen in 2024 but after this BR, Event and Script Action were made the issue was resolved. After our recent Xanadu upgrade this issue has come up again. The Script Action is as follows

 

 

 

var fieldValueChanges = event.parm1.toString();
var grRIMCI = current;
var grCIM = new GlideRecord("u_cmdb_ci_monitoring");
grCIM.addQuery("u_ci", grRIMCI.sys_id.toString());
grCIM.query();
if (grCIM.next()) {
    grCIM.u_capid = grRIMCI.u_rim_device_ci_id;
    grCIM.u_ci_impact = grRIMCI.u_rim_ci_impact;
    grCIM.u_ehealth_subscription = grRIMCI.u_rim_ehealth_subscription;
    grCIM.u_profile_end = grRIMCI.u_rim_profile_end;
    grCIM.u_profile_start = grRIMCI.u_rim_profile_start;
    grCIM.u_servicemodel_id = grRIMCI.u_rim_servicemodel_id;
    var configItemSerName = grRIMCI.u_rim_service_name;
    if (configItemSerName != "") {
        grCIM.u_service_name = configItemSerName;
        grCIM.u_monitoring_system = getMonSys(grRIMCI.company, configItemSerName);
    }
    grCIM.u_snmp_profile = grRIMCI.u_rim_snmp_profile.getDisplayValue() == "" ? "" : getRIMPRof(grRIMCI.u_rim_snmp_profile.getDisplayValue());
    grCIM.u_profile = grRIMCI.u_rim_profile.getDisplayValue() == "" ? "" : getRIMPRof(grRIMCI.u_rim_profile.getDisplayValue());

    if (fieldValueChanges == "false") {
        grCIM.u_provisioning_state = grRIMCI.u_rim_profile.getDisplayValue() == "" ? "3" : getState(grRIMCI);
    }
    grCIM.u_model_type = grRIMCI.u_rim_spectrum_model_type;
    grCIM.u_schedule_name = grRIMCI.u_rim_schedule_name;
    grCIM.u_ip_address = grRIMCI.ip_address;

    grCIM.update();
} else {

    // create ci monitoring records only if RIM fields value is not empty.

    if (!(grRIMCI.u_rim_profile.nil() && grRIMCI.u_rim_service_name.nil() && grRIMCI.u_rim_profile_start.nil() && grRIMCI.u_rim_profile_end.nil() && grRIMCI.u_rim_snmp_profile.nil() && grRIMCI.u_rim_ehealth_subscription.nil() && grRIMCI.u_rim_ci_impact.nil() && grRIMCI.u_rim_device_ci_id.nil() && grRIMCI.u_rim_servicemodel_id.nil())) {
        var grCIM1 = new GlideRecord("u_cmdb_ci_monitoring");
        grCIM1.initialize();
        grCIM1.u_capid = grRIMCI.u_rim_device_ci_id;
        grCIM1.u_ci = grRIMCI.sys_id;
        grCIM1.u_ci_impact = grRIMCI.u_rim_ci_impact;
        grCIM1.u_ehealth_subscription = grRIMCI.u_rim_ehealth_subscription;
        grCIM1.u_profile_end = grRIMCI.u_rim_profile_end;
        grCIM1.u_profile_start = grRIMCI.u_rim_profile_start;
        grCIM1.u_servicemodel_id = grRIMCI.u_rim_servicemodel_id;
        grCIM1.u_service_name = grRIMCI.u_rim_service_name;
        grCIM1.u_snmp_profile = grRIMCI.u_rim_snmp_profile.getDisplayValue() == "" ? "" : getRIMPRof(grRIMCI.u_rim_snmp_profile.getDisplayValue());
        grCIM1.u_profile = grRIMCI.u_rim_profile.getDisplayValue() == "" ? "" : getRIMPRof(grRIMCI.u_rim_profile.getDisplayValue());
        grCIM1.u_monitoring_system = grRIMCI.u_rim_service_name == "" ? "" : getMonSys(grRIMCI.company, grRIMCI.u_rim_service_name);
        if (fieldValueChanges == "false") {
            grCIM1.u_provisioning_state = getState(grRIMCI);
        }
        grCIM1.u_model_type = grRIMCI.u_rim_spectrum_model_type;
        grCIM1.u_schedule_name = grRIMCI.u_rim_schedule_name;
        grCIM1.u_ip_address = grRIMCI.ip_address;
        grCIM1.insert();
    }
}

function getRIMPRof(ID) {
    var getMonProf = new GlideRecord("u_cmdb_ci_monitoring_profile");
    getMonProf.addQuery("u_name", ID);
    getMonProf.query();
    if (getMonProf.next()) {
        return getMonProf.sys_id;
    }
}

function getMonSys(comp, servicename) {
    var grMonSet = new GlideRecord("u_gen_company_monitor_setting");
    grMonSet.addEncodedQuery("u_company_setting.u_company=" + comp + "^u_service_name=" + servicename + "^u_monitoring_system=1");
    grMonSet.query();
    if (grMonSet.next()) {
        return grMonSet.sys_id;
    } else {
        return "";
    }
}

function getGCS(comp) {
    var gcsSysid = new GlideRecord("u_gen_company_setting");
    gcsSysid.addEncodedQuery("u_company=" + comp);
    gcsSysid.query();
    if (gcsSysid.next()) {
        return gcsSysid.sys_id;
    } else {
        return "";
    }
}

function getState(grCIM) {
    var ciClass = grCIM.sys_class_name;
    var RIM_Start = new GlideDateTime(grCIM.u_rim_profile_start);
    var RIM_end = new GlideDateTime(grCIM.u_rim_profile_end);
    var currentDate = new GlideDateTime();
    if (ciClass != "cmdb_ci_port") {
        if (
            grCIM.install_status == "5" &&
            grCIM.u_rim_profile_start.getDisplayValue() != "" &&
            RIM_Start < currentDate &&
            (grCIM.u_rim_profile_end.getDisplayValue() == "" || RIM_end > gs.endOfToday()) &&
            grCIM.u_rim_profile.u_name.includes("A0")
        ) {
            return "1";
        } else if (grCIM.install_status != "5") {
            return "3";
        } else if (grCIM.u_rim_profile_end.getDisplayValue() != "" && RIM_end < gs.endOfToday()) {
            return "3";
        } else if (grCIM.u_rim_profile.u_name.includes("A0") == false) {
            return "3";
        }
    } else {
        if (
            grCIM.install_status == "5" &&
            grCIM.u_rim_profile_start.getDisplayValue() != "" &&
            RIM_Start < currentDate &&
            (grCIM.u_rim_profile_end.getDisplayValue() == "" || RIM_end > gs.endOfToday()) &&
            (grCIM.u_rim_profile.u_name.includes("A0") || grCIM.u_rim_profile.u_name.includes("B0"))
        ) {
            return "1";
        } else if (
            grCIM.u_rim_profile.u_name.includes("A0") == false ||
            grCIM.u_rim_profile.u_name.includes("B0") == false
        ) {
            return "3";
        } else if (grCIM.install_status != "5" || grCIM.u_rim_profile_end.getDisplayValue() != "" || RIM_end < gs.endOfToday()) {
            return "3";
        }
    }
}

 

 

4 REPLIES 4

raj chavan
Tera Guru

Hi @sachinaykara 

the duplicate CI Monitoring records are most likely caused by the Business Rule (or its underlying event/script action) firing more than once for the same CI record—especially since the issue reappeared after the Xanadu upgrade, which may have altered event processing or integration behavior.
   - Verify that the Business Rule is configured to run only once (for example, as an "After Insert" rule) and not on updates unless necessary.
   - Check the integration process to ensure it isn’t sending duplicate records or events for the same CI record.
   - Your script correctly queries the u_cmdb_ci_monitoring table and updates the record if it exists. However, if the BR fires twice nearly simultaneously, both instances might not "see" the other’s inserted record.
   - Flag Field Consider adding a flag field on the cmdb_ci record. When processing the record the first time, set this flag. Subsequent executions of the BR can check this flag and skip the duplicate processing.
If the integration might send duplicate requests, add logic to ensure that only one monitoring record can exist per CI. 
- Since the issue reappeared after an upgrade, review the changes in the event processing and Business Rule execution order. Sometimes upgrades affect the timing, causing near-simultaneous execution that leads to race conditions.
 
 
Kindly mark it correct and helpful if it is applicable.

Thanks,

Raj

Thanks for the insights. Another thought that came to my mind is if the issue is due to the script why is it creating duplicate records for only a few CIs. When I checked other CIs that are created during the same time, everything seems to be created/updated exactly like the one's that created the duplicates.

Ankur Bawiskar
Tera Patron
Tera Patron

@sachinaykara

so what debugging have you done from your side

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@sachinaykara 

It could be a possibility that by the time the CI is created next integration is coming and it's not finding the record and creating duplicates.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader