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.

Client Script to Auto Populate Single Text fields base on a select box value

Peter Williams
Kilo Sage

Good Day,

i have a form that i can auto populate fields bases on a selection from a user.

It will fill in the location address if they choose one location to another.

I am using a On Change Client Script to do this

but now i need to be able to add in an if statement into the mix.

If its a pickup then sender address will be empty and the Rec. will get filled in automatically

if its a delivery then reverse

 

This is the on change i have

PeterWilliams_0-1680720680134.png

 

this is the  my script.

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var fileNo = g_form.getReference('location_sender', myFunc);
var type = g_form.getValue('type');

function myFunc(fileNo){
if(type =='Cueillette / Pick up' ){
g_form.setValue('adresse_address', fileNo.street);
g_form.setValue('code_postal_zip_code', fileNo.zip);
g_form.setValue('ville_city', fileNo.city);
g_form.setValue('province_state', fileNo.state);
g_form.setValue('pays_country', fileNo.country);
g_form.setValue('t_l_phone_phone', fileNo.phone);
g_form.clearValue('adresse_address_2');
g_form.clearValue('code_postal_zip_code_2');
g_form.clearValue('ville_city_2');
g_form.clearValue('province_state_2');
g_form.clearValue('pays_country_2');
g_form.clearValue('t_l_phone_phone_2');
}
else if(type =='Livraison / Delivery' ){
g_form.setValue('adresse_address_2', fileNo.street);
g_form.setValue('code_postal_zip_code_2', fileNo.zip);
g_form.setValue('ville_city_2', fileNo.city);
g_form.setValue('province_state_2', fileNo.state);
g_form.setValue('pays_country_2', fileNo.country);
g_form.setValue('t_l_phone_phone_2', fileNo.phone);
g_form.clearValue('adresse_address');
g_form.clearValue('code_postal_zip_code');
g_form.clearValue('ville_city');
g_form.clearValue('province_state');
g_form.clearValue('pays_country');
g_form.clearValue('t_l_phone_phone');
}
}

}

 

i can get it to work without the if statement like this for the sender but i cant make it work with the if statements

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var fileNo = g_form.getReference('location_sender', myFunc);


function myFunc(fileNo){

g_form.setValue('adresse_address', fileNo.street);
g_form.setValue('code_postal_zip_code', fileNo.zip);
g_form.setValue('ville_city', fileNo.city);
g_form.setValue('province_state', fileNo.state);
g_form.setValue('pays_country', fileNo.country);
g_form.setValue('t_l_phone_phone', fileNo.phone);

1 ACCEPTED SOLUTION

i was able to figure it out, i had to change the Variable Name from location_sender to type and its working now

View solution in original post

3 REPLIES 3

Ahmmed Ali
Giga Sage
Giga Sage

Hello @Peter Williams 

 

I think issue with backend value of the type variable. Updated script below (highlighted changes in bold):

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var type = g_form.getValue('type');

alert(type)

var fileNo = g_form.getReference('location_sender', myFunc);


function myFunc(fileNo){
if(type =='Cueillette / Pick up' ){
g_form.setValue('adresse_address', fileNo.street);
g_form.setValue('code_postal_zip_code', fileNo.zip);
g_form.setValue('ville_city', fileNo.city);
g_form.setValue('province_state', fileNo.state);
g_form.setValue('pays_country', fileNo.country);
g_form.setValue('t_l_phone_phone', fileNo.phone);
g_form.clearValue('adresse_address_2');
g_form.clearValue('code_postal_zip_code_2');
g_form.clearValue('ville_city_2');
g_form.clearValue('province_state_2');
g_form.clearValue('pays_country_2');
g_form.clearValue('t_l_phone_phone_2');
}
else if(type =='Livraison / Delivery' ){
g_form.setValue('adresse_address_2', fileNo.street);
g_form.setValue('code_postal_zip_code_2', fileNo.zip);
g_form.setValue('ville_city_2', fileNo.city);
g_form.setValue('province_state_2', fileNo.state);
g_form.setValue('pays_country_2', fileNo.country);
g_form.setValue('t_l_phone_phone_2', fileNo.phone);
g_form.clearValue('adresse_address');
g_form.clearValue('code_postal_zip_code');
g_form.clearValue('ville_city');
g_form.clearValue('province_state');
g_form.clearValue('pays_country');
g_form.clearValue('t_l_phone_phone');
}
}

}

 

Are you sure values of the question choice of type variable are 'Cueillette / Pick up' and 'Livraison / Delivery'?

 

Let me know what are you getting in alert after select both the values in type field.

 

Thank you,

Ali

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

AnveshKumar M
Tera Sage
Tera Sage

Hi @Peter Williams ,

Are the type values you are using "Cueillette / Pick up" and "Livraison / Delivery" correct? I mean are these two things Labels of the Choices or Value of the choices? You should use value of the choice to compare.

 

Thanks,

Anvesh

Thanks,
Anvesh

i was able to figure it out, i had to change the Variable Name from location_sender to type and its working now