OnLoad Client Script to dot-walk the parent key to retrieve grandparent key

John Prouty
Kilo Guru

I am loading the grandchild record's form.  SN populates the Parent Key automatically, but I want a field that is the grandparent key (the key of the parent of the parent record).  I tried using the GlideRecord but I can't get it to work.  I compared my code the examples I found in the ServiceNow reference page on GlideRecord, but I am doing something wrong.  I just can't see what it is.

p.s. the first part of the onLoad works fine (it hides or shows fields based on the value of Route Type).  It is the second part that doesn't work. 

find_real_file.png

1 ACCEPTED SOLUTION

My bad on point 1. Variable should have been "sysparm_step_id"

On point 2, gr.get(<sys_id>) will get a record with sys_id equal to the argument.

If using query(), use gr.next().

var GetGrandParentKey = Class.create();
GetGrandParentKey.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getKey: function() {
        var sysparm_step_id = this.getParameter('sysparm_step_id');
        var gr = new GlideRecord('x_525091_it_sw_enh_process_model_step');
        if (gr.get(sysparm_step_id)) {
            return gr.process;
        }
        return '';
    },
	
    type: 'GetGrandParentKey'
});

View solution in original post

7 REPLIES 7

Akshay H Mulky
Kilo Guru

Hi John,

Its not recommended to use GlideRecord on client script, rather you can use g_scratchpad or g_form.getReference()

Hitoshi Ozawa
Giga Sage
Giga Sage

GlideRecord used in client script is only able to get parent. To get grandparent value, use GlideAjax with Script Include. Be sure to check "Client callable" in Script Include.

Client Script:

function onLoad() {
   var RouteType = g_form.getValue('route_type');
	
	if (RouteType === '' || RouteType == 'NONE') {
		g_form.setMandatory('route_next_step', false);
		g_form.setDisplay('route_next_step', false);
		g_form.setValue('route_next_step', '');
	}
	else {
		g_form.setDisplay('route_next_step', true);
		g_form.setMandatory('route_next_step', true);
	}
	var ProcessID = g_form.getValue('process');
	var stepid = g_form.get('process_step');
	var ajax = new GlideAjax('GetGrandParentKey');
	ajax.addParam('sysparm_name', 'getKey');
	ajas.addParam('sysparm_step_id', stepid);
	ajax.getXMLAnswer(function(answer) {
		if (answer.length > 0) {
			g_form.setValue('process', answer);
		}
	});
}

Script Include:

var GetGrandParentKey = Class.create();
GetGrandParentKey.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getKey: function() {
        var user_id = this.getParameter('sysparm_step_id');
        var gr = new GlideRecord('x_525091_it_sw_enh_process_model_step');
        if (gr.get(sysparm_step_id)) {
            return gr.process;
        }
        return '';
    },
	
    type: 'GetGrandParentKey'
});

It Seems that GlideRecod API will not work in Scoped Application, if this was in global scope it would have worked, so you can try with Glide Ajax as Ozawa suggested.

 

Mark Helpful/Answered accordingly

John Prouty
Kilo Guru

I tried using Glide Ajax, but I can't get it to work. 

I have two questions.

1) Why is user_id set to the StepID, but never used?

var user_id = this.getParameter('sysparm_step_id');

2) Shouldn't the GlideRecord be followed by an add Query and query() statements?

var user_id = this.getParameter('sysparm_step_id');

var gr = new GlideRecord('x_525091_it_sw_enh_process_model_step');
gr.addQuery('sys_id', step_id);
gr.query();

Here is what I have for the Client Script:

function onLoad() {
var RouteType = g_form.getValue('route_type');

if (RouteType === '' || RouteType == 'NONE') {
// Hide and Null Route Next Step
g_form.setMandatory('route_next_step', false);
g_form.setDisplay('route_next_step',false);
g_form.setValue('route_next_step', '');
}

else {
// Make Route Next Step Visible and Manditory.
g_form.setDisplay('route_next_step',true);
g_form.setMandatory('route_next_step', true);
}

var ProcessID = g_form.getValue('process');

if (ProcessID === ''){
// select Process of the Process Step.
var stepid = g_form.getValue('process_step');
var ajax = new GlideAjax('GetProcessKey');
ajax.addParam('sysparm_name', 'getKey');
ajax.addParam('sysparm_step_id', stepid);
ajax.getXMLAnswer(function(answer) {
if (answer.length > 0) {
g_form.setValue('process', answer);
}
});
}

}

Here is what I have for the script include:

find_real_file.png

Script:

var GetProcessKey = Class.create();
GetProcessKey.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

getKey: function() {
var step_id = this.getParameter('sysparm_step_id');
var gr = new GlideRecord('x_525091_it_sw_enh_process_model_step');
gr.addQuery('sys_id', step_id);
gr.query();
if (gr.get(sysparm_step_id)) {
return gr.process;
}
return '';
},

type: 'GetProcessKey'
});

-------------------------------------------

Protection policy: Read-only

What am I doing wrong?