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.

OnChange client script for auto-populating field values

Saloni Suthar
Giga Sage
Giga Sage

Hello all,

We have an onChange client script setup - whenever a caller is filled in, the location will be auto-populated on the incident form. But when the impacted user (not mandatory field) is filled in, it overrides the location of the caller and replaces the value with the impacted user's location. However, for eg, if tech by mistake uses the impacted user field and has a location for 'impacted user' on the incident form and then decides just to go with the caller field, the caller's location does not auto-populate and leaves the location as 'blank'. Is there any way to re-populate the value of the caller's field after the impacted user is used and then left blank? I am also pasting the scripts for reference. Any lead would be appreciated. 

(BP) Set Location to User :

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading)
return;

if (newValue == '' || newValue == null) {
g_form.setValue('u_location', '');
return;
}

if (!g_form.hasField('u_location'))
return;
var caller = g_form.getReference('u_impacted_user', setLocation);

 

function setLocation(caller) {
if (caller)
g_form.setValue('u_location', caller.location);
}
}

Impacted User Location :

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == '') {
g_form.setValue('u_location', '');
return;
}
if (!g_form.getControl('u_location')) {
return;
}
var caller = g_form.getReference('u_impacted_user', setContactNumber);
}

function setContactNumber(caller) {
if (caller) {
g_form.setValue('u_location', caller.location);
}
}


If my response helped you, please click on "Accept as solution" and mark it as helpful.
- Saloni
1 ACCEPTED SOLUTION

You can implement via onChange client script. Write below code in impacted user change client script

if (newValue == '') {
var caller = g_form.getReference('caller_id', setContactNumber);
return;
}
if (!g_form.getControl('u_location')) {
return;
}
var caller = g_form.getReference('u_impacted_user', setContactNumber);
function setContactNumber(caller) {
if (caller) {
g_form.setValue('u_location', caller.location);
}
}

View solution in original post

9 REPLIES 9

Upender Kumar
Mega Sage

Use below script on impacted user onChange client script.

 

if (newValue == '') {
var caller = g_form.getReference('caller_id', setContactNumber);
return;
}
if (!g_form.getControl('u_location')) {
return;
}
var caller = g_form.getReference('u_impacted_user', setContactNumber);
}

JohnnySnow
Kilo Sage

Hi Saloni,

To achieve this you can use similar code in both the scripts only change will be

 

var caller = g_form.getReference('opened_by', setLocation); //replace the field with your fieldname


var caller = g_form.getReference('caller_id', setLocation);

 

In my case it would be like this...

 

FOR ONCHANGE OF CALLER FIELD
-------------------------------------------------------------
function onChange(control, oldValue, newValue, isLoading) {
   if ((isLoading && !g_form.isNewRecord()) || (g_form.isLiveUpdating && g_form.isLiveUpdating()))
      return;

   if (newValue == '' || newValue == null) {
      g_form.setValue('location', '');
      return;
   }
   if (!g_form.hasField('location'))
      return;
   var caller = g_form.getReference('caller_id', setLocation);
}

function setLocation(caller) {
   if (caller)
       g_form.setValue('location', caller.location);
}


-----------------------------------------------------------------

FOR ONCHANGE OF OPENED BY FIELD

-----------------------------------------------------------------
function onChange(control, oldValue, newValue, isLoading) {
   if ((isLoading && !g_form.isNewRecord()) || (g_form.isLiveUpdating && g_form.isLiveUpdating()))
      return;

   if (newValue == '' || newValue == null) {
      g_form.setValue('location', '');
      return;
   }
   if (!g_form.hasField('location'))
      return;
   var caller = g_form.getReference('opened_by', setLocation);
}

function setLocation(caller) {
   if (caller)
       g_form.setValue('location', caller.location);
}

 

Please try and let me know if you face any issues.

 

Thanks

Thanks
Johnny

Please mark this response as correct or helpful if it assisted you with your question.

Hi Johnny,

 

Thank you for the script!! It still does the same thing!! When the caller field is used, the location auto-populates as well as the same with the impacted user. But when I clear the impacted user, it does not revert back to the caller's location and keep the location empty.

 


If my response helped you, please click on "Accept as solution" and mark it as helpful.
- Saloni

You can implement via onChange client script. Write below code in impacted user change client script

if (newValue == '') {
var caller = g_form.getReference('caller_id', setContactNumber);
return;
}
if (!g_form.getControl('u_location')) {
return;
}
var caller = g_form.getReference('u_impacted_user', setContactNumber);
function setContactNumber(caller) {
if (caller) {
g_form.setValue('u_location', caller.location);
}
}

Hi Saloni, Misunderstood the requirement, you can achieve it like this.

Refer the code under //--------- 

function onChange(control, oldValue, newValue, isLoading) {
    if ((isLoading && !g_form.isNewRecord()) || (g_form.isLiveUpdating && g_form.isLiveUpdating()))
        return;



//--------------------------------------------------------------
    var caller;
    if (newValue == '' && g_form.getValue('opened_by') != '') {
        caller = g_form.getReference('opened_by', setLocation);

        return;
    }
//--------------------------------------------------------------


    if (!g_form.hasField('location'))
        return;
    caller = g_form.getReference('caller_id', setLocation);
}

function setLocation(caller) {
    if (caller)
        g_form.setValue('location', caller.location);
}

 

Similarly do for other script as well.

 

Please mark helpful/correct if it worked.

 

Thanks

Thanks
Johnny

Please mark this response as correct or helpful if it assisted you with your question.