How to Populate incident table based on Caller id using Script Include

shaik_irfan
Tera Guru

When ever i select Caller id i need to populate email, phone, manager name, & title, this i need to get using Script include.

PLEASE HELP ME OUT on this

1 ACCEPTED SOLUTION

Rohith Sabbinen
Mega Expert

Hi,



For Auto population of the above defined Variables you can write an Script Include and call the same Script Include in an OnLoad Catalog Client Script as mentioned below:



Script Include:



var requestor_details = Class.create();


requestor_details.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  requestor_info: function() {


  var result = this.newItem("result");


  var logged_user = gs.getUserID();


  var user_detail = new GlideRecord('sys_user');


  user_detail.addQuery('sys_id',logged_user);


  user_detail.query();


  while(user_detail.next())


  {


  result.setAttribute("user",user_detail.sys_id);


  result.setAttribute("phone", user_detail.phone);


  result.setAttribute("email",user_detail.email);


  result.setAttribute("location",user_detail.location);



  }


  },



  type: 'requestor_details'


});


Auto Populate - Script Include.png



2) Then you can write an On Load Catalog Client Script wither on your Catalog Item or on the Variable Set to have the variables populated as shown below:



Script:



function onLoad()


{


  var ga = new GlideAjax("requestor_details");


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


  ga.getXML(ajaxResponse);


  function ajaxResponse(serverResponse) {


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


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


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


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


  var loc = result[0].getAttribute("location");


  g_form.setValue('requestor_phone',phone);


  g_form.setValue('requestor_name',user);


  g_form.setValue('Location_user',loc);


  }


}



Auto Populate - Client Script.png


Replace your Variable Name appropriately in the Set Value Conditions say for example in place of the variable "Location_user" with your Location Variable.



In a Similar Way you can try for other Variables also.


View solution in original post

6 REPLIES 6

Rohith Sabbinen
Mega Expert

Hi Shaik,



Use an onChange catalog client script.   It should work for your needs.



  1. function onChange(control, oldValue, newValue, isLoading) {  
  2. var id = g_form.getValue('u_first_field');//replace 'u_first_field' with the name of your reference field.  
  3. var user = new GlideRecord('sys_user');  
  4.           user.addQuery('sys_id',id);  
  5.           user.query();  
  6. if ( user.next() ) {  
  7.   g_form.setValue('u_manager_field', user.manager);  
  8.   g_form.setValue('u_last_name', user.last_name);  
  9.   g_form.setValue('u_whatever', user.field_on_sys_user);  
  10. }  
  11. }  

Note: Modify the above Script accordingly.




Thank you.


Rohith.


Rohith,



I already achived this via client scirpt since Glide Record is not a Best practice to use in Client script my manager asked me to implement same via Script include.



Can you please help me out   ?


Rohith Sabbinen
Mega Expert

Hi,



For Auto population of the above defined Variables you can write an Script Include and call the same Script Include in an OnLoad Catalog Client Script as mentioned below:



Script Include:



var requestor_details = Class.create();


requestor_details.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  requestor_info: function() {


  var result = this.newItem("result");


  var logged_user = gs.getUserID();


  var user_detail = new GlideRecord('sys_user');


  user_detail.addQuery('sys_id',logged_user);


  user_detail.query();


  while(user_detail.next())


  {


  result.setAttribute("user",user_detail.sys_id);


  result.setAttribute("phone", user_detail.phone);


  result.setAttribute("email",user_detail.email);


  result.setAttribute("location",user_detail.location);



  }


  },



  type: 'requestor_details'


});


Auto Populate - Script Include.png



2) Then you can write an On Load Catalog Client Script wither on your Catalog Item or on the Variable Set to have the variables populated as shown below:



Script:



function onLoad()


{


  var ga = new GlideAjax("requestor_details");


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


  ga.getXML(ajaxResponse);


  function ajaxResponse(serverResponse) {


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


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


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


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


  var loc = result[0].getAttribute("location");


  g_form.setValue('requestor_phone',phone);


  g_form.setValue('requestor_name',user);


  g_form.setValue('Location_user',loc);


  }


}



Auto Populate - Client Script.png


Replace your Variable Name appropriately in the Set Value Conditions say for example in place of the variable "Location_user" with your Location Variable.



In a Similar Way you can try for other Variables also.


Thanks Rohith ,



Can i do this using onChange ?



Mine is Incident table