- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2022 12:22 AM - edited 11-30-2022 12:26 AM
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();
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2022 01:16 AM - edited 11-30-2022 01:17 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2022 12:51 AM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2022 01:11 AM
Hi,
I want the each value in the array 'corrid' to be assigned to the list of users queried. please suggest.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2022 01:24 AM
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 "?
col1 | correlation_id |
user1 | 123 345 ・・・ 7676767
|
user2 | 123 345 ・・・ 7676767
|
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2022 01:16 AM - edited 11-30-2022 01:17 AM
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