Possible to Evaluate three Catalog Variables in one onChange Catalog Client Script?

Paul Porter
Tera Expert

I've been struggling to get a Catalog Client Script to fire, in which I'm wanting to evaluate three different Catalog Variables, that when evaluated as true will fire off an alert message. The Catalog Client Script has no Variable Name chosen in the form (which I recognize may be part of the problem). It would need to work on both the Platform and the Portal, which I know can introduce more issues, becase I may need two Catalog Client scripts

I'm fairly certain that my meager coding skills have done little for my success here. All my research has proved inconclusive and I'm at a dead end. Looking for a little help or perhaps the unfortunate conclusion that one cannot evaluate changes to three different Variable answers in one onChange Catalog Client Script.

The script for:
UI Type = All

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var var1 = g_form.getValue('travel_event_1_preferred_method'); // How do you plan to travel to your Event 1 destination?
    console.log("Preferred travel method is " + var1);
    var var2 = g_form.getValue('travel_event_1_booked_hotel_question'); // Did you book your own hotel for Event 1
    console.log("Booked own hotel answer for is " + var2);
    var var3 = g_form.getValue('travel_event_1_rent_car_question'); // Do you need a rental car for Event 1?
    console.log("Need a car rental answer for is " + var3);
    // Check the conditions for the specific answers
    if (var1 == '2' && var2 == 'No' && var3 == 'Yes') { //book own flts, not booking hotel &  renting a car
        alert('Please work with your Travel Arranger to book your hotel/car.');
    }
}

 

 

 



1 ACCEPTED SOLUTION

Vaibhav_Nikam
Tera Guru

Hi @Paul Porter ,

You can not access 3 variables in single onchange client script.

You can do it using below 3 ways:

1. you need to create separate onchange client scripts.
2.you can use the UI policy to where make the conditions as per requirement and in script you can add alert.

3. Use the onsubmit client script where you can check the values in variable and accordingly submit the form.


If my response finds helpful, please indicate its helpfulness by selecting Accept as Solution and Helpful.

Thanks,

Vaibhav Nikam

View solution in original post

5 REPLIES 5

Gangadhar Ravi
Giga Sage
Giga Sage

@Paul Porter I don't think one onChange script will work for multiple variables. you need to have 3 different onChange scripts for each of those variables or you can do one onSubmit script and do validation in onSubmit script.

 

Please mark my answer correct and helpful if this works for you.

Juhi Poddar
Kilo Patron

Hello @Paul Porter 

  • Yes, you need to specify the onChange field for the Catalog Client Script to trigger when a particular variable changes.

  • If you want the script to trigger on the change of three different variables, you'll need three separate onChange Client Scripts, even if they evaluate the same conditions. Each script should be tied to a specific variable to ensure it fires correctly when that variable changes.

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

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Paul Porter 

you will require 3 different onChange 1 for each variable

OR

you can have 1 onSubmit and show the info message to user if your only requirement is to show the message to user during submission

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Vaibhav_Nikam
Tera Guru

Hi @Paul Porter ,

You can not access 3 variables in single onchange client script.

You can do it using below 3 ways:

1. you need to create separate onchange client scripts.
2.you can use the UI policy to where make the conditions as per requirement and in script you can add alert.

3. Use the onsubmit client script where you can check the values in variable and accordingly submit the form.


If my response finds helpful, please indicate its helpfulness by selecting Accept as Solution and Helpful.

Thanks,

Vaibhav Nikam