Called script include using reference qualifier

GBS
Tera Contributor

called below script in a reference qualfier while calling passed a parameter but the value not getting pass script include function

 

Reference qualifier: javascript:newCIvalueretrieve().getCis(current.variables.name_of_application_service_you_want_to_map):

 

Script Include: var Civalueretrieve = Class.create();
initialize: function() {},

getCis: function(parentId) {

var parentIdValue "parentId;
gs.log("parentId sai testing" parentIdValue);

var relations = new GlideRecord('cmdb_rel_ci');

relations.addQuery("parent", parentIdValue);

relations.query();

var ciData [];

while (relations.next()) {

var cis new GlideRecord(cmdb_ci");
cis.addQuery("sys_id', relations.child.sys_id);

cis.query();

if (cis.next()) {

ciData.push(cis-getuniqueValue());
}
}
var strQuery new ArrayUtil().unique(ciData);

return "sys_idIN” + strQuery;
},

type: 'CIvalueretrieve
};

 

1 ACCEPTED SOLUTION

@GBS 

then in that list collector variable you should give variable attributes so that the ref qualifier works

ref_qual_elements=name_of_application_service_you_want_to_map

add that in variable attributes

AnkurBawiskar_0-1736770047027.png

 

Also don't use gs.log(). use gs.info()

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

View solution in original post

14 REPLIES 14

Juhi Poddar
Kilo Patron

Hello @GBS 

There has to be a space in between new and script include name.

Reference qualifier

javascript:new CIvalueretrieve().getCis(current.variables.name_of_application_service_you_want_to_map)

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

GBS
Tera Contributor

@Juhi Poddar thanks for the response. I have provided the space even though it is not working.

@GBS 

There are few syntax errors in script:

  1. Missing = in assignments (e.g., var parentIdValue "parentId).
  2. Missing " or ' around strings (e.g., cis new GlideRecord(cmdb_ci");).
  3. Missing = in assignments (e.g., var strQuery new ArrayUtil().unique(ciData);)
  4. Incorrect use of quotes in addQuery (e.g., "sys_id', relations.child.sys_id).
  5. Cannot pass array directly as an output, instead join them to convert it to string.

Here is the updated script include:

 

var CIvalueretrieve = Class.create();
CIvalueretrieve.prototype = {
    initialize: function () {},

    getCis: function (parentId) {
        if (!parentId) {
            gs.log("No parentId provided to getCis function");
            return '';
        }

        gs.log("Parent ID passed: " + parentId);

        // Query the cmdb_rel_ci table for relationships where the given ID is the parent
        var relations = new GlideRecord('cmdb_rel_ci');
        relations.addQuery("parent", parentId);
        relations.query();

        var ciData = []; // Array to store child CI sys_ids

        while (relations.next()) {
            // Query the cmdb_ci table for child CIs
            var cis = new GlideRecord("cmdb_ci");
            cis.addQuery("sys_id", relations.child.sys_id);
            cis.query();

            if (cis.next()) {
                ciData.push(cis.getUniqueValue());
            }
        }

        // Remove duplicate sys_ids
        var uniqueCis = new ArrayUtil().unique(ciData);

        // Construct the sys_idIN query
        if (uniqueCis.length > 0) {
            return "sys_idIN" + uniqueCis.join(",");
        }

        // Return an empty result if no CIs found
        return '';
    },

    type: 'CIvalueretrieve'
};

 

This corrected script should resolve the issues and work as expected. Let me know if you encounter further problems!

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Ankur Bawiskar
Tera Patron
Tera Patron

@GBS 

Please share on what variable type the ref qualifier is applied

Also share what's the variable type for "name_of_application_service_you_want_to_map"?

try this

javascript: new CIvalueretrieve().getCis(current.variables.name_of_application_service_you_want_to_map);

I assume both script include and field are in same scope

var CIvalueretrieve = Class.create();
CIvalueretrieve.prototype = {
    initialize: function() {},

    getCis: function(parentId) {
        var parentIdValue = parentId;
        gs.log("parentId sai testing: " + parentIdValue);

        var relations = new GlideRecord('cmdb_rel_ci');
        relations.addQuery("parent", parentIdValue);
        relations.query();
        var ciData = [];
        while (relations.next()) {
            ciData.push(relations.getValue('child'));
        }

        var strQuery = new ArrayUtil().unique(ciData);
        return "sys_idIN" + strQuery.join(",");
    },

    type: 'CIvalueretrieve'
};

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