- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2022 12:13 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2022 12:19 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2022 12:19 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader