Auto Populate location field on case form with contacts location

Luke James
Tera Contributor

Hi All, 

 

I have a requirement to auto populate on the case form to auto populate the location field with the location of the contact who is selected in the contact field. 

 

Does anyone know how to do this? Or have a script for this? 

 

Kind Regards, 

 

Luke

5 REPLIES 5

Community Alums
Not applicable

Hi @Luke James ,

You can achieve this using catalog client script. But for that in users profile (sys_user table) contact & accounts fields should be available.

Use g_user.userID() to get the logged in users sys_id.

Or you can use new platform capability: https://www.servicenow.com/community/developer-articles/auto-populate-a-variable-based-on-a-referenc...

 

Luke James
Tera Contributor

Hi @Community Alums , 

 

Thanks for your response. This would need to be on the actual case form though when an agent is filling out a case. Not through a catalog request itself. 

 

Any idea on how to do that? 

 

Kind Regards, 

 

Luke

@Luke James You need to create an onChange client script on the case form for Contact field. Inside the script you can use 

var contact = g_form.getReference('contact',contactCallback);

function contactCallback(contact){

g_form.setValue('location',contact.location);

}

Hope this helps.

 

Sandeep Rajput
Tera Patron
Tera Patron

@Luke James Here is how you should configure the onChange client script on the case form.

 

Screenshot 2024-04-03 at 10.20.33 PM.png

 

Here is the script.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    //Type appropriate comment here, and begin script below
    var contact = g_form.getReference('contact', contactCallback);

    function contactCallback(contact) {
        g_form.setValue('location', contact.location);

    }
}

 

Hope this helps.