Service Now Auto populate fields Script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2014 04:09 PM
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);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2014 07:56 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-19-2014 02:32 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-20-2014 06:25 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-20-2014 06:34 AM
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