How to Set a value of field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-08-2017 01:41 PM
I have a field on Form and i want to set the value of that field
so the field should be filled with the Company field value of the User record. I have tried the following code but there is no luck. can someone please hlep its very urgent
g_form.setValue('user',g_user.userID);
var user = g_form.getReference('name','department;department.name');
g_form.setValue('company',user.department.name);
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-08-2017 01:48 PM
From where you want to pull the User details ?
Do you have any User field OR you want to get info from logged in user?
Refer for script -
https://community.servicenow.com/thread/290480
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-08-2017 01:58 PM
Hello
Better use GlideAjax:
Try the following way
1. Script Include:
Name: GetUserDetails
Client callable: Checked
Script:
var GetUserDetails = Class.create();
GetUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDetails: function(){
var email = '';
var name= this.getParameter('sysparm_user_id');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', name);
gr.query();
if(gr.next())
{
email = gr.location.getDisplayValue();
}
return email;
},
type: 'GetUserDetails'
});
2.Client Script:
Let me know if you need anything else
Please mark my response as correct and helpful if it helped solved your question.
-Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-08-2017 02:24 PM
Please use below code
g_form.setValue('user',g_user.userID);
var user = g_form.getReference("name"); // assuming name is reference field form sys_user table
g_form.setValue('company',user.department.name);
Regards,
Sachin

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-08-2017 02:46 PM
FYI. Two level of dotwalking will not work in client script, you need to use GlideAjax to get the department details.