looking for a script for pulling data from cmdb_rel_ci for a service and its CI.

Servicenow lear
Tera Contributor

Hi All,

I am still learning JS and new to servicenow.

I have a requirement where in we want to pull data from cmdb_rel_ci table.

: parent should be a SERVICE and type should be Depends on :: Used By.

what query should i use to have it in my code ?

_getAllAssociations: function() {
        var gr = new GlideRecord("cmdb_rel_ci");

     gr.addQuery("type", ?);

gr.addQuery("parent", ?)
        gr.query();
        return gr;

Kindly refer the screenshot for reference wherein Securoty lending is a service and Xbox is a CI using it, like that I want all services and its CI data pulled in.

find_real_file.png
        

 

1 ACCEPTED SOLUTION

Julio Valdez JR
Giga Contributor

Hi, please try code below to get the data that you are looking for:

 

var gr = new GlideRecord('cmdb_rel_ci');
gr.addEncodedQuery('type=1a9cb166f1571100a92eb60da2bce5c5^parent.sys_class_name=cmdb_ci_service');
gr.query();
while (gr.next()) {
    // do something
}

View solution in original post

6 REPLIES 6

Julio Valdez JR
Giga Contributor

Hi, please try code below to get the data that you are looking for:

 

var gr = new GlideRecord('cmdb_rel_ci');
gr.addEncodedQuery('type=1a9cb166f1571100a92eb60da2bce5c5^parent.sys_class_name=cmdb_ci_service');
gr.query();
while (gr.next()) {
    // do something
}

Thank you this works,

just a quick query on it , since I am trying to glide record 2 tables in a function how can i do that and i think that should fix my issue.

Please, refer the code below :

copyServiceMappingEntries: function() {
        // Get existing associations
        var grServices = this._getAllAssociations();
        // Delete the current copies
        this._deleteExistingCopies();
        // Add the new copies
        this._addAllAssociations(grServices);
    },
    
    _getAllAssociations: function() {
        var gr = new GlideRecord('cmdb_rel_ci');
        gr.addEncodedQuery('type=1a9cb166f1571100a92eb60da2bce5c5^parent.sys_class_name=cmdb_ci_service');
        gr.query();
        return gr;
        
        var gr = new GlideRecord("svc_ci_assoc");
        gr.query();
        return gr;
        
  },

I am not sure what your goal is for this code. In general, You can't return twice in a function and you can't reliable return 2 different GlideRecord queries in the same function either. Your best bet would be to create 2 separate functions that query and return what you need. 

I deal goal is to pull data from two tables, cmdb and assoc table.