Set Value and Clear Value not working as expected

krishnareddy
Tera Contributor

Hello ServiceNow Experts.

 

We are having choice variable on the catalog item, whenever I select a choice value from the choice variable,  it should set a value on single line text based on our selection, if this value is not matched with the selected choice then  it should clear the single line variable.

 

To achieve this,  I have written  on change client script, but some times the value is not populating and some times it's not clearing the value, please provide your inputs.

 

I have attached my script screen shot, please help.

6 REPLIES 6

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @krishnareddy ,

Use below :-

 

if(Bonus ==“Bus & Govt Aviation Sales”){ //Considering you have bonus as string field value
g_form.setValue(“earning_code”,’39’);
}else
{
if(Bonus !=“Bus & Govt Aviation Sales”){
g_form.clearValue(“earning_code”);
}

 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Mohith Devatte
Tera Sage
Tera Sage

Hello @krishnareddy can you try this script 

 

you misplaced the braces , i tried to correct them .PlEASE TRY THE BELOW ONE AND LET ME KNOW

function onChange(control, oldValue, newValue, isLoading){
if (isLoading || newValue == "") {
return;
}
var Bonus= g_form. getValue ("bonus_category") ;
if(Bonus =="Bus & Govt Aviation Sales") 
{
g_form.setValue("earning_code",39);
}
if(Bonus !="Bus & Govt Aviation Sales")
{
g_form. clearValue ('earning_code") ;
}
}

Hope this helps 

Mark my answer correct if this helps you 

Thanks

Hi @Mohith Devatte 

 

I have tried same, but value is not setting, please find the attachment for the same.

 

 

 

@krishnareddyi can see you did not change your code with the code given in above answer . In your code the brace which you opened in 7th line is ending in 11th line which is not correct it should end below 8th line and also put an alert of bonus .See if the alert is throwing correct value that you are trying to compare with and also in second if you have used !== which is the wrong syntax you need to use !=

please paste the below code and try 

i have made the corrections 

 

 

function onChange(control, oldValue, newValue, isLoading){
if (isLoading || newValue == "") {
return;
}
var Bonus= g_form. getValue ("bonus_category") ;
alert(bonus);
if(Bonus =="Bus & Govt Aviation Sales") 
{
g_form.setValue("earning_code",39);
}
if(Bonus !="Bus & Govt Aviation Sales")
{
g_form. clearValue ('earning_code") ;
}
}

 

 

 

 

 

Hope this helps 

Mark my answer correct if this helps you 

Thanks