Information on _queryMatch

garyminnick
Tera Contributor

I am trying to understand exactly how _queryMatch works in the Lookup Rules.  There is very little information other than the start of the default scripts.

// _queryMatch function checks if the query returns 0, 1 or more than 1 CI.
// it returns:
//    null: if no CI found
//    ci record: if a unique CI was found
//    the first CI record found: if more than 1 CI was found and log a duplicate error message

If more than one CI is found it says it returns the first one.  But there is no indication if it filters out CI's that are retired or non-operational, or try to get the one with the latest discovery date.  And the default scripts do not take those into account in the GlideRecord queries.

 

I have in my notes that the Lookup Rules will return the CI with the latest created date, but I cannot recall if I saw that in some ServiceNow docs or one of their videos.  So, I am not sure if the _queryMatch does anything with that.  Any help will be appreciated.  I will most likely just add those filters to the GlideRecord query so I can know what I am getting.  But, if I do that, what is the purpose of _queryMatch?

3 REPLIES 3

Chavan AP
Tera Guru

Core Behavior:

The _queryMatch function returns "the first CI record found" when multiple CIs match - it does NOT apply any intelligent filtering for operational status, discovery dates, or creation dates by default.

 

// Simple CI Lookup Rule Example - Hostname matching with proper filtering

(function process(rule, sourceValue, sourcePayload) {
    var sourceField = {};
    sourceField[rule.source_field] = sourceValue;
    
    // Query for CI by hostname
    var cmdbci = new GlideRecord("cmdb_ci_server");
    cmdbci.addQuery("host_name", sourceValue);
    
    // ADD YOUR FILTERS (what _queryMatch doesn't do)
    cmdbci.addQuery('operational_status', '1');        // Operational only
    cmdbci.addQuery('install_status', '!=', '7');      // Not retired
    cmdbci.addQuery('duplicate_of', '');               // Not a duplicate
    
    // Order by latest discovery date
    cmdbci.orderByDesc('last_discovered');
    
    cmdbci.query();
    
    // Use _queryMatch for logging and duplicate handling
    cmdbci = _queryMatch(cmdbci, rule, sourceField);
    
    if (cmdbci) {
        return cmdbci.getUniqueValue();
    }
    
    return null;
    
})(rule, sourceValue, sourcePayload);
Chavan AP
[ Architect | Certified Professional]

Was this response helpful? If so, please mark it as Helpful and Accept as Solution to help others find answers.

garyminnick
Tera Contributor

Thanks for the information.  I typically do what you posted above just to be sure.  However, it just bugged me not knowing exactly what it was meant for.  Thanks for clarifying.

Chavan AP
Tera Guru

Glad I could help! If this solved your issue, please mark it as Helpful and Accept as Solution so others can benefit too.

 

Chavan A.P. Technical Architect | Certified Professional

Chavan AP
[ Architect | Certified Professional]

Was this response helpful? If so, please mark it as Helpful and Accept as Solution to help others find answers.