GlideRecord - updating records

anfield
Tera Guru

Running a fix script to search the ci table for ci's with certain product model sysids, when I find them I am trying to update the CI product model sysid on the ci record with a different product model sys id. I can find the CI using an encoded query syntax. My question is how do I update them? Tried the syntax below but it is not working. It does enter the while loop but doesnt seem to do the update. What is the correct syntax to do the update? Thanks

 

 


function product_updates_lookup() { 

	model_sysids = [];

	var grm = new GlideRecord('u_product_model_update');

		grm.addQuery('u_model_sysid', '!=' ,'');
		grm.addQuery('u_ci_update', '=', 'yes');
		grm.autoSysFields(false);
		grm.setWorkflow(false);
		grm.query();

		while (grm.next()){

			var model_sysid = grm.getValue('u_model_sysid');
			gs.log('Product Model: sysid: ' + model_sysid);

			var target_modelsysid = grm.u_target_model_sysid;
			gs.log('Product Model: Taregetsysid: ' + target_modelsysid);



	}

	model_ids = ['9b053d82dbc954d0baec561bdc96192c', '63c47982dbc954d0baec561bdc9619f0'];

	gs.log('Product Model: Update: ' + model_ids.length);

	for (var i=0; i < model_ids.length; i++) { 


	var grhasci = new GlideRecord('cmdb_ci');

		grhasci.addEncodedQuery('model_id.sys_id=' + model_ids[i]);
		grhasci.autoSysFields(false);
		grhasci.setWorkflow(false);
		grhasci.query();


		gs.log('Product Model: CI Found: ' + model_ids[i]); 
		gs.log('Product Model: CI Row Count: ' + grhasci.getRowCount());

			while (grhasci.next()){
			
				grhasci.setValue('model_id.sys_id=', target_modelsysid);	
				
				grhasci.update();
				
				gs.log('Product Model: CI Updated: ' + target_modelsysid);
			
		}
	}
}	// ci_model_update();
 product_updates_lookup();
1 ACCEPTED SOLUTION

A I see you updated the code.

At least this will not work:

grhasci.setValue('model_id.sys_id=', target_modelsysid);

Try:

grhasci.setValue('model_id', target_modelsysid);

Do debug if the below while actually is reached. And if it concerns a large number or records, look at what I wrote about updateMultiple.

while (grhasci.next()){

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

8 REPLIES 8

I think this is working now: grhasci.setValue('model_id', target_modelsysid);

Glad that it's working now.

 

Please mark this answer as correct if it solves your question. This will help others who are looking for a similar solution. Also marking this answer as correct takes the post of the unsolved list.
Thanks.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

In that example do you still need a gr.query, after the addQuery?

Mark Roethof
Tera Patron
Tera Patron

And here a Docs page which also contains info on updateMultiple:

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/c_GlideRecordSco...

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn