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

newhand
Mega Sage

HI @Mrman 

What is the problem now?

I read the code, it seems that every record is  be updated for many times and finally be updated to "7676767".

 

for (var k = 0; k <= corrid.length; k++) {
cID.correlation_id = corrid[k];
cID.update();
}

 

 

Please mark my answer as correct and helpful based on Impact.

Hi,

I want the each value in the array 'corrid' to be assigned to the list of users queried. please suggest.

HI

Do you mean you want the recoreds to be updated as  bellow  ?

And can you tell me what is the type of  the Field "correlation_id "?

 

col1correlation_id 
user1

123

345

・・・

7676767

 

user2

123

345

・・・

7676767

 

 

 

Please mark my answer as correct and helpful based on Impact.

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