How to Set a value of field

ursnani
Giga Guru

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

5 REPLIES 5

Deepak Kumar5
Kilo Sage

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


Prateek kumar
Mega Sage

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:


Capture.PNG


Let me know if you need anything else



Please mark my response as correct and helpful if it helped solved your question.
-Thanks

sachin_namjoshi
Kilo Patron
Kilo Patron

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


Shishir Srivast
Mega Sage

FYI. Two level of dotwalking will not work in client script, you need to use GlideAjax to get the department details.