- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2020 11:15 PM
Hi,
I am working on a requirement , wherein when the state value changes, the value in one of the custom created String field(u_string_2) should change. I tried using Onchange Script but it's not working. Also in 'isLoading' function the alert is working, but in the next steps alert is not popping up, I am mentioning the code below.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading )
{
alert(newValue);
return;
}
if(newValue == '2'){
g_form.setValue('u_string_2', 'ABC');
alert(newValue);
}
else if(newValue == 3){
g_form.setValue('u_string_2','LMN');
alert(newValue);
}
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2020 01:17 AM
Hi Krishna,
Then you can not achieve this by using onChange client script. Because onChange client script will only work when you are changing some field before submitting the record. As you are changing the state at the time of submitting the record with the help of some UI action so that is updating the record. onChange client script works before updating the record not at time of updating the record.
I will suggest you can direct use the UI action only through which you are updating the state value. You can write one line like this:
current.u_string_2 ='ABC';
and then update the record.
Or if you want to change the value of your string field only when the state changes after clicking on some button. Then you can use After Business rule which will work on update.
And the conditions will be like.
When state changes:
script will be like
if(current.state==2)
current.u_string_2='ABC';
else if(current.state==3)
current.u_string_2='LMN';
current.update();
This rule will help you in changing the field value.
Hope this will help you in resolving the query.
Thanks,
Mohit Kaushik
Mohit Kaushik
ServiceNow MVP (2023-2025)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2020 11:20 PM
Hi,
Is the state value integer? if yes then no need of quotes while comparing
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading)
{
alert(newValue);
return;
}
if(newValue == 2){
g_form.setValue('u_string_2', 'ABC');
alert(newValue);
}
else if(newValue == 3){
g_form.setValue('u_string_2','LMN');
alert(newValue);
}
}
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2020 11:26 PM
Hi Ankur,
I believe it should work with quotes also.
Thanks,
Mohit Kaushik
Mohit Kaushik
ServiceNow MVP (2023-2025)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2020 11:46 PM
Yes it works with quotes as well.
Regards
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2020 11:24 PM
Hey Krishna,
It should work as per your script.
when the form got loaded, after that did you try to to change the state field?
How are you changing the state field, after opening the form or you are trying your script to work once the form is loaded and field is already changed.
I mean to say, onChange client script works when you do some modification in the form for the value which you are using in your script.
It will be good if you can post some screenshots while playing with your script.
Please mark this answer correct if it resolved the query and mark it helpful too if it helped you at all.
Thanks,
Mohit Kaushik
Mohit Kaushik
ServiceNow MVP (2023-2025)