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

this is what i am doing:

 

_getAllAssociations: function() {
        
        var gr = new GlideRecord("svc_ci_assoc");
        gr.query();
        if(gr.next())
            {
                return gr;
                
                
            }
        else
            {
                var grRel = new GlideRecord('cmdb_rel_ci');
        grRel.addEncodedQuery('type=1a9cb166f1571100a92eb60da2bce5c5^parent.sys_class_name=cmdb_ci_service');
        grRel.query();
        return grRel;
                
            }
        
            
  },

Aman Kumar S
Kilo Patron

Hey @Servicenow learner 

You can try below code:

var ciList = [];
var relatedCIs = new GlideRecord('cmdb_rel_ci');
relatedCIs.addEncodedQuery('type=cb5592603751200032ff8c00dfbe5d17^parent.nameSTARTSWITHSecurity lending^parent.sys_class_name=cmdb_ci_service^child=46a438a8a9fe19810011dc213ad798a5');// change child's sys_id with xbox one
relatedCIs.query();
while (relatedCIs.next()) {
    ciList.push(relatedCIs.getUniqueValue());
}
return ciList.toString();
Best Regards
Aman Kumar