How to update userid on user table for bulk records in servicenow?

Ujjwala1
Tera Contributor

Hello Developers,

I have been trying to update to update userid which is on user table for 1000+records,

It is not taking correct format, I guess there is something wrong in the script.

I want to update userid as 'AZ123@company.com' , Could anyone suggest where should i make changes in the fix script so that script will successfully update the records that we have in our system,

I am using below script, for updating records via fix script

 

Fix Script:

 

var gr = new GlideRecord('sys_user');

gr.addQuery('sys_domain', '');
gr.addQuery('first_name', 'Rest');
var ans = desc.match(/([A-Z][A-Z]|[a-z][a-z])-[0-9][0-9][0-9]|/gi);
gr.addQuery('last_name', ans);
gr.query();
if (gr.next())


{


var str = gr.user_name;
var res = str.replace('-', '');


var res = str.concat('@company.com');

 

gr.user_name = res;


gr.setWorkflow(false);


gr.update();


}

 

 

Any kind of help is highly appreciated here,

Thanks & Regards,

Ujjwala

 

1 ACCEPTED SOLUTION

Pavankumar_1
Mega Patron

Hi @Ujjwala1 ,

can you try this script. if loop will not work for bulk update

 

var gr = new GlideRecord('sys_user');
gr.addQuery('sys_domain', '');
gr.addQuery('first_name', 'Rest');
var ans = desc.match(/([A-Z][A-Z]|[a-z][a-z])-[0-9][0-9][0-9]|/gi);
gr.addQuery('last_name', ans);
gr.query();
while (gr.next()) {

var str = gr.user_name;
var res = str.replace('-', '');
var res1 = res.concat('@company.com');
gr.user_name = res1;
gr.setWorkflow(false);
gr.update();
}

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

6 REPLIES 6

Voona Rohila
Kilo Patron
Kilo Patron

Hi Ujjwala1

 

You want to update username in what format?Can you explain in detail of your requirement with few sample examples?

 

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Hi,

I have to update user_name field which is on user table in this format 'AZ123@copado.com'

Actually I had imported some user records via transform map , after that user id took similar value as email is having,

PFA ss for more information,

Expected Output.png

Ujjwala1_1-1665674676427.png

 

Pavankumar_1
Mega Patron

Hi @Ujjwala1 ,

can you try this script. if loop will not work for bulk update

 

var gr = new GlideRecord('sys_user');
gr.addQuery('sys_domain', '');
gr.addQuery('first_name', 'Rest');
var ans = desc.match(/([A-Z][A-Z]|[a-z][a-z])-[0-9][0-9][0-9]|/gi);
gr.addQuery('last_name', ans);
gr.query();
while (gr.next()) {

var str = gr.user_name;
var res = str.replace('-', '');
var res1 = res.concat('@company.com');
gr.user_name = res1;
gr.setWorkflow(false);
gr.update();
}

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

AnubhavRitolia
Mega Sage
Mega Sage

Hi @Ujjwala1 

 

What I understand from your requirement is that you just want to remove '-' hyphen from your user ID, right?

 

Else can you give sample if this is not right?

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023