Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to pass an Array to the Encoded query ?

Community Alums
Not applicable

Hi ,

I have an Array-relatedArray[i] in which I have stored the Sys_id of the records , I am trying to query the Relationship table in which parent.sys_id  is one of the Array list OR child.sys_id is one of the Array List 

Please review my code and help me with the corrections

var FinalRelatedArray = new Array();
    var len = relatedArray[i].length;
    var query_String1 = "parent.sys_idIN=" + relatedArray[i] + "^ORchild.nameIN=" +relatedArray[i];
    relatedBu_Apps.addEncodedQuery(query_String1);
    relatedBu_Apps.query();
    
    while(relatedBu_Apps.next())
        {
            for(var j=0; j<len; j++)
                {
                    if(relatedBu_Apps.child==relatedArray[j])
                        {
    if (relatedBu_Apps.parent.sys_class_name == 'cmdb_ci_business_app'&& relatedBu_Apps.parent.install_status != 270 && relatedBu_Apps.parent.install_status != 230 && relatedBu_Apps.parent.install_status != 80 && relatedBu_Apps.parent.install_status != 240)                  {
                FinalRelatedArray[z] = relatedBu_Apps.parent.sys_id;
                z++;    
                    }
                            gs.log('final Impacted CIs are:','FinalRelatedArray[z]');
                    }
    else if(relatedBu_Apps.parent==relatedArray[j])
                                {
    if ( relatedBu_Apps.child.sys_class_name == 'cmdb_ci_service_discovered' || relatedBu_Apps.child.sys_class_name == 'cmdb_ci_service_auto' && relatedBu_Apps.child.install_status != 270 && relatedBu_Apps.child.install_status != 230 && relatedBu_Apps.child.install_status != 80 && relatedBu_Apps.child.install_status != 240) {
                FinalRelatedArray[z] = relatedBu_Apps.child.sys_id;
                z++;    
                                }
    gs.log('final Impacted CIs are:','FinalRelatedArray[z]');
                }
        }
        }

3 REPLIES 3

Maik Skoddow
Tera Patron
Tera Patron

Hi

in case in your array relatedArray are really only Sys IDs stored you have to write 

var strSysIDs = relatedArray.join(',');
var query_String1 = "parent.sys_idIN=" + strSysIDs + "^ORchild.nameIN=" +strSysIDs;

Maik

Community Alums
Not applicable

Hi Maik

I have implemented the same but code is not executing , could you please kindly review my code and suggest me the changes.

var relatedBu_Apps = new GlideRecord('cmdb_rel_ci');
    var FinalRelatedArray = new Array();
    var len = relatedArray[i].length;
     var strSysIDs = relatedArray.join(',');
     var query_String1 = "parent.sys_idIN=" + strSysIDs + "^ORchild.nameIN=" +strSysIDs;
    
    relatedBu_Apps.addEncodedQuery(query_String1);
    
    relatedBu_Apps.query();
    
    while(relatedBu_Apps.next())
        {
            for(var j=0; j<len; j++)
                {
                    if(relatedBu_Apps.child==relatedArray[j])
                        {
    if (relatedBu_Apps.parent.sys_class_name == 'cmdb_ci_business_app'&& relatedBu_Apps.parent.install_status != 270 && relatedBu_Apps.parent.install_status != 230 && relatedBu_Apps.parent.install_status != 80 && relatedBu_Apps.parent.install_status != 240)                  {
                FinalRelatedArray[z] = relatedBu_Apps.parent.sys_id;
                z++;    
                    }
                            gs.log('final Impacted CIs are:','FinalRelatedArray[z]');
                    }
    else if(relatedBu_Apps.parent==relatedArray[j])
                                {
    if (relatedBu_Apps.child.sys_class_name == 'cmdb_ci_business_app' || relatedBu_Apps.child.sys_class_name == 'cmdb_ci_service_discovered' || relatedBu_Apps.child.sys_class_name == 'cmdb_ci_service_auto' && relatedBu_Apps.child.install_status != 270 && relatedBu_Apps.child.install_status != 230 && relatedBu_Apps.child.install_status != 80 && relatedBu_Apps.child.install_status != 240) {
                FinalRelatedArray[z] = relatedBu_Apps.child.sys_id;
                z++;    
                                }
    gs.log('final Impacted CIs are:','FinalRelatedArray[z]');
                }
        }
        }
    var total_row = i;
    var addAppList = new GlideRecord('u_m2m_change_rel_cmdb_ci');
    //addAppList.initialize();
    for (i = 0; i < total_row; i++) {
        //Check if the CI already exist
        var FinalArray = int_arrayUtil.unique(FinalRelatedArray[z]);
        var checkAppList = new GlideRecord('u_m2m_change_rel_cmdb_ci');
        checkAppList.addQuery('u_cmdb_ci', FinalArray);
        checkAppList.addQuery('u_change_request', current.task);
        checkAppList.query();


        //Check if the Affected CI already exist
        var affAppList = new GlideRecord('task_ci');
        affAppList.addQuery('ci_item', FinalArray);
        affAppList.addQuery('task', current.task);
        affAppList.query();

        if (!checkAppList.hasNext() && !affAppList.hasNext()) {
            addAppList.initialize();
            addAppList.u_cmdb_ci = FinalArray;
            addAppList.u_change_request = current.task.sys_id;
            addAppList.insert();

        }
    }
    if (found_test) {
        //   gs.addInfoMessage("Adding related applications to Impacted CI's");
    }

}

Community Alums
Not applicable

Hi Maik,

Could you please review my code and help me in finding the errors

Thanks,

Prakash.