- 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-03-2020 12:34 AM
Hi Mohit,
In the form i have 5 states and in particular state i have given a unique ui button i.e..
in state -1 : submit button,
in state -2 : approve button ,....
By clicking on submit button,state changes to 2.
So when this change occurs, I required the u_string_2 field to change the value.
- 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-03-2020 02:55 AM
Thank you for the information Mohit. I will try to change the code accordingly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2020 04:36 AM
Sure Krishna,
Let me know if you need any help in code.
Thanks,
Mohit Kaushik
Mohit Kaushik
ServiceNow MVP (2023-2025)