Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

GetReference

SRIRAMSANKAR007
Tera Expert

What is the purpose of getReference(). On my understand it's an API used in client side, to fetch real time data of a referenced record. I need more explanation and real time examples when to use this. And give some two to three examples with code if possible.

1 ACCEPTED SOLUTION

yashkamde
Giga Sage

Hello @SRIRAMSANKAR007 ,

 

Your understanding is correct, adding it's not real time in the sense of live updates, it's an on demand server round trip to pull that referenced record's data into client scripts when you need more than what's already in the reference field's dot-walk cache.

 

So If the field isn't cached, dot-walking client side returns blank. getReference() solves this by fetching the full record from server on demand.

 

For Example :

1) Get caller's department & manager on Incident form :

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    g_form.getReference('caller_id', function(caller) {
        if (caller) {
            g_form.addInfoMessage('Caller Department: ' + caller.department + 
                                   ', Manager: ' + caller.manager);
        }
    });
}

 

2) Conditionally set field based on referenced record's field not in cache :

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    g_form.getReference('cmdb_ci', function(ci) {
        if (ci && ci.operational_status == '1') { // 1 = Operational
            g_form.setValue('priority', '3');
        } else if (ci) {
            g_form.addErrorMessage('Selected CI is not operational!');
        }
    });
}

 

If my response helped mark as helpful and accept the solution.

View solution in original post

2 REPLIES 2

Rafael Batistot
Kilo Patron

Hi @SRIRAMSANKAR007 

 

getReference() is used in Client Scripts to retrieve the full record referenced by a reference field, so you can access its other fields.

 

https://www.servicenow.com/community/developer-forum/quot-how-does-getreference-function-work-in-ser...

 

https://ikconsulting.com/post/understanding-the-getreference-method-in-servicenow-usage-and-best-pra...

 

 

If this response was helpful, please mark it as Helpful and, if applicable, as Correct.
This helps other users find accurate and useful information more easily

yashkamde
Giga Sage

Hello @SRIRAMSANKAR007 ,

 

Your understanding is correct, adding it's not real time in the sense of live updates, it's an on demand server round trip to pull that referenced record's data into client scripts when you need more than what's already in the reference field's dot-walk cache.

 

So If the field isn't cached, dot-walking client side returns blank. getReference() solves this by fetching the full record from server on demand.

 

For Example :

1) Get caller's department & manager on Incident form :

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    g_form.getReference('caller_id', function(caller) {
        if (caller) {
            g_form.addInfoMessage('Caller Department: ' + caller.department + 
                                   ', Manager: ' + caller.manager);
        }
    });
}

 

2) Conditionally set field based on referenced record's field not in cache :

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    g_form.getReference('cmdb_ci', function(ci) {
        if (ci && ci.operational_status == '1') { // 1 = Operational
            g_form.setValue('priority', '3');
        } else if (ci) {
            g_form.addErrorMessage('Selected CI is not operational!');
        }
    });
}

 

If my response helped mark as helpful and accept the solution.