Client Script to set a value from another table

rody-bjerke
Giga Guru

Hi,

My mind suddenly stood still.

I have a form: call which has a field company and a field caller.

How to make it so when you choose company, for example "IBM", it will autopopulate the "Caller" field?

The Caller field is a reference field to sys_user and on sys_user there is a field "u_company" that is a reference to the same company table, that the company field on the Call table is.

So when choosing company "IBM" in the Call table, I want to populate Caller field on Call table with the first matching user that has company "IBM" in sys_user table.

Best regards,

1 ACCEPTED SOLUTION

Fabian Kunzke
Kilo Sage
Kilo Sage

Hey,



Most simple solution would probably be an onChange client script for the field "company". Then do a GlideRecord for the sys_user table to return the first user of that company. Should look like this:



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


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


return;


}


var user = new GlideRecord('sys_user');


user.addQuery('company', newValue);


user.query();



if(user.next())


{


var userID = user.sys_id;


g_form.setValue('caller_id', userID);


}



}



Hope this helps.


Greetings



edit: full client script


View solution in original post

9 REPLIES 9

Nevermind the last one


Hello,



I have quickly implemented it in my dev instance. I have changed the original answer to the code i used. I can't recreate the issue you seem to have. Try the code i have provided please.



Greetings


Fabian


SanjivMeher
Kilo Patron
Kilo Patron

You need an onChange Client script and call a glide ajax method to call a script include.



In the glideajax script, do a query on sys_user table based on company, get the first record and return it to client script.



You can find examples in the below link


Examples of asynchronous GlideAjax



Please mark this response as correct or helpful if it assisted you with your question.

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI Rody,



I have one question for you.



Any user which comes first for IBM company should be populated in that Caller field Right?


Client script:


It should be onchange of Company;


var abc = new GlideAjax('script include name');


abc.addParm('sysparm_name','function name');


abc.addParm('sysparm_company',newValue);


abc.getXML(xyz);



function xyz(response) {  
   
var answer = response.responseXML.documentElement.getAttribute("answer");
  alert
(answer); // It will be Sys Id of first user



}




script include:


var Id = this.getParameter('sysparm_company');


var gr = new GlideRecord('sys_user');


gr.addQuery('company',Id);


gr.query();


if(gr.next())


{


return sys_id;


}


i have requierement of choosing incident state to change whenever i change the incident_task state 

can we client script if possible how can