How to get value from one table to another reference table by client script

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2017 03:42 AM
Hi ,
I have created Name field ,First name ,Last name ,Company field .
I written code for Name field g_form.getrefrence('name') ,i will get the value of -First name ,Last name
but when I tried for company (on user table company have reference table)but it doesnt work.
Client script -onchange
========================
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var x = g_form.getReference('name_list');
//alert(x);
alert(x.company.name);
//if(x.first_Name != ''&& x.last_name !='')
//{
g_form.setValue('First_Name',x.first_name);
g_form.setValue('Last_Name',x.last_name);
g_form.setValue('company',x.company.name);
//}
//Type appropriate comment here, and begin script below
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2017 03:53 AM
Hi Gaurav,
getReference() is not best practice and you can't dot walk to referenced values like company.name. Best practice is to use Glide Ajax to get the values from the server.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2017 04:08 AM
Here is a sample code you could use, make sure you name the script include GetUserDetails and ticket the client callable box. Also, please note, field names are always lower case.
client script
var user = g_form.getValue('name_list'); //i'm assuming this is the name of the reference field on your form that is referencing the user table?
var ga = new GlideAjax('GetUserDetails');
ga.addParam('sysparm_name', 'getDetails');
ga.addParam('sysparm_user', user);
ga.getXMLAnswer('returnUserDetails');
function returnUserDetails(response){
answer = response.evalJSON();
g_form.setValue(first_name', answer.firstname);
g_form.setValue('last_name', answer.lastname);
g_form.setValue('company', answer.company);
}
script include
var GetUserDetails = Class.create();
GetUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDetails: function() {
var user = this.getParameter('sysparm_user');
var gr = new GlideRecord('sys_user');
if(gr.get(user){
var obj = {}
obj.firstname = gr.first_name.toString();
obj.lastname = gr.last_name.toString();
obj.company = gr.company.getValue().toString();
var json = new JSON();
var data = json.encode(obj);
return data
}
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-12-2017 04:01 AM
Hi Gaurav,
I request you to visit this thread
how to copy reference field in text field using client script
I have answered the question there.