Accessing script includes array from onLoad client script

davilu
Mega Sage

i have a rudimentary understanding of script includes and client scripts so apologies in advance.  I am trying to pre-populate two reference fields on a form for a specific view and have the following script includes:

Client Callable is checked

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

hrRequest: function() { 

var gr = new GlideRecord('sn_hr_core_profile');
gr.addQuery('user', gs.getUserID());
gr.query();
var myInfo = [];
if(gr.next()) {
var info = {};
info.hr_profile = gr.getDisplayValue('user');
info.hr_contact = gr.getDisplayValue('x_dnf_hr_contact');

myInfo.push(info);
}
return myInfo;
},
type: 'submit_hr_request'
});

 

I want to setValues on my onLoad client script:

function onLoad() {
var ga = new GlideAjax('submit_hr_request');
ga.addParam('sysparm_name', 'hrRequest');
ga.getXML(hrRequestParse);

function hrRequestParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('hr_profile', ???);
}
}

My HR Profile field is a reference field to the hr_profile table, which is a reference field to sys_user.  My HR Contact field is a custom field we added to the hr_profile table, which also reference sys_user.  If I wanted to set my hr_profile field value to info.hr_profile in my object array, what's the correct syntax for that? 

Thanks.

1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

I would keep it simple. Also you need to return the sys id of the hr profile instead of user sysid

 

Script Include:
var submit_hr_request = Class.create();
submit_hr_request.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
 hrRequest: function() { 
 var myInfo = [];
 var gr = new GlideRecord('sn_hr_core_profile'); 
 gr.addQuery('user', gs.getUserID()); 
 gr.query(); 
 if(gr.next()) {
return gr.getValue('sys_id')+','+gr.getDisplayValue('x_dnf_hr_contact');
 }
 }, 
 type: 'submit_hr_request'
});
 

Client Script:
function onLoad() {
 var ga = new GlideAjax('submit_hr_request');
 ga.addParam('sysparm_name', 'hrRequest');
 ga.getXML(hrRequestParse);
function hrRequestParse(response) {
 var answer = response.responseXML.documentElement.getAttribute("answer");
 var myObj = answer.split(',');
alert(myObj[0]+'   -    '+myObj[1]);
 g_form.setValue('hr_profile', myObj[0]);
 }
}

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

View solution in original post

12 REPLIES 12

update the script include, please check if this helps.

 

Script Include:
var submit_hr_request = Class.create();
submit_hr_request.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
hrRequest: function() {
var myInfo = [];
var gr = new GlideRecord('sn_hr_core_profile');
gr.addQuery('user', gs.getUserID());
gr.query();
if(gr.next()) {
var info = {};
info.hr_profile = gr.getValue('user');
info.hr_contact = gr.getDisplayValue('x_dnf_hr_contact');
myInfo.push(info);
}
var jsonval = new JSON();
var hrData = jsonval.encode(myInfo);
return hrData;
},
type: 'submit_hr_request'
});

thanks! the alert now says null

okay, can we put some info log in script include and see what it captures..

 

var submit_hr_request = Class.create();
submit_hr_request.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
hrRequest: function() {
var myInfo = [];
var gr = new GlideRecord('sn_hr_core_profile');
gr.addQuery('user', gs.getUserID());
gs.info('User Count : ' + gr.getRowCount());
gr.query();
if(gr.next()) {
var info = {};
gs.info('hr_profile : ' + gr.getValue('user'));
gs.info('hr_contact : ' + gr.getDisplayValue('x_dnf_hr_contact'));
info.hr_profile = gr.getValue('user');
info.hr_contact = gr.getDisplayValue('x_dnf_hr_contact');
myInfo.push(info);
}
var jsonval = new JSON();
var hrData = jsonval.encode(myInfo);
return hrData;
},
type: 'submit_hr_request'
});

 

Refer to this article where I explained in detail

https://community.servicenow.com/community?id=community_article&sys_id=25975379dbe0f8d0190dfb2439961937

 

You can also watch the video tutorial here:

SanjivMeher
Kilo Patron
Kilo Patron

I would keep it simple. Also you need to return the sys id of the hr profile instead of user sysid

 

Script Include:
var submit_hr_request = Class.create();
submit_hr_request.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
 hrRequest: function() { 
 var myInfo = [];
 var gr = new GlideRecord('sn_hr_core_profile'); 
 gr.addQuery('user', gs.getUserID()); 
 gr.query(); 
 if(gr.next()) {
return gr.getValue('sys_id')+','+gr.getDisplayValue('x_dnf_hr_contact');
 }
 }, 
 type: 'submit_hr_request'
});
 

Client Script:
function onLoad() {
 var ga = new GlideAjax('submit_hr_request');
 ga.addParam('sysparm_name', 'hrRequest');
 ga.getXML(hrRequestParse);
function hrRequestParse(response) {
 var answer = response.responseXML.documentElement.getAttribute("answer");
 var myObj = answer.split(',');
alert(myObj[0]+'   -    '+myObj[1]);
 g_form.setValue('hr_profile', myObj[0]);
 }
}

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