How to retrieve the sys_id of a newly created User Record

pennieturner
Mega Guru

Hi there

I have a workflow which creates a new record:

var snuser = new GlideRecord('sys_user');

snuser.initialize();

snuser.first_name = current.variables.first_name;

snuser.last_name = current.variables.last_name;

snuser.location = current.variables.location;

snuser.u_start_date = current.variables.start_date;

snuser.title = current.variables.job_title;

snuser.u_end_date = current.variables.end_date;

snuser.company = current.variables.company;

snuser.u_division = snuser.company.parent;

snuser.insert();

From here, I want to populate that new user record into the the requested_for field, but, I am unsure of how to retrieve the sys_id of the new user to be able to add it into the field?

Any help would be appreciated

Thank you in advance.

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Pennie Turner



Here you go.


var snuser = new GlideRecord('sys_user');


snuser.initialize();


snuser.first_name = current.variables.first_name;


snuser.last_name = current.variables.last_name;


snuser.location = current.variables.location;


snuser.u_start_date = current.variables.start_date;


snuser.title = current.variables.job_title;


snuser.u_end_date = current.variables.end_date;


snuser.company = current.variables.company;


snuser.u_division = snuser.company.parent;


var sysId = snuser.insert(); // sysId will contain the record created sys_id


View solution in original post

4 REPLIES 4

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Pennie Turner



Here you go.


var snuser = new GlideRecord('sys_user');


snuser.initialize();


snuser.first_name = current.variables.first_name;


snuser.last_name = current.variables.last_name;


snuser.location = current.variables.location;


snuser.u_start_date = current.variables.start_date;


snuser.title = current.variables.job_title;


snuser.u_end_date = current.variables.end_date;


snuser.company = current.variables.company;


snuser.u_division = snuser.company.parent;


var sysId = snuser.insert(); // sysId will contain the record created sys_id


That simple!   Thank you so much for your quick response


Anurag Tripathi
Mega Patron
Mega Patron

Hi Pennie



var snuser = new GlideRecord('sys_user');


snuser.initialize();


snuser.first_name = current.variables.first_name;


snuser.last_name = current.variables.last_name;


snuser.location = current.variables.location;


snuser.u_start_date = current.variables.start_date;


snuser.title = current.variables.job_title;


snuser.u_end_date = current.variables.end_date;


snuser.company = current.variables.company;


snuser.u_division = snuser.company.parent;


var newsys_id = snuser.insert();



newsys_id is your newly created id


-Anurag

That simple!   Thank you so much for your quick response