Auto-Populate Reference fields

rishabh31
Mega Sage

Dear Team,

 

I have a requirement-

In the incident table, I want the caller (caller_id) to populate as Impacted User (u_impacted_user) when the Impacted User field is blank.
Then location (u_location) field should populate with the Impacted User's location.

 

I wrote 2 OnChange Client Scripts as shown below:

1st-

Name:setImpactedUser as of caller

Table: Incident

Type:onChange

Field name: Caller

 

 

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

    if (isLoading || newValue == '') {

          return;

    }

    var callerUsr = g_form.getReference('caller_id');

g_form.setValue('u_impacted_user',callerUsr.name);

}

 

2nd-

Name:setLocation of Impacted User

Table: Incident

Type:onChange

Field name: Impacted User

 

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

    if (isLoading || newValue == '') {

          return;

    }

    var impactedUsr = g_form.getReference('u_impacted_user');

g_form.setValue('u_location',impactedUsr.location);

}

 

 

But this is not working as desired because I am not good at applying conditions like here what we want is that- If any INC record(s) have the Impacted User (u_impacted_user) field Blank then only the Impacted User field should get auto-populate with the name as same of Caller (caller_id). Further, it should auto-populate the Location (u_location) field with the Impacted User's location. 

 

Thanks in advance, please help to achieve this.

 

 

1 ACCEPTED SOLUTION

Manmohan K
Tera Sage

@rishabh31 

 

Modify the on load script like below

function onLoad() {


    if (g_form.getValue('u_impacted_user') == '') {
        var impactuser = g_form.getReference("caller_id", loadImpactUser);

        function loadImpactUser(impactuser) {

            g_form.setValue("u_impacted_user", impactuser.sys_id);

            g_form.setValue("location", impactuser.location.sys_id);

        }
    }

}

 

View solution in original post

8 REPLIES 8

Manmohan K
Tera Sage

@rishabh31 

 

Modify the on load script like below

function onLoad() {


    if (g_form.getValue('u_impacted_user') == '') {
        var impactuser = g_form.getReference("caller_id", loadImpactUser);

        function loadImpactUser(impactuser) {

            g_form.setValue("u_impacted_user", impactuser.sys_id);

            g_form.setValue("location", impactuser.location.sys_id);

        }
    }

}

 

@Manmohan K  This is working as expected but not sure why this '!' mark is coming at this li

function loadImpactUser(impactuser) {

Move function declaration to function body root', can we remove this ?

@rishabh31 

 

Modify code like below to remove the ! prompt

function onLoad() {



    if (g_form.getValue('u_impacted_user') == '') {
        var impactuser = g_form.getReference("caller_id", loadImpactUser);

    }

    function loadImpactUser(impactuser) {
        g_form.setValue("u_impacted_user", impactuser.sys_id);
        g_form.setValue("location", impactuser.location.sys_id);

    }

}

 

rishabh31
Mega Sage

Thank you so much @Manmohan K , its working as desired. 

 

Marking your answer correct and helpful