Automate 'cmdb_group' creation from Query Builder query (attaching/linking the query in the group.

jakedaw
Tera Contributor

I've got most parts working other than truly, or correctly linking (or embedding) the query in the group. Here is what I've tried so far. 

objective:

1) (this part works fine) A Script Include to iterate through the 'qb_saved_query' table grabbing only the ones that have a -QB at the end of the name. 

2) (This also works fine) Check if a CMDB group already exists from the query in line and skip if it does, or continue to create the group if one does not already exist. 

3) (This works as well) Create the cmdb group from the query (giving the same name as the query (removing the -QB from the group name)

4) (**This is the part that seems to only partially work**) Creating the relationship (attaching) the query to the CMDB group. 

Currently when I attach or link the query it does link, at least in some level, since I see it when looking in the UI.

jakedaw_0-1728049430648.png

 

But when I click on the query (from the 'cmdb_group_contains_qb_query' table it always comes back with no results (no CI's). 

IN my different iterations of the script (javascript) I've tried utilizing the custom table that gets created from a Query Builder Query... to link the members of the group FROM this result table 'u_cmdb_qb_result_####...' Also to no avail. 

When I manually create the group and manually add the Query I see it's attached like above image. 

Also when I right click on the column heading down at the lower section, and select Configure > Table: 

jakedaw_1-1728049913008.png

I see that it's taking me to the 'cmdb_group_contains_qb_query' table. 

And that table does contain the 'sys_id" table reference. 

jakedaw_0-1728050904553.png

 

 

I just need to get over this hurdle of properly linking the Query inside of the CMDB group. 

Any assistance would be marvelous! 

 

By the way here is the script so far today: 

 

 

 

var queryGR = new GlideRecord('qb_saved_query');
queryGR.addQuery('name', 'ENDSWITH', '-QB'); // Filter for queries ending with '-QB'
queryGR.query();

while (queryGR.next()) {
    // Get the sys_id and name of the current query
    var querySysId = queryGR.sys_id;
    var baseName = queryGR.name.toString();

    // Check if CMDB Group already exists
    var cmdbGroup = new GlideRecord('cmdb_group');
    cmdbGroup.addQuery('u_query_builder_query', querySysId); // Check for existing group linked to this query
    cmdbGroup.query();

    if (!cmdbGroup.next()) { // If CMDB Group does not exist, create it
        cmdbGroup.initialize();
        cmdbGroup.group_name = baseName.replace('-QB', ''); // Remove '-QB' from group name
        cmdbGroup.u_query_builder_query = querySysId; // Set the query reference
        var cmdbGroupSysId = cmdbGroup.insert(); // Insert the new CMDB Group
        gs.info('Created CMDB Group: ' + cmdbGroup.group_name + ' with sys_id: ' + cmdbGroupSysId);

        // Link the Query Builder query to the CMDB Group
        var linkGR = new GlideRecord('cmdb_group_contains_qb_query');
        linkGR.initialize();
        linkGR.cmdb_group = cmdbGroupSysId; // Set the CMDB Group sys_id
        linkGR.qb_query = querySysId; // Link the Query Builder query sys_id
        linkGR.insert(); // Insert the record into the linking table
        gs.info('Linked Query: ' + baseName + ' to CMDB Group: ' + cmdbGroup.group_name);
    } else {
        gs.info('CMDB Group already exists for Query: ' + baseName);
    }
}

 

 

 

 

 

0 REPLIES 0