How to override autopopulated field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2025 03:21 AM
Hi,
I am trying to auto populate 'Department' field based on BIA owner selected from sys_user table. For this I have created Onload and Onchange client scripts. i have additional requirement is if i ovveride Department value to any other value from the lookup for the same BIA owner on form , BIA form should get update with latest 'department' but in cmn_department table it should not be changed.
Can someone help on this.
OnLoad:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2025 03:27 AM
Hello @Venkataramudu A ,
This can be handled through one on change script only which will run on load and on change
Just create one on change script and field as "assigned_to"
On change script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
var biaOwner = g_form.getValue('assigned_to');
if (!biaOwner) {
g_form.clearValue('department');
return;
}
g_form.getReference('assigned_to', function(userRecord) {
if (userRecord && userRecord.department) {
// Set the department ONLY if it hasn't been manually changed
if (!g_form.isUserModified('department')) {
g_form.setValue('department', userRecord.department);
}
}
});
}
Please try the above script if this helps you kindly mark it as helpful/correct.
Regards,
Debasis
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2025 03:42 AM
Thanks for the response. I have tried your given script using onchange, but it didn't worked. department value manually changed to other department then saved the form. again its populating original department from user profile

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2025 03:50 AM
Yes it will because we are running the script on both on load and on change if you save the form and reload then in backen the on load script runs and fetches the same values it fetched earlier.
Can you please explain more in brief about your requirement.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2025 03:37 AM
if you change the department after auto population then it will only impact this form/record and not the actual record from cmn_department
what's your concern?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader