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

Ankur Bawiskar
Tera Patron
Tera Patron

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

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

Hi Ankur,

I believe it should work with quotes also. 

 

Thanks,

Mohit Kaushik

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

Yes it works with quotes as well.

Regards

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

Mohit Kaushik
Mega Sage
Mega Sage

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

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)