Client script: Get values from additional columns in reference field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-09-2023 03:19 AM
Dear colleagues,
I have a client script in a variable set and I want to get the value from a reference field that references a CI table.
In this reference field I have configuered (ref_auto_completer=AJAXTableCompleter) that 3 columns are visible: name, ip_address,owned_by.
So when the user searches his CI in the list he sees a list with 3 columns - CI-name, ip_address_owned_by
Of course I can easily get the value of the CI name with:
g_form.getValue('[variabe_name') or g_form.getDisplayValue('[variabe_name').
But I want to get the value of the ip_address which is available in the list when the user searches for the CI.
I know that I could use the sys_id and AJAX to query the database-CI-table, but this is not what I am looking for.
I believe the value of the ip_address is already "there" because it is in the list when the user selects/sees it.
So I am wondering if there is an option to get this value in my client script.
Many thanks
Best regards
Eckhard
P.S:´.: I am on Tokyo

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-09-2023 03:56 AM
@Eckhard -
To get the value of the 'ip_address' field in your client script, you can use the 'getNamedReference' method of the GlideRecord object.
Here is an example of how you can use this method:
- First, create a GlideRecord object for the 'cmdb_ci' table and set the 'sys_id' field of the GlideRecord object to the 'sys_id' value of the selected CI:
var ciGr = new GlideRecord('cmdb_ci');
ciGr.get('sys_id', g_form.getValue('your_reference_field'));
​
- Next, use the 'getNamedReference' method of the GlideRecord object to get the 'ip_address' field value:
var ipAddress = ciGr.getNamedReference('ip_address').getDisplayValue();
- You can now use the 'ipAddress' variable in your client script.
Note: The 'getNamedReference' method returns a GlideElement object, which is why you need to use the 'getDisplayValue' method to get the actual field value.
Kindly mark the response as Correct or Helpful.
Cheers,
Anish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2023 01:17 AM
Dear Anish,
many thanks!
In the meantime I foud out that I can also use getReference() and I already tested it and it works.
Anyway I want to understand your proposal, so my question is:
The GlideRecord and the "get" method from your proposal will then access the database once more correct?
This should be avoided in client scripts or not?
Or do I have a misunderstanding here?
Thanks for clarification.
Best regards
Eckhard

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2023 01:28 AM
Yes, you are correct that in the example provided, a new GlideRecord is created and the 'get' method is used to retrieve the record from the database. This is done to retrieve the 'ip_address' field value based on the 'sys_id' of the selected CI.
It is generally considered best practice to avoid unnecessary database access in client scripts, as it can impact the performance of the system. One way to avoid this is to retrieve all the necessary information during the initial database query, and then store it in a client-side variable. This way you can avoid additional database queries in the client script, and use the stored variables instead.
An alternative way would be to use the 'getValue' method of the g_form object to retrieve the 'ip_address' field value directly from the form, instead of creating a new GlideRecord object and performing a separate query.
It's always important to consider the performance implications of your scripts and choose the most efficient solution that meets the requirement of your use case.
Here's an example of how you can use the 'getValue' method of the g_form object to retrieve the 'ip_address' field value directly from the form, in a client script:
var ipAddress = g_form.getValue('ip_address');
In this example, the 'getValue' method is used to retrieve the value of the 'ip_address' field from the form. The value is then stored in the 'ipAddress' variable, which you can then use in your client script.
It's important to note that for this to work, the 'ip_address' field must be present on the form and must have a value. If the field is not present on the form or does not have a value, the 'getValue' method will return an empty string.
Also, depending on your use case, you may need to make sure that the form is fully loaded and the data is available, before you try to retrieve the value of the 'ip_address' field.
g_form.onLoad(function() {
var ipAddress = g_form.getValue('ip_address');
});
This way, the script will wait until the form is loaded before trying to retrieve the value of the 'ip_address' field.
It is also important to mention that this method of getting the value, is only possible if the client script is running on the same page where the field is present, if it's running on a different page or from a different form, the getValue method will not work.
Kindly mark the response as Correct or Helpful.
Cheers,
Anish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2023 01:50 AM
Hi Anish,
many thanks.
Just a hint: Your proposal to use "g_form.getValue()" does not work, because I do not have a "free text" variable. I have a reference to a ci table which shows name, ip_address, owned_by.
I described this in my initial description.
When I use g_form.getValue() I receive the sys_id of the record.
So I have to use g_form.getReference() with a callback function.