How do you calculate a sum from two different fields and set as embedded data in Qualtrics?

LukePLukeP Community Member Qubie ✭
edited November 2020 in Custom Code

How do you calculate a sum from two different fields and set as embedded data in Qualtrics?

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 = ["${q://QID29/ChoiceNumericEntryValue/31/1}"];

answerArrayOO5PEExt = ["${q://QID29/ChoiceNumericEntryValue/32/1}"];

answerArrayOO5REInt = ["${q://QID29/ChoiceNumericEntryValue/33/1}"];

answerArrayOO5REExt = ["${q://QID29/ChoiceNumericEntryValue/34/1}"];

answerArrayOO5INFInt = ["${q://QID29/ChoiceNumericEntryValue/35/1}"];

answerArrayOO5INFExt = ["${q://QID29/ChoiceNumericEntryValue/36/1}"];

answerArrayOO5HFInt = ["${q://QID29/ChoiceNumericEntryValue/37/1}"];

answerArrayOO5HFExt = ["${q://QID29/ChoiceNumericEntryValue/38/1}"];

//CREATE VARIABLES

var i = 1;

  var x = answerArrayOO4[i-1];

var yLEInt = answerArrayOO5LEInt[i-1];

var yLEext = answerArrayOO5LEInt[i-1];

var yFIint = answerArrayOO5LEInt[i-1];

var yFIext = answerArrayOO5LEInt[i-1];

var yPEint = answerArrayOO5LEInt[i-1];

var yPEext = answerArrayOO5LEInt[i-1];

var yREint = answerArrayOO5LEInt[i-1];

var yREext = answerArrayOO5LEInt[i-1];

var yINFint = answerArrayOO5LEInt[i-1];

var yINFext = answerArrayOO5LEInt[i-1];

var yHFint = answerArrayOO5LEInt[i-1];

var yHFext = answerArrayOO5LEInt[i-1];


//SUM TOGETHER

var sumLEint = [(x/100)*yLEint];

var sumLEext = [(x/100)*yLEext];

var sumFIint = [(x/100)*yFIint];

var sumFIext = [(x/100)*yFIext];

var sumPEint = [(x/100)*yPEint];

var sumPEext = [(x/100)*yPEext];

var sumREint = [(x/100)*yREint];

var sumREext = [(x/100)*yREext];

var sumINFint = [(x/100)*yINFint];

var sumINFext = [(x/100)*yINFext];

var sumHFint = [(x/100)*yHFint];

var sumHFext = [(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*/


});

Answers

Sign In to Comment