create ServiceNow User account (sys_user) from a catalog run script?

emyrold
Giga Expert

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

11 REPLIES 11

Thanks so much Jay...!

I'm still not quite getting my mind around it yet.

On the "maintain Item" I have a variable called phy_reg which is a "Lookup Select Box", Lookup from table: [cmn_location], Lookup value field: "Name", Lookup label field(s): name, u_comments, Reference qual: parentISEMPTY

When users fill out the details on the Maintain Item they choose 1 of 14 regions from this lookup Select Box and that value is captured in the Variable and passed into the workflow after checkout. So say they choose 5R from the lookup select box, how do I get this value into the user record.

The sys_user record has a Filed label called Physical Region. When I right-click and personalize Dictionary the Table is [cmn_location], column name is "parent" type = reference, reference qual: = parent=null.

So with that background would you mind helping me with the syntax for both example 1 and 2? Thanks so much! -e


So I think that if you change your 'phy_reg' 'Lookup Select Box' just a little by making the lookup value field 'sys_id'. Then in your script you can just set the value directly without using setDisplayValue().



usr.location = current.variable_pool.phy_loc;


Also, with regards to inserting the record. In my original response you would have seen a section that writes the newly created sys_id for the user being created to the workflow scratchpad. This is done so that you can easily reference the user that was created later in the workflow if you need to. Since you are creating a new record, you can just use a .insert().



var user_id = usr.insert();
workflow.scratchpad.user_id = user_id;


Hi Jay,

I tried changing the 'Lookup Select Box' to the lookup value field 'sys_id' but then My Location filter catalog client script broke so I had to back that change out. I think my "light bulb' has not quite lit up yet - still missing the context for this.



// catalog client script, I not sure what needs to change on this, if I change the lookup value field to be 'sys_id' instead of 'name'

function onChange(control, oldValue, newValue, isLoading) {

if(newValue == oldValue){
return;
}

if(isLoading){
return;
}

//remove all items from subcat drop down to start
// Used the g_form.clearOptions() function instead of g_form.removeOption() function

g_form.clearOptions('phy_loc');

//build a new list of dependent options
var loc = new GlideRecord('cmn_location');
loc.addQuery('parent.name', newValue);
loc.orderBy('name');
loc.orderBy('u_street_address_2');
loc.query();
while(loc.next()){
g_form.addOption('phy_loc', loc.name, loc.name + ' | ' + loc.u_street_address_2);
}
}


Then on the sys_user stuff:



var usr = new GlideRecord('sys_user');
var user_id = usr.insert();

usr.initialize(); // What is the purpose for this?
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.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_room_number = current.variable_pool.room_cube;
usr.insertWithReferences();
usr.locked_out = true;
usr.user_password.setDisplayValue("abc123");
usr.u_access_type.setDisplayValue("Employee");
usr.location.setDisplayValue(current.variable_pool.phy_loc);
usr.u_administrative_region.setDisplayValue(current.variable_pool.new_emp_admin_reg);
usr.company.setDisplayValue(current.variable_pool.new_emp_admin_org);
usr.u_correspondence_symbol.setDisplayValue(current.variable_pool.office_symbol);
usr.u_nh_temp.setDisplayValue("provisional user account"); // I'm currently doing this because I do not yet know how to use scratchpad.
// usr.update(); So do initialize() and update() work together or not related?

workflow.scratchpad.user_id = user_id; // Seems like in one line you are creating the record and storing in scratchpad but not seeing it.

// ---------
// So In another run script, if the RITM is rejected, I branch off and run this run script:

// how would I change this to use the scratchpad above?

var usr = new GlideRecord('sys_user');
usr.addQuery("u_nh_temp", "provisional user account"); // That workaround for not knowing how to use scratchpad.
usr.query();
while (usr.next()) {
gs.print("****** going to delete ******");
usr.deleteRecord();
}




Hi Jay,

Here is my updated script with 3 remaining issues:
1. new block of code to update the requested for on the REQ with the value from the newly created user record. not yet working.
2. could not get the scratchpad line to work, needed to add the line above to do usr.insert()
3. When I run this I get two user records, one with all the new employee info and a second empty record.



var usr = new GlideRecord('sys_user');
var userSysID = usr.insert(); // could this be creating a blank user record?

usr.initialize(); // could this be creating a blank user record?

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.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_room_number = current.variable_pool.room_cube;
usr.locked_out = true;

usr.location.setDisplayValue(current.variable_pool.phy_loc);
// usr.location = current.variable_pool.phy_loc; // This did not work.

usr.u_administrative_region.setDisplayValue(current.variable_pool.new_emp_admin_reg);
usr.company.setDisplayValue(current.variable_pool.new_emp_admin_org);
usr.u_correspondence_symbol.setDisplayValue(current.variable_pool.office_symbol);

usr.user_password.setDisplayValue("abc123");
usr.u_access_type.setDisplayValue("Employee");
usr.u_nh_temp.setDisplayValue("provisional user account");

usr.insert();
//workflow.scratchpad.userSysID = userSysID; // This did not create the user record, need to add usr.insert(); one line above.



// Replace the REQ "Requested For" with the newly created user.

// I think we need some sort of wait for condition + error haneling... if SN account does not get created for some reason.

// User account is being created but the REQ "Requetest For" did not get updated with the new user record.

var reqGR = new GlideRecord("sc_request");
reqGR.addQuery('sys_id', current.request);
reqGR.query();

if (reqGR.next()){
reqGR.requested_for = userSysID;
reqGR.update();
}

//usr.update();
//usr.insertWithReferences();





// ---------
// In another run script, if the RITM is rejected, I branch off and run this run script:

// How do I reference the scratchpad if used above/

var usr = new GlideRecord('sys_user');
usr.addQuery("u_nh_temp", "provisional user account"); // That workaround for not knowing how to use scratchpad.
usr.query();
while (usr.next()) {
gs.print("****** going to delete ******");
usr.deleteRecord();
}




1. See below
2. You're setting the scratchpad variable to a blank user account being created
3. That's because you insert before setting any of the new users parms.


See below.



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.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_room_number = current.variable_pool.room_cube;
usr.locked_out = true;

usr.location.setDisplayValue(current.variable_pool.phy_loc);
// this works because your addOptions client script is setting the .name as the value of the variable

usr.u_administrative_region.setDisplayValue(current.variable_pool.new_emp_admin_reg);
usr.company.setDisplayValue(current.variable_pool.new_emp_admin_org);
usr.u_correspondence_symbol.setDisplayValue(current.variable_pool.office_symbol);

usr.user_password.setDisplayValue("abc123");
usr.u_access_type.setDisplayValue("Employee");
usr.u_nh_temp.setDisplayValue("provisional user account");
//You have to insert tha record last after setting all the field values
workflow.scratchpad.userSysID = usr.insert();


//update the REQ
var reqGR = new GlideRecord("sc_request");
reqGR.get(current.request);
reqGR.requested_for = workflow.scratchpad.userSysID;
reqGR.update();
}


//use the scratchpad like this

var usr = new GlideRecord('sys_user');
usr.get(workflow.scratchpad.userSysID)
usr.deleteRecord();