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

Shishir Srivast
Mega Sage

If i understand correctly, hr_profile is referring to sys_user table? 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);
}
return myInfo;
},
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 = JSON.parse(answer);
g_form.setValue('hr_profile', myObj.hr_profile);
}
}

thanks @shishir_srivastava!  I just tried your code, but it still doesn't auto-populate the field.  I opened up my console and see this error:

 

Uncaught SyntaxError: Unexpected token o in JSON at position 0
at JSON.parse (<anonymous>)
at ScopedGlideAjax.hrRequestParse [as callbackFunction] (x_dnf_federal_hr_e_federal_hr_cases.do:509)
at ScopedGlideAjax._responseReceived (js_includes_doctype.jsx:11377)
at ScopedGlideAjax._processReqChange (js_includes_doctype.jsx:11235)
at XMLHttpRequest.<anonymous> (js_includes_doctype.jsx:11)

Can we try with code.

 

alert(answer); //Please check what it alerts?
var myObj = JSON.parse(answer);
g_form.setValue('hr_profile', myObj[0].hr_profile);

the alert shows this: org.mozilla.javascript.NativeArray@1370407