We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

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

ahefaz1
Mega Sage

@ServNowDev ,

 

This is on change of what field on the form?

 

Thanks,

i have it set to none because i have 6 fields that need to be onchange. 

Can you paste a screenshot of the client script form.

because you still have if newValue == '' return added to the script.

 

Thanks,

Thomas42_0-1704986923521.png