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

PavanK960672992
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();
}

 

View solution in original post

6 REPLIES 6

Voona Rohila
Tera Patron

Hi Ujjwala1

 

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

 

 

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

 

 

 

PavanK960672992
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();
}

 

anubhavritolia
Giga 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?