Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Using loop to update record via GlideRecord

Moy1
Kilo Guru

I have 3 custom fields in the sn_hr_core_profile table. The details are as follows:

1. u_org_1 (Reference to can_department table)
2. u_org_2 (Reference to can_department table)
3. u_org_3 (Reference to can_department table)

I am running a background script to update these fields. For the purpose of simplification, I have simplified my code:

var users = new GlideRecord('sn_hr_core_profile');
users.query();
var orgNumber='';

while(users.next()){
	var k=0;
	for(k=0;k<3;k++){
		orgNumber.initialize();
		orgNumber = 'u_org_'+k;
		users.orgNumber = '835e6c81db12f748ed4e94f7db9619a9';
         }
users.update();
}

Using this code I am unable to update the records. However, when I try something like:

users.u_org_1 = '835e6c81db12f748ed4e94f7db9619a9';
users.u_org_2 = '835e6c81db12f748ed4e94f7db9619a9';
users.u_org_3 = '835e6c81db12f748ed4e94f7db9619a9';​

it works perfectly. Any suggestions on what I am might be doing wrong?

 

 

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

What is it you are trying to achieve? Just to update all records having the same u_u_org_1, u_org_2 and u_org_3? Have you considered an easier query with update multiple? I also see a .initialize(). What purpose did you had in mind? The code doesn't make sence to me, though I don't know the functional thought.

Something like:

var gr = new GlideRecord('sn_hr_core_profile');
gr._query();

gr.setValue('gr.u_org_1', '835e6c81db12f748ed4e94f7db9619a9');
gr.setValue('gr.u_org_2', '835e6c81db12f748ed4e94f7db9619a9');
gr.setValue('gr.u_org_3', '835e6c81db12f748ed4e94f7db9619a9');
gr.updateMultiple();

Or is your looping the purpose? Just as exercise or something?

Maybe something like this (untested!):

var gr = new GlideRecord('sn_hr_core_profile');
gr._query();

for(var orgInt = 1; orgInt  < 4; orgInt ++) {
	gr.setValue('gr.u_org_' + orgInt, '835e6c81db12f748ed4e94f7db9619a9');
}
gr.updateMultiple();

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

Kind regards,
Mark

---

LinkedIn

 

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

2 REPLIES 2

Mark Roethof
Tera Patron
Tera Patron

Hi there,

What is it you are trying to achieve? Just to update all records having the same u_u_org_1, u_org_2 and u_org_3? Have you considered an easier query with update multiple? I also see a .initialize(). What purpose did you had in mind? The code doesn't make sence to me, though I don't know the functional thought.

Something like:

var gr = new GlideRecord('sn_hr_core_profile');
gr._query();

gr.setValue('gr.u_org_1', '835e6c81db12f748ed4e94f7db9619a9');
gr.setValue('gr.u_org_2', '835e6c81db12f748ed4e94f7db9619a9');
gr.setValue('gr.u_org_3', '835e6c81db12f748ed4e94f7db9619a9');
gr.updateMultiple();

Or is your looping the purpose? Just as exercise or something?

Maybe something like this (untested!):

var gr = new GlideRecord('sn_hr_core_profile');
gr._query();

for(var orgInt = 1; orgInt  < 4; orgInt ++) {
	gr.setValue('gr.u_org_' + orgInt, '835e6c81db12f748ed4e94f7db9619a9');
}
gr.updateMultiple();

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

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Hi Mark,

Thanks for you response. Your 2nd suggestion did the trick.

The purpose of this question was to make the loop work. I just could not make the loop work with my script. The real purpose of this question was difficult to explain without providing a lot of context, which I felt was unnecessary and I, therefore, simplified the script.

 

Thanks once again.