How to get reference field Display value

Sironi
Kilo Sage

Hi All,

I request you , please help me on below issue.

 "cmdb_ci_service_discovered" table record has 'Corporation' field which referencing "u_corporation" table

find_real_file.png

 

Request : Corporation record has "Node" reference field which is referencing same table of record itself  .

Here Hierarchy:  CMDB ServiceDiscovered has "corporation"  field which referencing "Test 2" ----> Test 2 record of Node field Referencing "Test 3", Test 3 record of Node field Referencing "Test 4".......etc..

So now we need to check till which corporation record of Node field  is empty . so that record display need to Display in CMDB ServiceDiscovered.

find_real_file.png

 

i tried below code , but not working

var corSYSID ='';
var bs = new GlideRecord('cmdb_ci_service_discovered');
bs.addQuery('sys_id','04398b559f030200fe2ab0aec32e70fa');
bs.addQuery('u_corporationISNOTEMPTY');
bs.query();
if (bs.next()) 
{
gs.print('Gate 1 :' + bs.u_corporation.getDisplayValue());

var cor = new GlideRecord('u_corporation');
cor.addQuery('u_nodeISNOTEMPTY');
cor.addQuery('sys_id', bs.u_corporation);
cor.query();
if (cor.next()) 
{
gs.print('Gate 2 :' + cor.u_node.getDisplayValue());

corSYSID = cor.u_node;
for (var i = 0; i < repeat; i++) 
{
var cor2 = new GlideRecord('u_corporation');
cor2 .addQuery('u_nodeISNOTEMPTY');
cor2 .addquery('u_node', corSYSID);
cor2 .query();
if(cor2 .next()) 
{
repeat ++;
corSYSID = cor2.u_node;
gs.print('Gate 3 :' + cor2 .u_node.getDisplayValue() + '--' + repeat);
}
 }
  }
}

 

1 ACCEPTED SOLUTION

var corSYSID = '',
    repeat = 0;
var bs = new GlideRecord('cmdb_ci_service_discovered');
bs.addQuery('sys_id', '04398b559f030200fe2ab0aec32e70fa');
bs.addQuery('u_corporationISNOTEMPTY');
bs.query();
if (bs.next()) {
    corSYSID = bs.getValue("u_corporation");
    gs.print('Gate 1 :' + bs.getDisplayValue("u_corporation"));
    var cor = new GlideRecord('u_corporation');
    cor.addQuery('u_nodeISNOTEMPTY');
    cor.addQuery('sys_id', corSYSID);
    cor.query();
    if (cor.next()) {
        gs.print('Gate 2 :' + cor.getDisplayValue("u_node"));
        corSYSID = cor.getValue("u_node");
        while (corSYSID != "") {
            var cor2 = new GlideRecord('u_corporation');
            cor2.addQuery('u_nodeISNOTEMPTY');
            cor2.addQuery('sys_id', corSYSID);
            cor2.query();
            if (cor2.next()) {
                corSYSID = cor2.getValue("u_node");
                gs.print('Gate 3 :' + cor2.getDisplayValue("u_node") + '--' + (repeat++));
                if (repeat >= 20) break; // You may want to remove this line later after your test is successful.
            } else {
                // Corporation with empty node
                break;
            }
        }
    }
}

 

Thanks for clarification!

Please try this. 
In the code "addquery" was used instead of "addQuery", system might be ignoring that part of query and may be always landing at same node.

 

Thanks and regards,

Subrahmanyam Satti

View solution in original post

11 REPLIES 11

Jason123
Tera Contributor

I'd recommend using Flow Designer instead of that script to make the update. This video shows how to setup the Lookup (GlideRecord Query in scripting terms) and the For Loops. 

 

https://www.youtube.com/watch?v=wGQPuod7kP0

Hey Jason,

we are trying to fix old records , not for New records .