Form - Set field default value using Javascript

thiraj
Tera Contributor

Hi Team,

On the change request form, i have a field called "Change Owner". There is also a "company field" which is a reference to the company table. The company record has a field called "u_test" which is a user reference field. (It's like a company manager field with the managers name).

I want the "Change owner" field in the change request form to have the default value of "u_test" based on the company field.

How can this be done?

I have tried "javascript : current.change_owner = company.u_test"

But i did not get anything.

All help is appreciated.

Thank you

1 ACCEPTED SOLUTION

Shishir Srivast
Mega Sage

So, basically, you need to have to change_owner name based upon the company name. I think you can get it done through onChange() client, let's try and see if this helps.



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


    var cmp= g_form.getReference('company', doAlert);


}


function doAlert(cmp) {


        g_form.setValue('change_owner', cmp.u_test);


}


View solution in original post

5 REPLIES 5

gauravchoudhury
Tera Guru

Hi Thiraj,



Try it in a before insert business rule instead.



Example:


current.u_requested_for = current.u_project.u_business_contact;



Else,



Example BR:



When: Display


g_scratchpad.vendorID = current.purchase_order.vendor;


g_scratchpad.vendorName = current.purchase_order.vendor.getDisplayValue();



Example CS:



function onLoad() {


        g_form.setValue('vendor', g_scratchpad.vendorID, g_scratchpad.vendorName);


}



Source: Link


Julian Hoch
ServiceNow Employee
ServiceNow Employee

You'll probably not be able to use a default value, since the default values are initialized before the user can fill in the form.


Since the value for the change owner depends on the company, you'll have to set the value AFTER the user selects the company.


An "on change" client script on the company field should do the trick, although you might have to perform an Ajax Call to look up the manager from the company.


Another way that is easier to implement but less user friendly is an "on before" business rule that is triggered on change of company and sets the change owner.


Shishir Srivast
Mega Sage

So, basically, you need to have to change_owner name based upon the company name. I think you can get it done through onChange() client, let's try and see if this helps.



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


    var cmp= g_form.getReference('company', doAlert);


}


function doAlert(cmp) {


        g_form.setValue('change_owner', cmp.u_test);


}


kiranhegde
Mega Expert

Hi Thiraj,



As the u_test is reference to sys_user table, please use company.u_test.name



Regards,



Kiran