Script include returns 'undefined' value

mdsannavulla
Kilo Guru

Hi All,

I have written a client callable script include to set some reference field values on SRM form.

But whenever script include returns 'undefined' value the reference field values are setting with some value and the color of the field is changed to green even though the filed is empty. Please see the below screen shot

Untitled.png

Please let me know if you have any idea on this

1 ACCEPTED SOLUTION

I fixed this issue by modifying the client side script.



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


    if (isLoading) {


          return;


    }


if (newValue != '')


{


locationValues(newValue);


}


else{


if(g_form.getValue('location1') != "")


  g_form.setValue('location1',"");


if(g_form.getValue('location2') != "")


  g_form.setValue('location2',"");


if(g_form.getValue('location3') != "")


  g_form.setValue('location3',"");


if(g_form.getValue('location4') != "")


  g_form.setValue('location4',"");


  }


}


function locationValues(newValue){


    var ajax = new GlideAjax('SiteContactChange2');


  ajax.addParam('sysparm_name','getSiiteContact2');


  ajax.addParam('sysparm_id2',newValue);


  ajax.getXML(getResult);


}



function getResult(serverResponse){


var result = serverResponse.responseXML.getElementsByTagName("result");


var locat1 = result[0].getAttribute("loc1");


g_form.setValue('location1',locat1);


var locat2 = result[0].getAttribute("loc2");


if(locat2 != "undefined"){


  g_form.setValue('location2',locat2);


  }


  else


  {


  if(g_form.getValue('location2') != "")


  g_form.setValue('location2',"");


  if(g_form.getValue('location3') != "")


  g_form.setValue('location3',"");


  if(g_form.getValue('location4') != "")


  g_form.setValue('location4',"");


        return;


  }



var locat3 = result[0].getAttribute("loc3");



  if(locat3!= "undefined"){



  g_form.setValue('location3',locat3);


  }


  else


  {


  if(g_form.getValue('location3') != "")


  g_form.setValue('location3',"");


  if(g_form.getValue('location4') != "")


  g_form.setValue('location4',"");


        return;


  }


var locat4 = result[0].getAttribute("loc4");



if(locat4!= "undefined"){



  g_form.setValue('location4',locat4);


  }


  else


  {


  if(g_form.getValue('location4') != "")


  g_form.setValue('location4',"");


  return;


  }



}


View solution in original post

9 REPLIES 9

edwin_munoz
Mega Guru

Hi,



What happens if you return an empty string? Do you get the same behavior?



return typeof your_variable === "undefined" ?   "" : your_variable;


In this way I tried by using return but still it is not working



It is working for first change of field name but when I changed the user name second time the filed colors are changed to green even though the the value is empty



var result = serverResponse.responseXML.getElementsByTagName("result");

var locat1 = result[0].getAttribute("loc1");


g_form.setValue('location1',locat1);


var locat2 = result[0].getAttribute("loc2");


if(locat2 != "undefined"){
alert('not undefine2');
g_form.setValue('location2',locat2);
}
else
{
alert('undefine2');
g_form.setValue('location2',"");
g_form.setValue('location3',"");
g_form.setValue('location4',"");


      return typeof locat2 === "undefined" ?   "" : locat2;
}

Sorry, I meant to use the return statement on the script include to return an empty string instead of undefined.



Can you post the entire code that you are using for this? Script include and full client script?



Thanks.


I fixed this issue by modifying the client side script.



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


    if (isLoading) {


          return;


    }


if (newValue != '')


{


locationValues(newValue);


}


else{


if(g_form.getValue('location1') != "")


  g_form.setValue('location1',"");


if(g_form.getValue('location2') != "")


  g_form.setValue('location2',"");


if(g_form.getValue('location3') != "")


  g_form.setValue('location3',"");


if(g_form.getValue('location4') != "")


  g_form.setValue('location4',"");


  }


}


function locationValues(newValue){


    var ajax = new GlideAjax('SiteContactChange2');


  ajax.addParam('sysparm_name','getSiiteContact2');


  ajax.addParam('sysparm_id2',newValue);


  ajax.getXML(getResult);


}



function getResult(serverResponse){


var result = serverResponse.responseXML.getElementsByTagName("result");


var locat1 = result[0].getAttribute("loc1");


g_form.setValue('location1',locat1);


var locat2 = result[0].getAttribute("loc2");


if(locat2 != "undefined"){


  g_form.setValue('location2',locat2);


  }


  else


  {


  if(g_form.getValue('location2') != "")


  g_form.setValue('location2',"");


  if(g_form.getValue('location3') != "")


  g_form.setValue('location3',"");


  if(g_form.getValue('location4') != "")


  g_form.setValue('location4',"");


        return;


  }



var locat3 = result[0].getAttribute("loc3");



  if(locat3!= "undefined"){



  g_form.setValue('location3',locat3);


  }


  else


  {


  if(g_form.getValue('location3') != "")


  g_form.setValue('location3',"");


  if(g_form.getValue('location4') != "")


  g_form.setValue('location4',"");


        return;


  }


var locat4 = result[0].getAttribute("loc4");



if(locat4!= "undefined"){



  g_form.setValue('location4',locat4);


  }


  else


  {


  if(g_form.getValue('location4') != "")


  g_form.setValue('location4',"");


  return;


  }



}


decarlo
Giga Contributor

Hi Md,



Have you looked at the glideajax wiki? The link below shows a pretty good example on how to return more than one value from a script include via ajax.



GlideAjax - ServiceNow Wiki