onchange client script, oldValue is not coming in the alert.

Community Alums
Not applicable

Hi All,

Its a Yes/No catalog variable. I have written a onchange client script. The requirement is something like "if the particular field changes from Yes to No, do the logic". 

So i wrote like below, if (oldValue is Yes & newValue is No) but its not working. Even the alert in the 5th line is empty for oldValue, newValue alert is coming.

 

find_real_file.png

1 ACCEPTED SOLUTION

Hi,

then clear it only when the value selected is None

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

    if(newValue == 'Yes'){
        g_form.setValue('hiddenVariable', 'Yes');
    }
    else if(newValue == ''){
        g_form.clearValue('hiddenVariable');
    }

    if(g_form.getValue('hiddenVariable') == 'Yes' && newValue == 'No'){
        // your logic
    }

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

18 REPLIES 18

That the validation is not working, is as expected and correct. newValue does not work this way. That's why I was asking for more details on your case. What you are describing will not work with using oldValue. oldValue is the original value, this is not the value from before an onchange, its the original value from the form load.

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Community Alums
Not applicable

Thanks Mark, then do you have any idea how can i implement this? show alert only when value changes from Yes to No.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Where is this not working?

Did you set UI Type - ALL for your client script

Does your variable has Include None as well?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable

In the serviceportal Ankur.

Yes UI type is All and my variable include none as well.

OldValue is getting empty, newValue is coming in the alert properly.

Screenshot 1 : 

find_real_file.png

Screenshot 2 : 

find_real_file.png

From the pics you can see that Old is getting empty.

Any input to my questions?

Like I asked, please describe your case more. I suspect this is about entering a new Catalog Item. As I mentioned, then using your approach is not correct. If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn