The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Script include returning null

Zachary Gumm
Giga Guru

Hi,

The catalog client script & script include is returning null. The scripts are aimed at determing if the location records name is empty.

The empty location record is caused by a separate issue which we are currently unable to resolve.

The purpose of this script include on the service portal is to determine if the locations name is empty, and clear the field if it is empty. This will allow the field to become mandatory.

Typically we would clear the field if the value retrieved from it was empty (equivelant of ""), however the retrieved value is a sys_id which is why I am trying to use a database lookup.

The script will first set the location, and then validate the location using the 'util' object. I have the workaround functioning in onload, but onchange is proving more difficult. Onchange being when a user is submitting the request on behalf of another user.

Catalog client script:

function onChange(control, oldValue, newValue, isLoading) {

if(oldValue == newValue){

return;

}

if (newValue == ""){

g_form.setValue("location", "");

g_form.setValue("contact_number", "");

}

else if (newValue != ""){

var ga = new GlideAjax("TafeCatalogUtilCS");

ga.addParam("sysparm_name", "getRequestorDetails");

ga.addParam("sysparm_requestorID", newValue);

ga.getXML(userDetailParse);

function userDetailParse(response) {

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

answerStr = answerStr.toString();

var arrayAnswerStr = answerStr.split(",");

var locationStr = arrayAnswerStr[0];

var phoneStr = arrayAnswerStr[1];

if (locationStr != ""){

g_form.setValue("location", locationStr);

} else {

g_form.clearValue("location");

}

if (phoneStr != "")

g_form.setValue("contact_number", phoneStr);

var util = new GlideAjax('TafeCatalogUtilCS');

util.addParam("sys_parmname","validateUserLocation");

util.addParam("sys_parm_userLoc",locationStr);

g_form.addInfoMessage("Location str:" + locationStr);

util.getXML(GetXMLParse);

function GetXMLParse(response){

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

g_form.addInfoMessage("Answer:" + answer);

if(answer=="false" || !answer || answer == '' || answer == null){

g_form.clearValue("location");

g_form.addInfoMessage("Clearing location");

}

}

}

//Type appropriate comment here, and begin script below

}

}

Script include function 'validateUserLocation':

validateUserLocation: function (){

var locID = this.getParameter('sys_parm_userLoc');

var cmnRec = new GlideRecord("cmn_location");

var locName = "";

cmnRec.addQuery("sys_id",locID);

cmnRec.query();

while(cmnRec.next()){

locName = cmnRec.name;

}

if(!locName || locName == ""){

locName = false;

} else {

locName = true;

}

return locName;

},

1 ACCEPTED SOLUTION

Shishir Srivast
Mega Sage

I think,calling the function and passing the parameters doesn't have proper syntax, it should be sysparm_name instead of sys_parmname



In client script:


var util = new GlideAjax('TafeCatalogUtilCS');


util.addParam("sysparm_name", "validateUserLocation");


util.addParam("sysparm_userLoc", locationStr);


g_form.addInfoMessage("Location str:" + locationStr);


util.getXML(GetXMLParse);



In script Include:


var locID = this.getParameter('sysparm_userLoc');


View solution in original post

3 REPLIES 3

SanjivMeher
Kilo Patron
Kilo Patron

Can you add this debug statement and run and let me know what you see in system log



validateUserLocation: function (){


var locID = this.getParameter('sys_parm_userLoc');


var cmnRec = new GlideRecord("cmn_location");


var locName = "";


cmnRec.addQuery("sys_id",locID+'');


cmnRec.query();



while(cmnRec.next()){


locName = cmnRec.name;


}


gs.log('--------------locName is-----------------'+locName);


if(!locName || locName == ""){


locName = false;


} else {


locName = true;


}


return locName;


},



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

Shishir Srivast
Mega Sage

I think,calling the function and passing the parameters doesn't have proper syntax, it should be sysparm_name instead of sys_parmname



In client script:


var util = new GlideAjax('TafeCatalogUtilCS');


util.addParam("sysparm_name", "validateUserLocation");


util.addParam("sysparm_userLoc", locationStr);


g_form.addInfoMessage("Location str:" + locationStr);


util.getXML(GetXMLParse);



In script Include:


var locID = this.getParameter('sysparm_userLoc');


Thanks! Worked a treat