Script Include returning null

Raviteja K
Tera Expert

Hello Everyone

I have following client script

var userDet = g_form.getReference('openedfor');

var ga = new GlideAjax('HRManagerApprovalCheck');

ga.addParam('sysparm_name','ApprovalCheck');

ga.addParam('sysparam_userDet','userDet');

ga.getXML(GetCountryDetails);

function GetCountryDetails(response) {

    var answer = response.responseXML.documentElement.getAttribute("answer");

    alert(answer);

}

and following script include

getUserData: function()

{

var userDet = this.getParameter('sysparam_userDet');

var det = userDet.country;

var countryapp='';

var user = new GlideRecord('u_hr_country_and_approval_lookup');

user.addQuery('u_country',det);

user.query();

while (user.next()) {

countryapp=user.u_line_manager_approval_required;

}

return countryapp;

},

This should Yes, but returning null

Need to do any changes to the client script or script include.

Thanks in advance.

1 ACCEPTED SOLUTION

PriyaRanji
Tera Guru

Hi Ravi,



Try to make some changes as below and run it from your end. Hope it works!



I have added the logs & alerts to check the values that we receive and to find out where it exactly goes wrong in case it fails.



I have commented some code as it is not required, and highlighted the newly added code for your reference.



Client Script:



//var userDet = g_form.getReference('openedfor');


var country = g_form.getReference('openedfor').country;/*it will returns you the country */


alert('The country is'+country);


var ga = new GlideAjax('HRManagerApprovalCheck');


ga.addParam('sysparm_name','ApprovalCheck');


//ga.addParam('sysparm_userDet',userDet);


ga.addParam('sysparm_country',country);


ga.getXML(GetCountryDetails);



function GetCountryDetails(response) {


    var answer = response.responseXML.documentElement.getAttribute("answer");


    alert(answer);


}



Script include Name : HRManagerApprovalCheck



ApprovalCheck : function()


{


//var userDet = this.getParameter('sysparm_userDet');



var country = this.getParameter('sysparm_country');


gs.log('country is'+country);



//var det = userDet.country;



var countryapp='';


var user = new GlideRecord('u_hr_country_and_approval_lookup');


user.addQuery('u_country',country);


user.query();


if(user.next()) {


gs.log('I m in');


countryapp=user.u_line_manager_approval_required;


gs.log('countryapp:'+countryapp);


}


return countryapp;


},



Please let me know in case of any concerns. Please mark it as correct/helpful based on the impact of the response you get!



Thanks,


Priyanka R


View solution in original post

16 REPLIES 16

Raju Koyagura
Tera Guru

var det = userDet.country will not return any value so you will not get the result, you can use call back function to get the uuserDet and det values var userDet = g_form.getReference('openedfor'); And also check   is this the right element name openedfor or is this opened_for?


http://wiki.servicenow.com/index.php?title=GlideForm_(g_form)#getReference


Ujjawal Vishnoi
Mega Sage
Mega Sage

Hi Ravi,



Please modify your script as mention below.



var userDet = g_form.getValue('openedfor');


var ga = new GlideAjax('HRManagerApprovalCheck');


ga.addParam('sysparm_name','ApprovalCheck');


ga.addParam('sysparam_userDet','userDet');


ga.getXML(GetCountryDetails);




function GetCountryDetails(response) {


    var answer = response.responseXML.documentElement.getAttribute("answer");


    alert(answer);


}



ApprovalCheck: function()


{


var det ;


var userDet = this.getParameter('sysparam_userDet');


var gr = new GlideRecord('sys_user');


gr.addQuery('sys_id',userDet);


gr.query();


if (gr.next())


{


det=gr.country;


}


var countryapp='';


var user = new GlideRecord('u_hr_country_and_approval_lookup');


user.addQuery('u_country',det);


user.query();


if(user.next()) {


countryapp=user.u_line_manager_approval_required;


}


return countryapp;


},



Hope this helps.



Regards


Ujjawal


PriyaRanji
Tera Guru

Hi ravi,



I hope you go with wrong syntax :-


ga.addParam('sysparam_userDet','userDet');



Can you please update the code to :


ga.addParam('sysparm_userDet','userDet');



And also please change the same in the script include :-



Instead of this,



var userDet = this.getParameter('sysparam_userDet');


Please change it as below :-


var userDet = this.getParameter('sysparm_userDet');



It is better to add the logs to check the values you are passing and answer it returns.



Hope it works now!



Thanks,


Priyanka R



PriyaRanji
Tera Guru

Hi Ravi,



Try to make some changes as below and run it from your end. Hope it works!



I have added the logs & alerts to check the values that we receive and to find out where it exactly goes wrong in case it fails.



I have commented some code as it is not required, and highlighted the newly added code for your reference.



Client Script:



//var userDet = g_form.getReference('openedfor');


var country = g_form.getReference('openedfor').country;/*it will returns you the country */


alert('The country is'+country);


var ga = new GlideAjax('HRManagerApprovalCheck');


ga.addParam('sysparm_name','ApprovalCheck');


//ga.addParam('sysparm_userDet',userDet);


ga.addParam('sysparm_country',country);


ga.getXML(GetCountryDetails);



function GetCountryDetails(response) {


    var answer = response.responseXML.documentElement.getAttribute("answer");


    alert(answer);


}



Script include Name : HRManagerApprovalCheck



ApprovalCheck : function()


{


//var userDet = this.getParameter('sysparm_userDet');



var country = this.getParameter('sysparm_country');


gs.log('country is'+country);



//var det = userDet.country;



var countryapp='';


var user = new GlideRecord('u_hr_country_and_approval_lookup');


user.addQuery('u_country',country);


user.query();


if(user.next()) {


gs.log('I m in');


countryapp=user.u_line_manager_approval_required;


gs.log('countryapp:'+countryapp);


}


return countryapp;


},



Please let me know in case of any concerns. Please mark it as correct/helpful based on the impact of the response you get!



Thanks,


Priyanka R


PriyaRanji
Tera Guru

Hi Ravi,



Good Day



Hope we have answered your query, please try the code provided by us and mark the answer as correct/helpful based on the impact of the response, so that it will gets removed from unanswered list.



Thanks,


Priyanka R