How update a field for multiple records

Mrman
Tera Guru

Hi,

I am trying to update a string field on user record for some users using the fix script , but I am not able to do it .

Please suggest.

 

var cID = new GlideRecord('sys_user');
cID.addEncodedQuery("user_nameIN");
cID.query();
while (cID.next()) {
var uID = cID.user_name;
var corrid = ['123', '345', '5645', '756', '76767', '09090', '555', '000', '232323', '44444', '22222', '454545', '56565656', '9999', '09888', '7676767'];
for (var k = 0; k <= corrid.length; k++) {
cID.correlation_id = corrid[k];
cID.update();
}

}

 

@Ankur Bawiskar 

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

Hello @Mrman ,

i think with the above code it will update all the records with 7676767 as you are not telling the system that which users needs which number .So that matching is missing in the script and it will pick up the last element in the array and then update it to that number.

Also i observed that in the encoded query you did not give the user sys_ids or names so it will update for all the records with the same value even if you give the sys_ids or names as i said above.

 

So in this case you can store this in array of JSON's like below and update it 

just make sure you  replace your user names in details array 

 

 

var details= [{"user_name1": "123"} ,{"user_name2": "345"}];


for(var i=0; i<details.length ; i++)
{
var names = details[i];

for(var j in names)
{

var cID = new GlideRecord('sys_user');
cID.addQuery("user_name",j);
cID.query();
while (cID.next())
{


cID.correlation_id = names[j];
cID.update();

}
}

}

 

 

Hope this helps 

Mark my answer correct if this helps you 

Thanks

 

 

View solution in original post

9 REPLIES 9

Hi @Mohith Devatte  This worked and able to assign the ID value to each user . Is there any other way to run the script with out building the JSON . I have almost 200 users for which this value needs to be updated. Please suggest

@Mrman in that case you need to build an encoded query for those 200 users and use orderBy to sort it in alphabetical order and any ways you are gonna need that 200 numbers to be stored in an array.

If you don't do it alphabetical order then it might update random records with with random numbers so it should be like 

user_nameIN+"Abhishek,Brandon"; 

then the numbers array you need to keep abhisheks number first and then followed by brandon.

 

In this way also you need to build this array so why to struggle again with orders.

 

Try creating a JSON like above and update them at once as it is a one time activity 

ope this helps 

Mark my answer correct if this helps you 

Thanks

Ankur Bawiskar
Tera Patron
Tera Patron

@Mrman 

how will you determine which user record gets assigned which array element?

what's the logic there?

Is it random?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Mrman 

It would have been nice if you could have shared the complete requirement in your original question itself for quick solution

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar  Thanks for your response .  Henceforth I will explain the requirement clearly in the post.

Just want to check with you if you have any solution for updating the field with out building JSON for 200 users.