Service Now Auto populate fields Script?

andyc2432
Kilo Explorer

Hello I believe I am trying to do something very simple, but this is my first time programming anything. Using the Wiki and stuff I believe I am heading in the right the direction hopefully. I am currently trying to create a form in service now where it auto populate the Full Name, and User Name. The form would look like this

Full Name: Autopopulate

UserName: Autopopulate

Question1

Question2

etc

 

Here is the code I am currently messing around with using the catalog client script. One thing to note that I get the full name of the user to populate on the first field, but when I submit the form it doesnt show up on the ticket. The second part just doesn't work. If there is a better way to do this code please help!

 

function onLoad()

{

        var ur = new GlideRecord("sys_user");

        gr.addQuery("sys_id", G_user.userID);

        gr.query();

        if(gr.next()) {

                  g_form.setValue("reqName", ur.name);

                  g_form.setValue("usrName", ur.user_name);

 

        }

                  g_form.setReadonly("reqName", true);

                  g_form.setReadonly("usrName", true);

       

}

7 REPLIES 7

andyc2432
Kilo Explorer

If I am reading your replay correctly yes I believe it resets everytime. Maybe I explained it poorly so basically the problem I am having is that my userid variable is returning my.full name. Now I assumed all this code looks at the sys user table .I want to return the full name of the user which it does correctly and I want to return the user account id but currently it also returns the full name but when I do something like



Var userName = g_user.userName


Alert ('current user' + userName)


It pop up the field that I want which is andy1234


The solution which I gave you is considering that both your fields are string fields. You can have a reference field for Full name but not for userID. This is because reference fields show only display value and display valu for sys_user table is Full name.


Hope it answers your question.


Vladi1
Kilo Guru

we use following script for loading data from profile in service requests:


please see description for the variables (fields):


ca_nw_login = name (user first last name)


nh_title =variable in the request


ca_dob = variable in the request


userDetails.title = field in user profile (sys_user table)




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


//Populate Requester and Approver Details


      if(g_form.getValue('ca_nw_login') != '') {


              var userDetails = g_form.getReference('ca_nw_login', findMyDetails);


      }


      else {


g_form.setValue('nh_title', '');


g_form.setValue('cost_center' , '');


g_form.setValue('ca_dob' , '');


g_form.setValue('nh_dept' , '');


  }


}


function findMyDetails(userDetails) {


  g_form.setValue('nh_title', userDetails.title);


  g_form.setValue('ca_dob', userDetails.u_date_of_birth__mm_dd_);


  g_form.setValue('nh_costctr', userDetails.cost_center);


  g_form.setValue('nh_dept', userDetails.department);


}