How to override autopopulated field

Venkataramudu A
Tera Contributor

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.

 

VenkataramuduA_1-1740136568404.png

 

OnLoad:

 

function onLoad() {
    var biaOwner = g_form.getValue('assigned_to');
    if (!biaOwner) {
        g_form.clearValue('department');
        return;
    }
   
    // Same logic as before, but triggered on form load
    g_form.getReference('assigned_to', function(userRecord) {
        if (userRecord && userRecord.department) {
            g_form.setValue('department', userRecord.department);
        }
        // else {
        //     g_form.clearValue('department');
        // }
    });
}
 
OnChange:
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) {
        return;
    }
    g_form.getReference('assigned_to', function(userRecord) {
        if (userRecord && userRecord.department) {
            g_form.setValue('department', userRecord.department);
        }
        // else {
        //     g_form.clearValue('department');
        // }
    });
}

 

 

7 REPLIES 7

Debasis Pati
Tera Guru

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

HI @Debasis Pati 

 

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

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.

Ankur Bawiskar
Tera Patron
Tera Patron

@Venkataramudu A 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader