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

Yes, opened_for is set on the parent record. It is a required field.


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Jwalton,



Can you check if the column name of the parent field is "parent". Also try to keep the alert statements in client script and check which line is exactly causing the issue.


No sure I follow. The column name of the parent field is 'opened_for', and which alerts are you referring to?


Jwalton,



To get request_for value below is the working code that I have.


Please replace with opened_for and try if it works.



    var reqByName = g_form.getReference('requested_by');




if (reqByName != ''){


                    g_form.setValue('requested_by_id',reqByName.user_name);}


   


}


I updated the client script with your 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('requested_by_id',reqByName.user_name);


   


                }




      jslog("JW-DEBUG: " + parentRecord.opened_for);


      jslog("JW-DEBUG: " + parentRecord.location);


      jslog("JW-DEBUG: " + reqByName);




          }


    }


}



JS debug returned the following:



JW-DEBUG: undefined


JW-DEBUG: c5b48cbb0a0a3c7e01a3328fd5bb601a


JW-DEBUG: [object Object]