create ServiceNow User account (sys_user) from a catalog run script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-20-2013 09:57 PM
Looking for a way to create a user account from within a catalog run script. I found some inbound email examples but am not having luck translating this into a catalog run script.
Background:
New Hire Order guide with some basic new hire variables: (firstName, middleName, lastName, physicalRegion, physicalLocation, tmp_usr_record=true)
- We want to fill out the order guide with the tmp_requested_for variable = "New Hire" AND have 1-9 optional items selected. Then submit order and generate REQ and RITMs.
- The first RITM (New Hire) will have a run script (which I'm trying to figure out) that will create a ServiceNow contact record with just a few of the variables from above.
- After the partial user account has been created, then I want to dot walk to the parent REQ and change the "requested for" from "New Hire" to the newly created user record.
- Once the REQ (and the nested RITMs) have the new valid "requested for". then the approvals will start and the approvers will have the needed information to approve or reject the New Hire RITM.
- After both approvals, the following catalog tasks in the "new hire" RITM will be create AD account, then create/update ServiceNow contact record with the remaining new hire variable that will be asked on the "New Hire" maintain item. This last task can be manual at first and eventually automated.
***The thing I'm stuck on is the initial catalog run script to create the initial ServiceNow user record.
Any help would be really appreciated.
Thanks,
-Erik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2013 06:47 AM
Here is one we use in a workflow. Modify it to meet you needs.
var count = 0;
var usr = new GlideRecord('sys_user');
usr.addQuery('u_employee_id', current.variable_pool.employee_id);
usr.addQuery('first_name', current.variable_pool.first_name);
usr.addQuery('last_name', current.variable_pool.last_name);
usr.query();
while(usr.next()){
count++;
}
if(count > 0){
current.comments = 'User is already in Service-Now ' + usr.first_name;
workflow.scratchpad.user_id = usr.sys_id;
}
else{
usr.initialize();
usr.first_name = current.variable_pool.first_name;
usr.last_name = current.variable_pool.last_name;
usr.u_employee_id = current.variable_pool.employee_id;
usr.title = current.variable_pool.job_title;
usr.location = current.variable_pool.location;
var user_id = usr.insert();
workflow.scratchpad.user_id = user_id;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2013 08:02 AM
Hi @Jay,
Thank you so much for the quick reply, this is awesome... I will reply back once I get it working...
really appreciate it...!!!
-Erik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-27-2013 08:02 PM
Hi Jay,
Here is my catalog run script, still having a few issues with reference fields. The lines that are commented out are the ones that do not populate the new contact record.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var usr = new GlideRecord('sys_user');
usr.initialize();
usr.first_name = current.variable_pool.new_emp_firstName;
usr.middle_name = current.variable_pool.new_emp_middleName;
usr.last_name = current.variable_pool.new_emp_lastName;
// usr.sys_domain.name = current.variable_pool.full_name; *** tried both .name and sys_domain.name for the full name but cannot get it to work.
// usr.name = current.variable_pool.full_name;
usr.title = current.variable_pool.new_emp_pos_title;
usr.u_user_type = current.variable_pool.new_emp_status;
usr.u_supervisor = current.variable_pool.new_emp_supervisor;
usr.phone = current.variable_pool.new_emp_phone_num;
// usr.u_correspondence_symbol = current.variable_pool.office_symbol; ***this is a ref that is not working either
// usr.location.parent = current.variable_pool.phy_reg; **** the user record sys_user.location.parent is a reverence field to cmn_location.name and it is not populating
// usr.location = current.variable_pool.phy_loc; **** the user record sys_user.location is a reverence field to cmn_location.name and it is not populating
usr.u_room_number = current.variable_pool.room_cube;
// usr.u_administrative_region = current.variable_pool.new_emp_admin_reg; ****the user record is really the same as phy_reg (Physical Region) on the user contact record there is a ref qual of parent=null
// usr.company = current.variable_pool.new_emp_admin_org; **** This is another ref that is not working, it has a ref qual: u_administrative_organization=true
usr.insertWithReferences();
Also, since this is a provisional user account, any suggestions on how to flag it as such... append -temp to the last name or something or last four of a phone number, just something to make it unique.
Thanks,
-e
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-28-2013 10:01 AM
In order to set reference field values you have to either supply the sys_id of the referenced record or use the setDisplayValue() function.
As an example. If your domain (which is a reference field that relates back to the group table) was named 'Database' and your variable on the form reflected that, and stored the string 'Database', you could either do a GlideRecord query to the sys_user_group table for the string and pull back the sys_id to set the sys_domain field (sample 1) or use setDisplayName() (sample 2).
//sample 1
var doma = new GlideRecord('sys_user_group');
doma.addQuery('name',current.variable_pool.domain_name);
doma.query();
while(doma.next()){
current.sys_domain = doma.sys_id;
}
//sample 2
current.sys_domain.setDisplayValue(current.variable_pool.domain_name);
setDisplayValue() is a lot easier to use but if it something that has the tendency to change names you have to go back and change your code each time.