How to use the sys_id in encoded query to fetch records in the realtionship script for the reltd lis

prasanna_da
Tera Expert

I am facing issue getting records for the contract table , by using the current.addEncodedQuery('sys_idIN+contract);

Table 1 : Relation between ci and contract,

Table 2 : has contract records

Condition : All contract with ci_type = BusinessApplication 

Table 3 : Has the related list which will fetch Table 2 records based on the condition 
-----------
In the script ,

1. i have queried table 1 and collected sysid which satisfy condition in array contract[];//contract.push(gr.sys_id.toString())

 

2. I am using current.adEncodedQuery('sys_idIN'+contract);

but this dont work for multiple sysId , if there is only single sysId in the contract it works otherwise no records are shown.

Thanks in Advance.

1 ACCEPTED SOLUTION

Hello @prasanna_da 

  • Seems like there is a issue with setting the query. 
  • Just a minor change in script in building the query

Please refer the script

var contract = [];

    var gr = new GlideRecord('ast_contract_instance');
    gr.addQuery('ci_type', 'Business Application');
    gr.query();

    while (gr.next()) {
        contract.push(gr.sys_id.toString());
    }

    if (contract.length > 0) {
        // Construct the sys_idIN query with the array of sys_ids
        var queryC = 'sys_idIN' + contract.join(',');
        gs.info('Constructed query: ' + queryC);
        current.addEncodedQuery(queryC);
    } else {
        gs.info('No contracts found with ci_type "Business Application". Adding an empty query.');
        current.addEncodedQuery('sys_idIN'); // Optional, or handle as needed
    }

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

I'm not following exactly - are tables 1, 2, and/or 3 out of box or custom?  'the script' is a custom relationship script?  It would help to see the entire script, and screenshots of the tables/records/related list.

Table 2 : ast_contract
Table 1 : ast_contract_instance
Table 3 : cmdb_ci_business_app

Requirement : On table 3 to have related list To have all contracts from table 2, such that the configuration type is Business Application [this is mentioned in table 1]

To get the related list i have created a relationship 

prasanna_da_0-1733419032220.png

 

Hello @prasanna_da 

  • Seems like there is a issue with setting the query. 
  • Just a minor change in script in building the query

Please refer the script

var contract = [];

    var gr = new GlideRecord('ast_contract_instance');
    gr.addQuery('ci_type', 'Business Application');
    gr.query();

    while (gr.next()) {
        contract.push(gr.sys_id.toString());
    }

    if (contract.length > 0) {
        // Construct the sys_idIN query with the array of sys_ids
        var queryC = 'sys_idIN' + contract.join(',');
        gs.info('Constructed query: ' + queryC);
        current.addEncodedQuery(queryC);
    } else {
        gs.info('No contracts found with ci_type "Business Application". Adding an empty query.');
        current.addEncodedQuery('sys_idIN'); // Optional, or handle as needed
    }

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar