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.

Auto Populate Values for field type Template Value

U_verma
Tera Expert

HI All,

I am trying to populate values  in "template value"  type of field using onChange script for target field(whenever we change target) .

when create a record and select a value of target table it populates value for if condition i.e 'active=true' but then when I try to change the target table to satisfy else condition it always give 'active=true' seems like what ever 1st value is set it does not replace with new value when changing the target table

if(g_form.isNewRecord()){
var targetTable = g_form.getValue('target_table');
var query = g_form.getValue('template_values');
if(targetTable =='sc_task' ){
query= 'active=true'; //template values are stored and set as an encoded query string
}else{
query= 'short_description=' ;
}g_form.setValue('template_values',query);
}
 
can you please help me with the code, thanks
3 REPLIES 3

Tushar
Kilo Sage
Kilo Sage

Hello,

 

The issue you are facing is that when changing the target table after initially setting the value for the template_values field, the new value is not being applied.

code you provided only sets the value for the template_values field if it's a new record.

To update the value when the target table changes, you can modify your script as follows:

 

var targetTable = g_form.getValue('target_table'); var query = '';

if (targetTable == 'sc_task') { query = 'active=true'; }

else { query = 'short_description='; }

g_form.setValue('template_values', query);

 

I removed the g_form.isNewRecord() condition so that the value is set regardless of whether it's a new record or an existing one.

HI Tushar, I have tried this earlier but its the same. 

rugvedmanoorkar
ServiceNow Employee
ServiceNow Employee

Were you able to find any solution ?