The CreatorCon Call for Content is officially open! Get started here.

How to change a field value when state changes using Onchange script.

M Krishna Chai1
Giga Contributor

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);

}

}

1 ACCEPTED SOLUTION

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

 

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

View solution in original post

8 REPLIES 8

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.

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

 

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

Thank you for the information Mohit. I will try to change the code accordingly.

Sure Krishna,

Let me know if you need any help in code.

 

Thanks,

Mohit Kaushik

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)