Navigating across reference fields in Client Scripts

conradofonseca
Tera Contributor

Hello!

 

I need to access a value from the grandparent of a record through a Client Script. I don't want to use a GlideRecord. Is there a way of doing so?

These are the relations: CHG (grandparent), RITM (parent), SCTASK (where I'm standing). I need to get to the grandparent's state and sys_class_name values. Here's my code:

 

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


    var parent = g_form.getReference('request_item', callback);

    function callback(parent) {
        var grandparent = parent.parent; // This stores the sys_id of the parent as a string.

		// If I could dot-walk through the grandparent's values, the script would work perfectly.
	
        if (!grandparent && grandparent.sys_class_name != 'change_request') {
            return;
        }

        if (grandparent.state != 3 && newValue == 3 || newValue == 4 || newValue == 7) {
            g_form.setValue('state', 1);
            g_form.showFieldMsg('state', 'Please close the related change', 'error');
        }
    }

}

 

1 ACCEPTED SOLUTION

Maik Skoddow
Tera Patron
Tera Patron

It's recommended to load everything which the client script will need for sure on server side and then send the data with the help of the g_scratchpad to the client.

There are many articles and videos aqround that topic available, for example Display Business Rule and g_scratchpad

And an extensive explanation by ServiceNow can be found at https://docs.servicenow.com/bundle/washingtondc-application-development/page/script/client-scripts/c... 

View solution in original post

2 REPLIES 2

Maik Skoddow
Tera Patron
Tera Patron

It's recommended to load everything which the client script will need for sure on server side and then send the data with the help of the g_scratchpad to the client.

There are many articles and videos aqround that topic available, for example Display Business Rule and g_scratchpad

And an extensive explanation by ServiceNow can be found at https://docs.servicenow.com/bundle/washingtondc-application-development/page/script/client-scripts/c... 

Vrushali  Kolte
Mega Sage

Hello @conradofonseca ,

 

Best way to achieve this is using scratchpad variable. You can write a Display Business Rule and populate the g_scratchpad variable, since you are specific about what information you need. Then retrieve this variable in the client script.

 

Display BR :

g_scratchpad.sys_class_name  = 'Value';

g_scratchpad.state   = 'Value';

 

Client Script :

var sys = g_scratchpad.sys_class_name;

var state = g_scratchpad.state   = 'Value';

 

If my answer solves your issue, please mark it as Accepted ✔️ and Helpful 👍 based on the impact.