client scripts

mahesh009
Tera Contributor

How can I use the g_form.getReference() method to populate a reference field based on the value in another field?

1 ACCEPTED SOLUTION

Ramesh_143
Giga Guru

Hi @mahesh009 ,

To use the "g_form.getRefernce()" method in ServiceNow for populating a reference field based on another field's value, you need to create a Client Script that triggers on the "onChange" event of the relevant field. Within this script, call  "g_form.getRefernce()" to retrieve the record associated with the first field, using a callback function to handle the response asynchronously. This allows you to access specific fields from the retrieved record and set them in another reference field without blocking the user interface.

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return; 
}
g_form.getReference('assigned_to', function(assignee) {        
if (assignee) {
g_form.setValue('manager', assignee.manager.sys_id); 
}
});
}

 

 Thanks & regards,
Ramesh

View solution in original post

7 REPLIES 7

Sandeep Rajput
Tera Patron
Tera Patron

@mahesh009 Here is an example.

 

function onChange(control, oldValue, newValue, isLoading) {
    // Make sure the form is not loading before running the script
    if (isLoading || newValue == '') {
        return;
    }

    // Get the reference field value (assuming 'caller_id' is the reference field)
    g_form.getReference('caller_id', function(reference) {
        // reference is the object containing fields from the referenced record
        var callerName = reference.name;
        var callerEmail = reference.email;

        // Log the caller's name and email to the console
        console.log('Caller Name: ' + callerName);
        console.log('Caller Email: ' + callerEmail);

        // Optionally, set values to other fields based on the reference record
        g_form.setValue('caller_name', callerName);
        g_form.setValue('caller_email', callerEmail);
    });
}

Aditya02
Tera Guru

Hi @mahesh009 ,

 

This can help you to find the better solution for your question. Please refer this once:

Solved: Auto populate Reference Field based on other Selec... - ServiceNow Community

 

 

While g_form.getReference() is immensely useful, it’s important to use it judiciously. Remember, every use of this method involves a server call, which can introduce latency and affect page performance. Avoid overusing it in scripts where possible, and always prefer callback functions to prevent browser lock-ups.

 

===================================***************=========================================

"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!"

 

Thanks & Regards,

Aditya

=====================================***********==========================================

VishaalRanS
Tera Guru

Hi @mahesh009 

 

The g_form.getReference() method retrieves the entire record of a specified reference field asynchronously, allowing you to access various fields from that record. This is useful for populating another field based on the data retrieved.

Implementation Steps:

  1. Identify the Fields
  2. Create an onChange Client Script
  3. Use g_form.getReference():

 

Here’s an example of how to implement this:

 

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

    // Check if the form is loading or if there's no new value

    if (isLoading || newValue === '') {

        return;

    }

 

    // Use getReference to retrieve the user record

    g_form.getReference('requested_for', function(userRef) {

        // Check if userRef is valid

        if (userRef) {

            // Set the location field based on the user's location

            g_form.setValue('location', userRef.location.toString());

        }

    });

}

Please go through the below link for further details:

Solved: What is use of getReference(). - ServiceNow Community

 

Thanks, and Regards

Vishaal

Please mark this response as correct or helpful if it assisted you with your question.