How do you calculate a sum from two different fields and set as embedded data in Qualtrics? | XM Community
Skip to main content

Hey,
I have a question in which its rows will display based on whether a the sum total of the responses from two other questions adds up to less than 10 billion. Due to the fact that I have a complex Table of Contents, it will not allow be to add an embedded data field just before the block of this last question, which leads me to believe I need to use JavaScript in order to do this.
I have tried the following, but it doesn't work, as it doesn't update the embedded data field to the calculation.
Any help would be greatly appreciated - I have zero JavaScript knowledge!
Thanks,
Luke
Qualtrics.SurveyEngine.addOnload(function()
{
//Set new Active Module, hide buttons on empty page and automatically go to next page.
//This is because the embedded data works after the ToC creation, so needs a loading question to update it.
//REQUIRES a page break after this question.

//GATHER ALL VALUES INTO ONE PLACE
answerArrayOO4 = ["${q://QID4/ChoiceNumericEntryValue/1}"];
answerArrayOO5LEInt = "${q://QID29/ChoiceNumericEntryValue/1/1}"];
answerArrayOO5LEExt = "${q://QID29/ChoiceNumericEntryValue/28/1}"];
answerArrayOO5FIInt = ="${q://QID29/ChoiceNumericEntryValue/29/1}"];
answerArrayOO5FIExt =  "${q://QID29/ChoiceNumericEntryValue/30/1}"];
answerArrayOO5PEInt = t"${q://QID29/ChoiceNumericEntryValue/31/1}"];
answerArrayOO5PEExt = x"${q://QID29/ChoiceNumericEntryValue/32/1}"];
answerArrayOO5REInt = I"${q://QID29/ChoiceNumericEntryValue/33/1}"];
answerArrayOO5REExt = E"${q://QID29/ChoiceNumericEntryValue/34/1}"];
answerArrayOO5INFInt = N"${q://QID29/ChoiceNumericEntryValue/35/1}"];
answerArrayOO5INFExt = I"${q://QID29/ChoiceNumericEntryValue/36/1}"];
answerArrayOO5HFInt = O"${q://QID29/ChoiceNumericEntryValue/37/1}"];
answerArrayOO5HFExt = O"${q://QID29/ChoiceNumericEntryValue/38/1}"];

//CREATE VARIABLES
var i = 1;
  var x = answerArrayOO4ai-1];
var yLEInt = answerArrayOO5LEIntri-1];
var yLEext = answerArrayOO5LEIntri-1];
var yFIint = answerArrayOO5LEIntri-1];
var yFIext = answerArrayOO5LEIntri-1];
var yPEint = answerArrayOO5LEIntri-1];
var yPEext = answerArrayOO5LEIntri-1];
var yREint = answerArrayOO5LEIntri-1];
var yREext = answerArrayOO5LEIntri-1];
var yINFint = answerArrayOO5LEIntri-1];
var yINFext = answerArrayOO5LEIntri-1];
var yHFint = answerArrayOO5LEIntri-1];
var yHFext = answerArrayOO5LEIntri-1];

//SUM TOGETHER
var sumLEint = a(x/100)*yLEint];
var sumLEext = a(x/100)*yLEext];
var sumFIint = a(x/100)*yFIint];
var sumFIext = a(x/100)*yFIext];
var sumPEint = a(x/100)*yPEint];
var sumPEext = a(x/100)*yPEext];
var sumREint = a(x/100)*yREint];
var sumREext = a(x/100)*yREext];
var sumINFint = r(x/100)*yINFint];
var sumINFext = r(x/100)*yINFext];
var sumHFint = a(x/100)*yHFint];
var sumHFext = a(x/100)*yHFext];

//LE
Qualtrics.SurveyEngine.setEmbeddedData("Int_LE_USD", sumLEint);
Qualtrics.SurveyEngine.setEmbeddedData("Ext_LE_USD", sumLEext);
//FI
Qualtrics.SurveyEngine.setEmbeddedData("Int_FI_USD", sumFIint);
Qualtrics.SurveyEngine.setEmbeddedData("Ext_FI_USD", sumFIext);
//PE
Qualtrics.SurveyEngine.setEmbeddedData("Int_PE_USD", sumPEint);
Qualtrics.SurveyEngine.setEmbeddedData("Ext_PE_USD", sumPEext);
//RE
Qualtrics.SurveyEngine.setEmbeddedData("Int_RE_USD", sumREint);
Qualtrics.SurveyEngine.setEmbeddedData("Ext_RE_USD", sumREext);
//INF
Qualtrics.SurveyEngine.setEmbeddedData("Int_INF_USD", sumINFint);
Qualtrics.SurveyEngine.setEmbeddedData("Ext_INF_USD", sumINFext);
//HF
Qualtrics.SurveyEngine.setEmbeddedData("Int_HF_USD", sumHFint);
Qualtrics.SurveyEngine.setEmbeddedData("Ext_HFI_USD", sumHFext);

jQuery("#PreviousButton, #NextButton").css("visibility","hidden");
this.clickNextButton()

});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});

I'm not sure what type of question you are using, but

ChoiceNumericEntryValue 
refers to the Choice Number in something like a multiple choice question.
What you are looking for is
ChoiceTextEntry


Scoring Your Survey might help. Check out the video below.


https://basecamp.qualtrics.com/rc-customizing-your-project/216021


LukeP ,
You are trying to do calculations on strings. You need to convert the string to numbers. It isn't clear to me why you are using arrays since everything is single values.


Leave a Reply