Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Hi @Ankur Bawiskar 

 

As per the requirement, department change should impact only on form/record. Could you please help on this

@Venkataramudu A 

your script is already working fine for fetching the department

ensure your onload script runs only for new record

function onLoad() {

    if (g_form.isNewRecord())
        return;

    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');
        // }
    });
}

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

@Venkataramudu A 

Hope you are doing good.

Did my reply answer your question?

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