The CreatorCon Call for Content is officially open! Get started here.

how to create user using background script?

munimohan1
Kilo Contributor

Please any one answer.

1 ACCEPTED SOLUTION

PrashantLearnIT
Giga Sage

Hi munimohan,



                                      var usr = new GlideRecord('sys_user');


                                      usr.initialize();


                                      usr.first_name= 'Prashant';        


                                      usr.last_name = 'Kumar';


                                      user.insert();



Mark answer as correct if it satisfies.....



Cheers,


Prashant


********************************************************************************************************
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.

********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect


Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************

View solution in original post

7 REPLIES 7

santoshsahoonis
Kilo Guru

Why do you want to create a single user using Background Script? You can create a user directly by going to User Administration >> Users >> New



If you plan on creating multiple users, then you should import them from an spreadsheet by creating a datasource.


munimohan1
Kilo Contributor

thanks to all



Akshat rai
Tera Contributor

Hi munimohan ,

To create a single user I will recommend you directly utilize Users module to create new i.e. by going to User Administration >> Users >> New

but in case you need to create multiple users at a single time you can utilize the below code :

var user = new GlideRecord('sys_user');

var n = 10;
for(var i =0; i<n;i++)
{
user.initialize(); // for initializing user every time the loop runs
user.user_name = "TESTING"+i;
user.first_name = "TEST";
user.last_name = "ING"+i;
user.mobile_phone = "986745321"+i;
user.insert(); // for inserting the data every time the loop runs

}

NOTE : I have used variable n as the number of users you want to create and provided with 4 random field values to populate for each user . You can play with the fields as per your requirement and check on results.

Thank you