Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Get Phone Number from user record

sn624
Kilo Explorer

Hi Community,

I am trying to pull the requester's phone number from their file using the script below, however it's not working. Does anyone have any suggestions, as to what I am doing wrong here?

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

  if (isLoading) return;

  if (newValue == "") {

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

  return;

  }

  //Call Script Include to get User phone number

  var ga = new GlideAjax("GetPhoneData");

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

  ga.addParam("sysparm_user", g_form.getValue("new_phn_loc"));

  ga.getXML(updatephone);

}

function updateLocation(serverResponse) {

  //Process response from Script Include

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

  g_form.setValue("phone", answer);

}

9 REPLIES 9

Use alerts on g_form.getValue("new_phn_loc") before glide ajax and "answer" in th callback function to see if you are getting both the values.



make sure that the script include is client callable.


-Anurag

such as this?



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


  if (isLoading) return;


  if (newValue == "") {


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


  return;


  }


  //Call Script Include to get User phone number


  g_form.getValue("new_phn_loc"));var ga = new GlideAjax("GetPhoneData");


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


  ga.addParam("sysparm_user");


  ga.getXML(updateLocation);


}



function updateLocation(serverResponse) {


  //Process response from Script Include


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


  g_form.setValue("phone", answer);


}


Hit this



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


  if (isLoading) return;


  if (newValue == "") {


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


  return;


  }


  //Call Script Include to get User phone number


alert(g_form.getValue("new_phn_loc"));


var ga = new GlideAjax("GetPhoneData");


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


  ga.addParam("sysparm_user");


  ga.getXML(updateLocation);


}



function updateLocation(response) {


  //Process response from Script Include


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


alert(answer);


  g_form.setValue("phone", answer);


}




Also just cross check that client callable check box on the "GetPhoneData" script include should be checked.


-Anurag

Are you receiving any errors in the console?   You can also use the JavaScript Debug Window to check but I am preferential to using the console within the browser.   Also, can you share a couple details:



1.   The code for the GetPhoneData script include


2.   Browser you are using (sometimes can cause an issue)


navneetaman
Kilo Explorer

Hi,


You can take help from below example.Requirement is same: To Auto-Populate the Mobile number once Student Roll Number is entered.



Table: u_student (Customized table)


Query Information From: u_student



Script Include:



var AjaxCal4AutoPopulate = Class.create();


AjaxCal4AutoPopulate.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  getAutoPopulate : function(){


  var data ;


  var roll_no = this.getParameter('sysparm_u_number');


  var gr = new GlideRecord("u_student");


  gr.addQuery("u_number", roll_no); // need to query


  gr.query();



  if (gr.next()) {


  var obj = {}; // JSON scripting for fetching multiple value. Otherwise we can use setAttribute or getAttribute


  obj.var1 = gr.getValue('u_first_name');


  obj.var2 = gr.getValue('u_last_name');


  obj.var3 = gr.getValue('u_mobile');


  var json = new JSON();


  data = json.encode(obj);//JSON formatted string


  }


  return data;



  },



  type: 'AjaxCal4AutoPopulate'



  });



Client Script:



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


  if (isLoading)   {


  return;


  }


  if(newValue === ''){


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


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


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


  return;


  }


  var ga = new GlideAjax('AjaxCal4AutoPopulate');


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


  ga.addParam('sysparm_u_number',g_form.getValue('u_number'));


  ga.getXML(retrieveAutoPopulate);



}




function retrieveAutoPopulate(response){



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


  //alert(answer); JSON string.


  answer = answer.evalJSON();


  g_form.setValue("u_mobile", answer.var3);


  g_form.setValue("u_first_name", answer.var1);


  g_form.setValue("u_last_name", answer.var2);



}



Please let me know if you need further help.



Thanks,


Navneet Aman