Difference between g_form.getValue and g_form.getReference

TT3
Kilo Guru

New to ServiceNow. I am trying to understand different between g_form.getValue and g_form.getReference. I believe that getReference gives GlideRecord - i.e. the record we have in database, however getValue will return the current value. However, in order to see this in practical, I tried below code for Incident table:

 

//OnChange of category

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var c = g_form.getValue('category');
    alert('c ' + c); //This is working

    g_form.getReference('category', doAlert); // doAlert is our callback 
}

function doAlert(caller) { // reference is passed into callback as first arguments
    alert('category is ' + caller); // This is not working, no alert
    alert('category ' ); // Even this is also not working
}

 

The alerts in doAlert function never show-up. However, if I change getReference to 'caller_id' then it is showing alert.

1 ACCEPTED SOLUTION

MrMuhammad
Giga Sage

Yes, your understanding is correct. getReference returns the GlideRecord of specified field and getValue() returns the current value of field. 

We use getReference on Reference fields to access the values of referenced tables. Let say I want to access the email id of caller on the incident table so I will do getReference and dot walk to get the email of the caller. 

your script is not showing alert because you have used getReference on choice fields. Try below code. 

//OnChange of category

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var c = g_form.getValue('category');
    alert('c ' + c); //This is working

    g_form.getReference('caller_id', doAlert); // doAlert is our callback 
}

function doAlert(caller) { // reference is passed into callback as first arguments
    alert('category is ' + caller.email); // This is not working, no alert
    alert('category '); // Even this is also not working
}

 

NOTE - we use GlideAjax and script include as a much better alternative to getReference(). 

GlideAjax Docs - https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/server_scripting/reference...

 

Thanks & Regards,

Sharjeel

Regards,
Muhammad

View solution in original post

2 REPLIES 2

MrMuhammad
Giga Sage

Yes, your understanding is correct. getReference returns the GlideRecord of specified field and getValue() returns the current value of field. 

We use getReference on Reference fields to access the values of referenced tables. Let say I want to access the email id of caller on the incident table so I will do getReference and dot walk to get the email of the caller. 

your script is not showing alert because you have used getReference on choice fields. Try below code. 

//OnChange of category

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var c = g_form.getValue('category');
    alert('c ' + c); //This is working

    g_form.getReference('caller_id', doAlert); // doAlert is our callback 
}

function doAlert(caller) { // reference is passed into callback as first arguments
    alert('category is ' + caller.email); // This is not working, no alert
    alert('category '); // Even this is also not working
}

 

NOTE - we use GlideAjax and script include as a much better alternative to getReference(). 

GlideAjax Docs - https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/server_scripting/reference...

 

Thanks & Regards,

Sharjeel

Regards,
Muhammad

Brad Tilton
ServiceNow Employee
ServiceNow Employee

getValue should just give you the sys_id of the record in the reference field where getReference gives you the record as an object in your callback as you've shown. The main issue with trying to test against the category field is that category is a choice field, not a reference field. getReference only works when you run it against a reference field like caller_id, assigned_to, configuration item, etc.