Question T14631
Visible to All Users

Survery runner : defaultValueExpression is not working

created a year ago

HI Team,
We have came across a scenario where we had a defaultValueExpression which is sum the columns in dynamic matrix as below

Code
"defaultValueExpression": "sumInArray({question2},'Column 2')"

but the sum calculation is only working for the first time means when no data is patched to survey runner. When data is patched to survey runner if we tried to update the matrix colum's the sum calculation is not reflecting.

I have sample project for reference / to replicate the Scenario

Json for reference

Code
{   "logoPosition": "right",   "pages": [    {     "name": "page1",     "elements": [      {       "type": "text",       "name": "question1",       "title": "sum",       "defaultValueExpression": "sumInArray({question2},'Column 2')"      },      {       "type": "matrixdynamic",       "name": "question2",       "title": "matrix",       "columns": [        {         "name": "Column 1"        },        {         "name": "Column 2",         "cellType": "text"        },        {         "name": "Column 3"        }       ],       "choices": [        1,        2,        3,        4,        5       ]      }     ]    }   ]  }

Answers approved by surveyjs Support

created a year ago

Hi Santosh,
I ran your demo and noticed that the sum field correctly displays the sum within the Column 1 fields:
Clipboard-File-1.png

Please clarify what is the issue. Additionally, I may recommend that you use the survey.mergeData function to specify question values from code.

JavaScript
survey.mergeData({ question1: 0, //0 question2: [ { "Column 2": "5" } ] });

The survey.data erases default question values, while the mergeData function preserves default values. For the difference between the two ways of setting question answers, please refer to the following guide: Multiple Question Values.

View Demo

Let me know if you have additional questions.

    Show previous comments (9)

      Hello Andrew, Any update on this issue?

      AT AT
      Andrew Telnov a year ago

        Hello Santosh,
        The PR was totally re-written and the PR is waiting to be reviewed.
        I will come back to you later.

        Thank you,
        Andrew
        SrurveyJS Team

        AT AT
        Andrew Telnov a year ago

          Hello Santosh,
          Why don't you use expression question type?
          { "type": "expression", "name": "question1", "title": "sum", "expression": "sumInArray({question2},'Column 2')" },
          It will fix your issue.

          Thank you,
          Andrew
          SurveyJS Team