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

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Haven't looked at your complete code, though assuming it concerns grhasci.

Withing the while, there's no grhasci.update()
Have you tried that already?

Personally, I wouldn't go for a while in this case. Go for updateMultiple() instead, by far quicker. Also, model_id.sys_id is not needed. model_id is already a sys_id, dotwalking to the sys_id performs another query (= performance loss).

Example updateMultiple:

var gr = new GlideRecord('table_name');
gr.addQuery('field_name', 'field_value');

gr.setValue('field_name', 'field_value');
gr.updateMultiple();

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

Yes. I just didnt have it in the version I posted. Updated it. It has the grhasci.update

Can you share that exact code? Maybe there's just a typo.

Also try to add debugging (or if Orlando, use logpoints). With debugging, you could easily see where your script is still working, and from which points it fails/doesn't reach, if the variables contain the values you would expect, etc.. This helps to pinpoint much quicker

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

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