g_form.getReference returns an undefined value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2015 10:59 PM
I am currently trying to create a child ticket with all the information from the parent in it.
Using the new button on the related list 'Facilities Request->Parent';
the following client script is triggered:
function onLoad() {
if(g_form.isNewRecord()){
var dad = g_form.getValue('parent');
if(dad != ''){
var parentRecord = g_form.getReference('parent');
g_form.setValue('opened for', parentRecord.opened_for);
g_form.setValue('location', parentRecord.location);
jslog("JW-DEBUG: " + parentRecord.opened_for);
jslog("JW-DEBUG: " + parentRecord.location);
}
}
}
function onLoad() {
if(g_form.isNewRecord()){
var dad = g_form.getValue('parent');
if(dad != ''){
var parentRecord = g_form.getReference('parent');
g_form.setValue('opened for', parentRecord.opened_for)
The location field is correctly populated on my child ticket, however, the opened_for is not.
Looking at the javascript log i see the following:
JW-DEBUG: undefined
JW-DEBUG: b466f2800a0a3c7e00b84e0d16c206ae
Both fields are reference fields so I am confused why one works and one does not - any suggestions?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2015 10:52 AM
update this code
- var reqByName = g_form.getReference('opened_for');
- if (reqByName != ''){
- g_form.setValue('opened_for_id',reqByName.user_name);
- }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2015 11:01 AM
Updated code:
function onLoad() {
if(g_form.isNewRecord()){
var dad = g_form.getValue('parent');
if(dad != ''){
var parentRecord = g_form.getReference('parent');
g_form.setValue('opened_for', parentRecord.opened_for);
g_form.setValue('location', parentRecord.location);
var reqByName = g_form.getReference('opened_for');
if (reqByName != ''){
g_form.setValue('opened_for_id', reqByName.user_name);
}
jslog("JW-DEBUG: " + parentRecord.opened_for);
jslog("JW-DEBUG: " + parentRecord.location);
jslog("JW-DEBUG: " + reqByName);
}
}
}
JS Log:
JW-DEBUG: undefined
JW-DEBUG: c5b48cbb0a0a3c7e01a3328fd5bb601a
JW-DEBUG: [object Object]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2015 08:58 AM
I was having the same problem - it is because the getReference is returning an object of a higher level table that does not have that field defined. You'll need to use a GlideRecord so that you get back the correct object.
something like:
var grParent = new GlideRecord('parent table name');
if (grParent.get(g_form.getValue('parent'))) {
//your logic goes here...
g_form.setValue('opened_for', grParent.opened_for);
g_form.setValue('location', grParent.location);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2015 09:26 AM
Your work around works perfectly! Thanks! But why doesn't getReferenc(fieldname) returns undefined? I am following the documentation GlideForm (g form) - ServiceNow Wiki and i am using this function as expected. Is this a bug? I would like to use this function. I shouldn't have to work around this function...
Thanks for your help,
Tod Toter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2015 11:02 AM
The issue, at least in this case, has to do with extended tables and the way the fields are accessible as opposed to a problem with getReference.
What is happening here is that I was trying to get a value from a table, let's use Task as the example since it is used heavily. The reference field that I was calling for was a child of Task (let's use Incident), but the relationship is based on the parent Task table. The relationship allows any child of the Task table to be associated, since they are also Tasks - such as Change and others. I was trying to obtain a value that only exists on the Incident table, not on the parent Task table like caller_id. So the getReference is looking at the root table, or parent, and cannot find that field since it is only defined on the child. Another example is:
Table 1 fields
A
B
C
Table 2 (extended from Table 1) fields
A [table 1]
B [table 1]
C [table 1]
D
If you have a list of items from table 1, you will see the items in table 2 since it is extended. If you try to get a value out of field D from Table 1, it doesn't exist even though items in Table 2 are of the same base type as Table 1. So you have to instantiate a specific call to Table 2 in order to get a value out of field D. So getReference will work to get A, B, or C, but not D.
Hope that made sense and wasn't more confusing instead as I don't remember exactly why it was happening, but I think this is the concept that caused the issue.