g_form.getReference returns an undefined value

Jon23
Mega Sage

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';

Capture.PNG

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?

20 REPLIES 20

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Jwalton,



This line g_form.setValue('opened for', parentRecord.opened_for); should be g_form.setValue('opened_for', parentRecord.opened_for);


Basically I can see the column name of the field opened for has blank space in it. Make sure the column name of the field is correct.



Please let me know if you have any questions.


Tanaji Patil
Tera Guru

Hi jwalton,



I guess 'opened_for' field in 'parentRecord.opened_for' is not an OOB field. If it is a custom field then please check the field name as it should have 'u_' prefixed to it (replace 'opened for' and 'opened_for' with 'u_opened_for' in your code). And after this as mentioned by Pradeep you should use field names and not the field labels your script.


Jon23
Mega Sage

Pradeep Sharma, thanks for spotting that typo. I have corrected it:



g_form.setValue('opened_for', parentRecord.opened_for);



However, i am still not returning the value from the parent based on my log output.



Tanaji Patil, this is an OOB field from the 'Facilities Request' table: here is the dictionary entry:



Capture.PNG


Hello, could it be that the opened_for is not set in the parent (FCR0010085)?



Thanks,


Berny