Value from Lookup Select Box copied to string

andrewdunn
Giga Expert

Hi Team,

I have an issue which I am not sure is a bug or just my coding.

The situation is that I have a Record Producer with a Lookup Select Box variable.

What I want to do is extract data from the record that is selected from the above variable.

If the field is reference (instead of ) the following works perfectly as an "On Change" script based on the field "u_site"

(I butchered code that related to displaying telephone number which is why the names of variables may look strange).

Definitions:

  • u_apasiterating = field that I want to add the data to
  • u_site = the field that is the Lookup Select Box. This holds the name of the site that I want to utilise to get the required data from.

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

      if (isLoading)

            return;

      if (newValue == '') {

            g_form.setValue('u_apasiterating', '');

            return;

      }

      if (!g_form.getControl('u_apasiterating'))

            return;

      var caller = g_form.getReference('u_site', setMobile);

}

function setMobile(caller) {

      if (caller)

              g_form.setValue('u_apasiterating', caller.u_site_classification);

}

Any ideas?

Cheers

3 REPLIES 3

Abhinay Erra
Giga Sage

Andrew,



  g_form.getReference is only used for reference fields. It will not work for the look up select box variable. Use GlideAjax and create a script include to do server side manipulation like querying on the table using the display value of the select box and get the values returned to the client side.


GlideAjax - ServiceNow Wiki



Thanks,


Abhinay




Please mark Helpful, Like, or Correct depending on the impact of the response


Jim Coyne
Kilo Patron

As abhi_r mentioned, g_form.getReference() only works with true Reference variables.



In order to save round trips to the server, do you mind if the site classification is shown in the Lookup Select Box?   If not, then you can configure the variable to include the second field to be displayed in the drop down list by adding the field name in the "Lookup label field(s)" field (comma separated):


find_real_file.png



It will then display the two fields in the drop down list, separated by the "|" character:


find_real_file.png



You can then grab the site classification data with some code similar to this (changing variable names for your own of course):



var group = g_form.getControl('group');


var selection = group.options[group.selectedIndex].innerHTML;


var classification = selection.substr(selection.indexOf("|") + 1).trim();


g_form.setValue("string", classification);


Tarish
Kilo Contributor
this helped me but This doesn't work on service portal. It says- There's a javascript error in your browser console. How can I correct this?