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

Multiple Variables in one OnChange Client script

wefw
Tera Contributor

Hi, we have 3 to 4 variables with yes/no type
when any variable is changing with Yes/No then one pop up message should need to appear(Same pop-up for all the variables).  we have written for Single variable and its working fine with below script.

 

function onChange(control, oldValue, newValue, isLoading) {
 if (isLoading || newValue == '') {
      return;
   }
 
   
        if (newValue !== oldValue) {
         var message = "This decision could not be changed later. Are you sure you want to proceed with this decision?";
         var confirmation = confirm(message);

           
          if (!confirmation) {
           g_form.setValue('u_cer_pr', oldValue);
            }
        }

 

I tried with multiple Variables, but it's not working. First is it possible to add multiple variables in one onChange client script? if so can anyone help me in this.


function onChange(control, oldValue, newValue, isLoading) {
 if (isLoading || newValue == '') {
      return;
   }

        var fieldsToTrack = ['u_cer_pr', 'final_purchase_decision', 'deployment_required'];
 
    if (fieldsToTrack.indexOf(control.name) !== -1 && newValue !== oldValue) {
        var message = "This decision could not be changed later. Are you sure you want to proceed with this decision?";
        var confirmation = confirm(message);
 
        if (!confirmation) {
            g_form.setValue(control.name, oldValue);
        }
    }
    }

Thanks


3 ACCEPTED SOLUTIONS

Mark Manders
Mega Patron

No. A Client script is triggered on an action on the form (you want it to trigger 'onChange'. You can only select one field that changes, so you will need to create an onChange client script for each variable. You can check within a script if certain values are set, but that script will still need to be triggered onChange of one field.

 

What isn't working when you create it for all 4 fields, because if it works on one, it should work on the others as well.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

View solution in original post

Ayushi12
Mega Sage

Hi @wefw ,

The onChange client script triggers when a specific field value changes. It cannot monitor changes for 3–4 variables in a single script. It is recommended to create separate onChange client scripts for each variable. If any variable value changes, the corresponding script will display the message.

If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful."

Thanks!

View solution in original post

Juhi Poddar
Kilo Patron
Kilo Patron

Hello @wefw 

  • A single onChange client script will not work for all 3-4 variables, as the onChange client script is specifically triggered when the value of a particular field changes.
  • To address this, you need to create a separate onChange client script for each variable to ensure the desired functionality works correctly.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

View solution in original post

6 REPLIES 6

Mark Manders
Mega Patron

No. A Client script is triggered on an action on the form (you want it to trigger 'onChange'. You can only select one field that changes, so you will need to create an onChange client script for each variable. You can check within a script if certain values are set, but that script will still need to be triggered onChange of one field.

 

What isn't working when you create it for all 4 fields, because if it works on one, it should work on the others as well.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Ayushi12
Mega Sage

Hi @wefw ,

The onChange client script triggers when a specific field value changes. It cannot monitor changes for 3–4 variables in a single script. It is recommended to create separate onChange client scripts for each variable. If any variable value changes, the corresponding script will display the message.

If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful."

Thanks!

Juhi Poddar
Kilo Patron
Kilo Patron

Hello @wefw 

  • A single onChange client script will not work for all 3-4 variables, as the onChange client script is specifically triggered when the value of a particular field changes.
  • To address this, you need to create a separate onChange client script for each variable to ensure the desired functionality works correctly.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Community Alums
Not applicable

With onChange - no, with onLoad however, you can achieve that by using events. I'm not saying this is best practice, but answering to your question.

Try using something like that with onLoad script :

function onLoad() {
    var control = g_form.getControl('description');
var control_two = g_form.getControl('short_description');

    Event.observe(control, 'change', function() {
        g_form.addInfoMessage('You have changed the description field.');
    });
    Event.observe(control, 'change', function() {
        g_form.addInfoMessage('You have changed the short description field.');
    });

}