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

Bhavesh Jain1
Giga Guru

Personalize dictionary on these 2 fields and update the default value.



Default value for Full Name -- javascript:gs.getUser().getFullName()


Default value for UserName --   javascript:gs.getUserName()



Capture.PNG


solutioningnow
Giga Guru

Hi,



As Bavesh said, you can set Default values. You can also achieve the same using onLoad Client Script. Below is the updated script of yours


As there is no need of using GlideRecord, I have commented GlideRecord part.


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.setValue("reqName", g_user.getFullName());


                  g_form.setValue("usrName", g_user.userName);



                  g_form.setReadonly("reqName", true);


                  g_form.setReadonly("usrName", true);


}



You can also refer below wiki url for more information on g_user:


GlideUser (g user) - ServiceNow Wiki



Let me know if you have any queries.



Regards,


Solutioner


andyc2432
Kilo Explorer

Thanks for the reply guys! I'm still having trouble with the code I used the given code it does populate the full name of the requester however it populates it again in the user name.   I want it to populate the company user id so for instance instead of population andy cakes it would populate AC1234.


If we use onload script it will set value every time we open the record. If first time we change the value, after saving the record if we open it again it will reset the value. Please correct me if I am wrong.



Thanks,


Mrinmoy