Set Value and Clear Value not working as expected
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2022 12:46 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2022 12:55 AM - edited 11-30-2022 12:55 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2022 12:56 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2022 01:16 AM
I have tried same, but value is not setting, please find the attachment for the same.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2022 01:22 AM
@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