Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Auto populate company name from reference field in user table

dmixer
Mega Contributor

Hello all,

I have a bit of an issue I've been poking around with for more than I would care to admit. I've been working on auto populating various string fields, which thanks to the Snow community I'm enjoying some success.

The script I am using is as follows:

function onChange(control, oldValue, newValue, isLoading) {

  var ref = g_form.getReference('u_reference_4', setUser);

}

function setUser(ref) {

  if (ref)

  g_form.setValue('u_string_5', ref.u_end_user_name);

  g_form.setValue('u_string_3', ref.u_rptc_income_st);

g_form.setValue('u_string_1', ref.company); // does not display value, only sys_id

}

Basically, when 'u_reference_4' is set to its value, u_string_5 and u_string_3 auto populate. - this works great...

My issue is, I need to auto populate one more field, u_string_1 from a value in a reference field from my user form that references the core_company table. When I script it in to the above, I either get a "undefined" error or the actual sys_id. I need the end users company name to also auto populate based on the employee ID set in u_reference_4.

Any assistance would be greatly appreciated.

1 ACCEPTED SOLUTION

Hi Daniel,



You can refer the below demo where I have created an example.


https://demo006.service-now.com/login.do


Username : admin


Password : admin


Client script : (BP) Set Location to User


View solution in original post

19 REPLIES 19

Mike,



when I add...



g_form.setValue('u_string_1', ref.company.name);



the field u_string_1 throws a "undefined" error... I tried this before with no joy...


Yeah, client script, sorry.   You can do a simple AJAX call that brings the value back.   The issue is that client scripts do not like dot-walking, since dot-walking is a server call.


I'm not extremely familiar with AJAX or Javacript for that matter...



Do you recommend a good place to start?


Daniel,



Did you try dot_walkin. You need not do any scripting in that case.


I'm somewhat familiar with dot-walking... I did refer to your wiki link but Im not sure how to apply it so that the "company" field auto populates...