Business Rule - Trying to populate cmdb_ci with ip address from cmbd_ci_ip_address

Joshua Wolfe
Tera Expert

Hello,

 

I believe I am close, but not sure where I am going wrong here. From SG-SCCM, we retrieve the IP Address and it is put on the string IP address field on the cmdb_ci_ip_address. My goal is to get the IP address from the ip_address field on the cmdb_ci_ip_address table and use a business rule to populate to most recent IP address value in an ip_address string field on the cmdb_ci table for our computer class.

 

I am trying to use this advanced script in my business rule (running on cmdb_ci_ip_address table):

(function executeRule(current, previous /*null when async*/) {

 

// Add your code here
// Insert a IP address from cmdb_ci_ip_address to configuration item
var IPgr = new GlideRecord('cmdb_ci_ip_address'); // look in the IP address table
IPgr.addQuery('cmdb_ci',current.cmdb_ci); //look for items with the same CMDB_CI to see if there's another IP on the same CI
IPgr.orderByDesc('sys_created_on'); //Added sort so that the newest ones are at the top
IPgr.setLimit(1); //Set limit to 1 which should bring back the most recent log that matches the encoded query. We only want to see the most recent.
IPgr.query();
if (IPgr.next()){
// if we find an ip address, we need to update the CI for that ip.
var CIgr = new GlideRecord("cmdb_ci");
CIgr.addQuery("sys_id", current.cmdb_ci);
CIgr.query();
if (CIgr.next()) {
CIgr.ip_address != IPgr.ip_address;
CIgr.update();

}

 


}

 

})(current, previous);

 

I'm not sure what I am getting wrong or missing so any help would be appreciated!

1 ACCEPTED SOLUTION

Hey! 

 

Thank you all for the help! My coworker and I seemed to have found the answer.

 

We initially did a OnDemand Scheduled job (variation of the business rule below) to pull in the IP addresses:

var nicgr = new GlideRecord('cmdb_ci_network_adapter');
nicgr.addQuery("discovery_source","SG-SCCM");
// nicgr.orderByDesc('sys_created_on');
//nicgr.setLimit(1);
nicgr.query();
while(nicgr.next()){

// Add your code here
// Insert a IP address from cmdb_ci_ip_address to configuration item
var IPgr = new GlideRecord('cmdb_ci_ip_address'); // look in the IP address table
IPgr.addQuery('nic.cmdb_ci',nicgr.cmdb_ci); //look for items with the same CMDB_CI to see if there's another IP on the same CI \\'where to look', what to look for (assumed to be is equals to unless told otherwise)
IPgr.orderByDesc('sys_created_on'); //Added sort so that the newest ones are at the top
IPgr.setLimit(1); //Set limit to 1 which should bring back the most recent log that matches the encoded query. We only want to see the most recent.
IPgr.query();
while(IPgr.next()){
// if we find an ip address, we need to update the CI for that ip.
var CIgr = new GlideRecord("cmdb_ci");
CIgr.addQuery("sys_id", nicgr.cmdb_ci);
CIgr.query();
while (CIgr.next()) {
CIgr.ip_address = IPgr.ip_address;
CIgr.update();
gs.log('Nic is: ' + nicgr.getDisplayValue() + "\nAnd IP is: " + IPgr.ip_address, "Josh4");

}

//current.update(); //never use current.update on a update br!
}

}

 

 

Then we used this business rule to continually update them going forward:

 

(function executeRule(current, previous /*null when async*/) {

// Add your code here
// Insert a IP address from cmdb_ci_ip_address to configuration item
var IPgr = new GlideRecord('cmdb_ci_ip_address'); // look in the IP address table
IPgr.addQuery('nic.cmdb_ci',current.nic.cmdb_ci); //look for items with the same CMDB_CI to see if there's another IP on the same CI
IPgr.orderByDesc('sys_created_on'); //Added sort so that the newest ones are at the top
IPgr.setLimit(1); //Set limit to 1 which should bring back the most recent log that matches the encoded query. We only want to see the most recent.
IPgr.query();
if (IPgr.next()){
// if we find an ip address, we need to update the CI for that ip.
var CIgr = new GlideRecord("cmdb_ci");
CIgr.addQuery("sys_id", nic.cmdb_ci);
CIgr.query();
if (CIgr.next()) {
CIgr.ip_address = IPgr.ip_address;
CIgr.update();

}

//current.update(); //never use current.update on a update br!
}

})(current, previous);

View solution in original post

7 REPLIES 7

Akif_Shah
Kilo Sage
Kilo Sage

I would double check the usage of [current.cmdb_ci] in your script. OOB on the "cmdb_ci_ip_address" there is no field named cmdb_ci so current.cmdb_ci may always return incorrect results. 

I tried changing the script to query the cmdb_ci_computer.table since the newly created ip_address field is on that table and it still didn't work.

 

(function executeRule(current, previous /*null when async*/) {

 

// Add your code here
// Insert a IP address from cmdb_ci_ip_address to configuration item
var IPgr = new GlideRecord('cmdb_ci_ip_address'); // look in the IP address table
IPgr.addQuery('sys_id',current.ip_address); //look for items with the same CMDB_CI to see if there's another IP on the same CI
IPgr.orderByDesc('sys_created_on'); //Added sort so that the newest ones are at the top
IPgr.setLimit(1); //Set limit to 1 which should bring back the most recent log that matches the encoded query. We only want to see the most recent.
IPgr.query();
if (IPgr.next()){
// if we find an ip address, we need to update the CI for that ip.
var CIgr = new GlideRecord("cmdb_ci_computer");
CIgr.addQuery("sys_id", current.ip_address);
CIgr.query();
if (CIgr.next()) {
CIgr.ip_address != IPgr.ip_address;
CIgr.update();

}

 

//current.update(); 
}

 

})(current, previous);

Akif_Shah
Kilo Sage
Kilo Sage

should this line CIgr.ip_address != IPgr.ip_address; be changed to

"CIgr.ip_address = IPgr.ip_address;"

Currently CIgr.update() is not set to update anything


Unfortunately, it didn't. I changed it and ran the SG-SCCM network data source to see and it still didn't put the IP address value on the cmdb_ci.

 

I looked further and the table appears to be the cmdb_ci_computer table. So I updated it with your most recent advice (thank you so much!) and it still doesn't seem to be doing what I need. Granted, I am really new at scripting so I am likely missing something I don't understand enough to know how to fix. I have been watching some videos on doing this though and feel like my script is mostly in line with what they taught.



(function executeRule(current, previous /*null when async*/) {

 

// Add your code here
// Insert a IP address from cmdb_ci_ip_address to configuration item
var IPgr = new GlideRecord('cmdb_ci_ip_address'); // look in the IP address table
IPgr.addQuery('cmdb_ci_computer',current.ip_address); //look for items with the same CMDB_CI to see if there's another IP on the same CI
IPgr.orderByDesc('sys_created_on'); //Added sort so that the newest ones are at the top
IPgr.setLimit(1); //Set limit to 1 which should bring back the most recent log that matches the encoded query. We only want to see the most recent.
IPgr.query();
if (IPgr.next()){
// if we find an ip address, we need to update the CI for that ip.
var CIgr = new GlideRecord("cmdb_ci_computer");
CIgr.addQuery("sys_id", current.ip_address);
CIgr.query();
if (CIgr.next()) {
CIgr.ip_address = IPgr.ip_address;
CIgr.update();

}

 

//current.update(); //never use current.update on a update br!
}

 

})(current, previous);