ATF - How to Set Variable Value for Choice Variable with Choices Added via Client Script

symonflores_23
Tera Guru

I'm trying to set variable values for a Select Box variable. However, the choices are added via a catalog client script. How can I select a value for this variable and proceed with the submission if the choice does not appear in this step?

symonflores_23_0-1751013747072.png

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@symonflores_23 

you must have created choices in question_choice table for that variable?

If not then it won't come in your Step when you set the variable value.

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

View solution in original post

5 REPLIES 5

Abbas_5
Tera Sage
Tera Sage

Hello @symonflores_23,

To select a value for a Select Box variable, even if the choice isn't initially present, you can use g_form.setValue() within an onSubmit catalog client script. First, ensure the script has access to the intended value, and then use the script to set the variable's value before submission. If the choice needs to be dynamically added to the select box, you can manipulate the question_choice table within a client script; however, this is generally discouraged and may require adjustments to read ACLs. 
 
Here's a breakdown of the process and considerations:
1. Understanding the Problem:
  • You have a Select Box variable on a catalog item.
  • The choices for this variable are being added dynamically via a Catalog Client Script.
  • You want to pre-select a specific value in this Select Box, even if that value isn't currently present in the options.
  • You also need to ensure the form submission proceeds, even if the desired value isn't initially available in the dropdown. 
     
2. Solution using g_form.setValue():
  • Identify the variable: Make sure you have the correct name of the Select Box variable you want to manipulate.
  • Locate the value: You'll need to know the exact value (not just the display value) that you want to pre-select.
  • Create an onSubmit Catalog Client Script: This script will run before the form is submitted.
  • Use g_form.setValue()Inside the script, use g_form.setValue('your_variable_name', 'your_value'); to set the desired value. If the value is a string, make sure it is enclosed in single quotes.
  • Example: 
     
JavaScript
    function onSubmit() {        g_form.setValue('your_variable_name', 'desired_value');        return true; // Allow submission    }
  • Return trueEnsure the script returns true to allow the form to submit.
     
3. Considerations for Dynamic Choices:
  • Adding choices to the question_choice table:
    If the intended value is not always present, you might consider dynamically adding it to the question_choice table via a client script. However, this is generally not recommended.
  • ACLs:
    Adding or modifying choices in the question_choice table may require adjustments to read access control lists (ACLs) to ensure the script has the necessary permissions. This can impact other parts of the system.
  • Alternatives:
    If you frequently need this functionality, consider using a Reference qualifier or a Reference variable instead of dynamically adding choices to the question_choice table. These options provide better control and maintainability. 
     
4. Example Scenario:
Let's say you have a Select Box variable called os_version and you want to pre-select "Windows 10" in it.
  1. Find the value: You need to know the actual value associated with "Windows 10" in the question_choice table. Let's assume it's windows_10.
  2. Create the script:
JavaScript
    function onSubmit() {        g_form.setValue('os_version', 'windows_10');        return true;    }
In Summary:
Use g_form.setValue() in an onSubmit Catalog Client Script to select a specific value for your Select Box variable, even if the choice isn't initially available. If you need to dynamically add choices, explore alternative options like Reference qualifiers or Reference variables for better system management. 
 
If this is helpful, please hit the thumbs up button and accept the correct solution by referring to this solution in future it will helpful to them.
 
Thanks & Regards,
Abbas Shaik

@Abbas_5 

Seems you got confused on the question asked.

It's about ATF and not about scripting.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@symonflores_23 

you must have created choices in question_choice table for that variable?

If not then it won't come in your Step when you set the variable value.

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

@symonflores_23 

Hope you are doing good.

Did my reply answer your question?

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