Client script that adds all yes value and populates score on change
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2024 07:02 AM - edited 01-11-2024 07:12 AM
I have 6 yes/no choice fields that i need to add all the yes values when they are changed to yes to the score field, these values are also dot walked , but client script will not execute here is the code and form view.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var questionWeight = {
'table1.question1': 2,
'table1.question2': 2,
'table1.question3': 3,
'table1.question4': 3
};
var totalScore = 0;
for (var fieldName in questionWeight) {
var choiceValue = g_form.getValue(fieldName);
if (choiceValue === 'yes') {
totalScore += parseFloat(questionWeight[fieldName]);
}
}
g_form.setValue('survey_score', totalScore);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2024 07:31 AM
Your client script will not execute, because you have not selected a field in the "Field name".
You need to select a field. Once the field you select changes, you client script code will execute.
Thanks,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2024 07:40 AM
these fields are dot walked so i will not be able to access those 6 variables to choose in the field drop down also i have 6 fields i would need to be onchange is this possible
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2024 07:53 AM
I would try using doing it via UI policies. 6 of them.
UI policies allow you to access dot walked fields. They execute on change.
and you can use the script section to constantly calculate/update the score.
6 UI policies, 6 Run Scripts where you do the same calculation.
Not sure of your design needs, but can be done for sure. Messy code and approach.
You will need to have your above client script code in every ui policy script field. Lot of repetition as well.
Please accept the solution or mark helpful if this helped.
Thanks,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2024 12:43 PM
what if we changed it to an on submit?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2024 12:58 PM
That should work. I initially thought you wanted the score to update dynamically as the user modifies the fields.
OnSubmit, you gather values from the fields, calculate the score, and display and add it right before saving the form.
If the user is redirected to the same form, they will see the updated score upon reload.
Achievable, but not sure the kind of experience you want for the users, because they will never see the score change when they update the fields.
My suggestion is to do it server side in a business rule, rather than going with an onSubmit script. Same effect. Submit saves the form, BR runs and calculates/adds the score to the field. The form reopens and shows the score to the user.
Please accept the solution or mark helpful if this helped.
Thanks,