The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Best Practice to Populate Fields from a User Field Entry

jateater
Kilo Explorer

I am sure this must have been answered by now, but I can only find where the fields are being populated from the current user logged in.

 

My client wants to have a form section that list all the information about a selected user.   I do understand the there is info box is next to the listed user, that shows all the information they are looking for, but they need this for printing reasons.

 

Full Name Field -> Referenced -> User

 

Full Name.png

I am thinking I could try a client script - onChange -> LXK Full Name

but I am not sure how to set the value of the other fields from LXK Full Name.

 

I know if I was looking for the information from the logged in user, it would be a simple script.

javascript:gs.getUser().getManagerID()

 

Would it be something like this?

 

var fullname = 'u_fullname';

var manager = 'u_manager';

 

fullname = g_form.tableName + '.' + fullname;

g_form.setValue('manager' , 'fullname.getManagerID()' );

 

Give me a idea of how to do one and I can figure out the rest.

 

Thanks

1 ACCEPTED SOLUTION

Mark Stanger
Giga Sage

I think if you need it on the form for printing reasons the best practice is just to drill into the reference field while personalizing the form to add the related fields.   The autofill would happen automatically in that case with no additional scripting necessary.



The only reason you wouldn't want to do that is if you wanted your users to be able to change the information in the fields being displayed...i.e. the user information to populate might not always be the same as the referenced record so you want to default it but allow it to be changed.



If that's the case, the best practice is to use an onChange client script with an asynchronous 'getReference' call.   This SNGuru article explains this method...



http://www.servicenowguru.com/scripting/client-scripts-scripting/gform-getreference-callback/



Here's a simplified example...



//Get the caller object so we can access fields


//Calls the 'popCallerInfo' function when the value is returned


var caller = g_form.getReference('caller_id', popCallerInfo);



function popCallerInfo(caller){


      //Value setting section


      //If the form isn't loading and the new value isn't equal to the old value


      if(!isLoading && newValue != oldValue){


              g_form.setValue('location', caller.location);


              //Set additional values here...


      }


}


View solution in original post

2 REPLIES 2

marcguy
ServiceNow Employee
ServiceNow Employee

Hi,



You wouldn't need to script anything if you just add the derived fields from the user lookup record to the form.



Displaying Information from Other Records - ServiceNow Wiki


Mark Stanger
Giga Sage

I think if you need it on the form for printing reasons the best practice is just to drill into the reference field while personalizing the form to add the related fields.   The autofill would happen automatically in that case with no additional scripting necessary.



The only reason you wouldn't want to do that is if you wanted your users to be able to change the information in the fields being displayed...i.e. the user information to populate might not always be the same as the referenced record so you want to default it but allow it to be changed.



If that's the case, the best practice is to use an onChange client script with an asynchronous 'getReference' call.   This SNGuru article explains this method...



http://www.servicenowguru.com/scripting/client-scripts-scripting/gform-getreference-callback/



Here's a simplified example...



//Get the caller object so we can access fields


//Calls the 'popCallerInfo' function when the value is returned


var caller = g_form.getReference('caller_id', popCallerInfo);



function popCallerInfo(caller){


      //Value setting section


      //If the form isn't loading and the new value isn't equal to the old value


      if(!isLoading && newValue != oldValue){


              g_form.setValue('location', caller.location);


              //Set additional values here...


      }


}