How to make One Variable Change When Another is Altered in a Catalog Item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-09-2022 05:54 PM
I have created two catalog items, one for movies and one for video games. I have the movie and game choices connected to tables I've uploaded that include data like the prices and ratings for each title. Now, comes the hard part, having the "Rating" field on the catalog item automatically update when I change the game or movie to sync up with the rating found on their table entries.
Can anyone share a simple to edit and understand client script template for that purpose with me? Preferably something that is set up to simply reference either the record number or the title as a variable so that I don't have to type in 25 individual entries?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-09-2022 07:25 PM
Hi Danielle,
What you will need is a callable script include function to get the rating value based on the record sys_id (That will be received as a parameter through an AJAX call).
Then, you have to create an OnChange script on your dropdown list and make the AJAX request.
Something like this:
var ga = new GlideAjax('<script>');
ga.addParam('sysparm_name', '<function name>');
ga.addParam('sysparm_sys_id', newValue);
ga.getXML(function(response){
var result = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('rating', result);
});
Let me know if you need more details!
Alex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-10-2022 12:17 AM
Thank you very much! So, would I place that code in the title select variable, the rating variable, a catalog UI Policy, or would I create a new Catalog Client Script for it? Also, for <script> and <function name>, would I replace those with names from my table's XML version, or the catalog entry's variables?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-10-2022 07:00 AM
You will need to create the OnChange script on your Movie/Video game dropdown.
That way, anytime you change it, the AJAX call will be made to the server and the rating will be updated with g_form.setValue in the call back function.
I am not sure how familiar you are with AJAX, but the call back function is the one defined in gs.getXML(). It is a function that will be called once the result has been returned by the server. The line "var result = response.responseXML.documentElement.getAttribute("answer");" is what contains the answer that has been returned by the Script Include (Server code)
Now for name and function name, that will simply depend of what you have in your script include. First you need to make sure it's client callable (So that AJAX can use it).
Here's what it could look like (I haven't ran it though so it might need adjustments).
The API Name is actually the <script> you will need to use.
The <function name> is actually the one defined on line 3 (retrieveRating) but you can name it how you want.
I hope it helps!
Alex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-10-2022 08:44 AM
Thanks for explaining! So, once I have this AJAX piece configured, then I:
1. go to my catalog item
2. go to the Client Script tab
3. create an OnChange script using the template from your first post connected to the item selection variable
4 plug in my function and API
Am I understanding that correctly?