Script : how to check and code and insert or an update of table?

vanessaheux
Tera Contributor

Hello

I have a workflow activity (run script one) in which I need to create a user in servicenow.

This is the code I have today.

var user = new GlideRecord('sys_user');

user.initialize();

user.user_name = workflow.scratchpad.vid;

user.first_name = current.variables.user_firstname;

user.last_name = current.variables.user_lastname;

user.u_employee_type = workflow.scratchpad.employeeType;

user.manager = current.variables.user_manager;

user.company = current.variables.user_company;

user.country = current.variables.user_location.u_country;

user.insert();

 

This is ok except when the user is already existing in servicenow.

In this case I need to do an update but how to script it? I know I need to do an update in this case but how to code?

The check must be done on filed user_name.
If the user_name already exists, then I need to update the record if not insert the record.

 

Thanks for your inputs.

Vanessa

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

then query first and if found then update; if not then create

Please check whether you want user_name also to get updated if user is found

var user = new GlideRecord('sys_user');
user.addQuery('user_name',workflow.scratchpad.vid);
user.query();
if(user.next()){
	user.user_name = workflow.scratchpad.vid;
	user.first_name = current.variables.user_firstname;
	user.last_name = current.variables.user_lastname;
	user.u_employee_type = workflow.scratchpad.employeeType;
	user.manager = current.variables.user_manager;
	user.company = current.variables.user_company;
	user.country = current.variables.user_location.u_country;
	user.update();
}
else{
	user.initialize();
	user.user_name = workflow.scratchpad.vid;
	user.first_name = current.variables.user_firstname;
	user.last_name = current.variables.user_lastname;
	user.u_employee_type = workflow.scratchpad.employeeType;
	user.manager = current.variables.user_manager;
	user.company = current.variables.user_company;
	user.country = current.variables.user_location.u_country;
	user.insert();
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

then query first and if found then update; if not then create

Please check whether you want user_name also to get updated if user is found

var user = new GlideRecord('sys_user');
user.addQuery('user_name',workflow.scratchpad.vid);
user.query();
if(user.next()){
	user.user_name = workflow.scratchpad.vid;
	user.first_name = current.variables.user_firstname;
	user.last_name = current.variables.user_lastname;
	user.u_employee_type = workflow.scratchpad.employeeType;
	user.manager = current.variables.user_manager;
	user.company = current.variables.user_company;
	user.country = current.variables.user_location.u_country;
	user.update();
}
else{
	user.initialize();
	user.user_name = workflow.scratchpad.vid;
	user.first_name = current.variables.user_firstname;
	user.last_name = current.variables.user_lastname;
	user.u_employee_type = workflow.scratchpad.employeeType;
	user.manager = current.variables.user_manager;
	user.company = current.variables.user_company;
	user.country = current.variables.user_location.u_country;
	user.insert();
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader