Get a field value from a table and set this field value on Service Portal form field

john303
Kilo Explorer

Hi everyone,

I created a table, name is users_phone. This table have a phone_number field and this field filled by it users. I want to create a Request, from the Service Portal and request form have variable name is Own_Phone_Number. So I want to get phone_number field value from users_phone table and set this value request form variable Own_Phone_Number field when users creating Request. I tried catalog client script or business rule, but i didn't get any solution. Please help for   this issue.

1 ACCEPTED SOLUTION

Hello Huseyin,



The following Catalog Client Script should work for you. Create a OnChange script with Field set as the Name of the Field which refers to the user_phone table.




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


    if (isLoading || newValue == '') {


          return;


    }




var gr = new GlideRecord('users_phone');


gr.addQuery('NameOfFieldInUsers_phoneTable', newValue);


gr.query();


if(gr.next()){


g_form.setValue('Own_Phone_Number',gr.mobile_phone);


}



   


}


View solution in original post

11 REPLIES 11

I would create a glideajax solution:



client script



var ga = new GlideAjax('UserPhoneAjax');
ga.addParam('sysparm_name', 'getPhone');
ga.addParam('sysparm_my_user', g_form.getValue('requested_for'));
ga.getXML(setPhone);

function setPhone(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  alert(answer);


g_form.setValue('Own_Phone_Number', answer);


}



Script Include:


name: MyPhone


client callable: true



var UserPhoneAjax= Class.create();
UserPhoneAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  getPhone:function() {


var user = this.getParameter('sysparm_my_user');


var phone = new GlideRecord('users_phone');


phone.addQuery('sys_user', user);


phone.query();


if(phone.next()){


return phone.getValue('sys_id');


}


return ' ';


} ,
  _privateFunction: function() { // this function is not client callable  
  }
});


EashVerma
Giga Expert

Isn't the requirement that upon changing the requested_for, you get his phone_number from a reference table?