- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2022 11:01 AM
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!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2022 01:19 PM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2022 01:48 PM
Replace this piece of code
var CIgr = new GlideRecord("cmdb_ci_computer");
CIgr.addQuery("sys_id", current.ip_address);
With the below
var CIgr = new GlideRecord("cmdb_ci_computer");
CIgr.addQuery("ip_address", current.ip_address);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2022 06:23 PM - edited 12-27-2022 06:24 PM
@Joshua Wolfe Please update code as below and check
(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.toString()); //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.cmdb_ci.toString());
CIgr.query();
if (CIgr.next()) {
CIgr.ip_address = IPgr.ip_address;
CIgr.update();
}
}
})(current, previous);
Note:
Please send screen shot of when to run if the above code does not work
Please mark as correct answer if this solves your issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2022 01:19 PM
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);