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

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


It is not a good practice to use GlideRecord in Client Script



Using Glide record in client script



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

Hello,



I agree, if the action performed is a complex data manipulation. Up to that point i like to revert to common sense, meaning that an async ajax call would be confusing to the user at this point and a sync ajax call would probably be slower. This here is a simple query of table data which should not impact perfomance.



Other than that you are technically right. Then though i would implement a serverside script which takes 3 parameters (table, column, value) to cut down on the times this kind of query has to be done. All in all i think an ajax call for this would be overkill, but i have been wrong before.



Thanks for the headsup.



Greetings


Hi Fabian,



Do you know why the OnChange client script on Company field sets the Company blank afterwards? It seems to do so after it has set the user in the Caller field.



Best regards,