- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2023 01:41 AM - edited 11-03-2023 01:45 AM
I need to update database records via background script.
The field to be updated is support_grupe.
Each database in the cmdb_ci_database table is associated with an application from cmdb_ci_service.
Based on this affiliation, I am supposed to complete the support_grupe record for a specific database, depending on the application.
1. I create GlideRecord objects for the CMDB_CI_DATABASE and CMDB_CI_SERVICE tables, which are used to interact with the data in these tables.
2. The while loop iterates through all records in the cmdb_ci_database table
3. Inside the loop, the script tries to find records in the cmdb_ci_service table based on the 'Name' field in the cmdb_ci_service table and the u_application field in the cmdb_ci_database table.
4. If a record is found (that is, a record was found where 'name' from cmdb_ci_service matches 'u_application' from cmdb_ci_database), the script rewrites the sys_id from the u_grupa administativ field in the cmdb_ci_service table to the support_group field in the cmdb_ci_database table.
5. after updating the record, the script checks whether the update was successful.
To summarize, this script combines data from two tables cmdb_ci_database and cmdb_ci_service based on the match between the value name from cmdb_ci_service and u_applicaiton from cmdb_ci_database. If a match is found, the script updates the suppoert_grupe field in the cmdb_ci_database table using the sys_id from the cmdb_ci_service record.
var database = new GlideRecord ('cmdb_ci_database');
var service = new GlideRecord ('cmdb_ci_service');
while (database.next()){
service.addQuery('name',database.u_application);
service.query();
if(service.next()){
database.support_group = service.u_grupa_administrative.sys_id;
var updated = database.update();
if(updated)
{
gs.info('Updated the record in cmdb_ci_database for the application' + database.u_application);
}else{
gs.error ('Error updating record in cmdb_ci database for applicationi '+ database.u_application);
}
}else{
gs.info('record not fund cmdb_ci_service for application '+ database.u_application);
}
}
Don't work ??
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2023 01:48 AM
Use database.query() on fourth line
var database = new GlideRecord ('cmdb_ci_database');
var service = new GlideRecord ('cmdb_ci_service');
database.query();
while (database.next()){
service.addQuery('name',database.u_application);
service.query();
if(service.next()){
database.support_group = service.u_grupa_administrative.sys_id;
var updated = database.update();
if(updated)
{
gs.info('Updated the record in cmdb_ci_database for the application' + database.u_application);
}else{
gs.error ('Error updating record in cmdb_ci database for applicationi '+ database.u_application);
}
}else{
gs.info('record not fund cmdb_ci_service for application '+ database.u_application);
}
}
Regards,
Piyush Sain
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2023 01:48 AM
Use database.query() on fourth line
var database = new GlideRecord ('cmdb_ci_database');
var service = new GlideRecord ('cmdb_ci_service');
database.query();
while (database.next()){
service.addQuery('name',database.u_application);
service.query();
if(service.next()){
database.support_group = service.u_grupa_administrative.sys_id;
var updated = database.update();
if(updated)
{
gs.info('Updated the record in cmdb_ci_database for the application' + database.u_application);
}else{
gs.error ('Error updating record in cmdb_ci database for applicationi '+ database.u_application);
}
}else{
gs.info('record not fund cmdb_ci_service for application '+ database.u_application);
}
}
Regards,
Piyush Sain
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2023 01:52 AM - edited 11-03-2023 02:01 AM
I missed such a simple thing eh.. 🙂😛
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2023 01:52 AM
You are not querying 'database' object, please add line database.query();
Best regards
Suyog