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
Mega 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]);
 }
}

View solution in original post

12 REPLIES 12

thanks! that worked but I'm seeing the following warning in my console:

 

*** WARNING *** GlideAjax.getXMLWait - synchronous function - processor: AjaxClientHelper

 

is this anything important?

Thats not because of this client script.

You would have used getXMLWait function somewhere, may be in a onSubmit script.

 

But that should be fine.

Mr world wide
Mega Guru

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: