update database records via background script - don't work

Pawel Barcik
Mega Guru

 

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 ??

 

PawelBarcik_0-1699001109058.png

 

1 ACCEPTED SOLUTION

piyushsain
Tera Guru
Tera Guru

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);
	}
}

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Piyush Sain

View solution in original post

3 REPLIES 3

piyushsain
Tera Guru
Tera Guru

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);
	}
}

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Piyush Sain

I missed such a simple thing eh.. 🙂😛

Suyog Aptikar
Giga Guru

@Pawel Barcik 

You are not querying 'database' object, please add line database.query();

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.



Best regards

Suyog