Client script that adds all yes value and populates score on change

ServNowDev
Tera Guru

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);

 

 

Thomas42_0-1704985318362.png

 

9 REPLIES 9

@ServNowDev ,

 

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,

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

@ServNowDev ,

 

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.

 

ahefaz1_0-1704988125585.png

 

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.

 

ahefaz1_1-1704988234810.png

 

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,

what if we changed it to an on submit?

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,