Assigning values to embedded data with Javascript -- wacky results | Experience Community
Skip to main content
Solved

Assigning values to embedded data with Javascript -- wacky results

  • February 17, 2026
  • 3 replies
  • 52 views

Forum|alt.badge.img+1

I’m trying to do some calculations in the addOnLoad function, and then assign the result to embedded data variables which were defined in Survey Flow at the top of the survey.  When I look at the calculations (printing to console.log and then viewing with Inspect) they are correct.  But for some reason the assignment to embedded data variables seems to be going wrong.  

In the Javascript code I have an array with 7 members, all initialized to zero, and for two randomly selected members I flip the value from 0 to 1.  

There are 7 embedded data variables, and at the end of my Javascript code each member of the array is assigned to one of these.  When I look at the final array using Inspect, it’s fine.  But then the embedded data variables are wrong.  In each case 2 of the 7 are indeed set to 1, but it’s rarely the correct 2.  But sometimes it is correct!  Maybe random chance??

Here’s my code:

Qualtrics.SurveyEngine.addOnload(function()
{
/* this function will randomly select two purchasing decision blocks for display */
/* depending on the answers to previous questions H11 and WH3 */

/* first determine if respondent has central AC (H11) or an electric water heater (WH3) */
/*var HasCentralAC = "${e://Field/HasCentralAC}";
var HasElectricWaterHeater = "${e://Field/HasElectricWaterHeater}";*/

var HasCentralAC = 1;
var HasElectricWaterHeater = 1;

console.log("HasCentralAC: ");
console.log(HasCentralAC); /* 1 = has central AC */
console.log("HasElectricWaterHeater: ");
console.log(HasElectricWaterHeater); /* 1 = has electric water heater */

/* initialize array of blocks */

if (HasCentralAC == 1 && HasElectricWaterHeater == 1)
var PDBlocks = [1, 2, 3, 4, 5, 6, 7]; /* includes all blocks */
else if (HasCentralAC == 1 && HasElectricWaterHeater != 1)
var PDBlocks = [1, 2, 3, 4, 6, 7]; /* excludes water heater block */
else if (HasCentralAC != 1 && HasElectricWaterHeater == 1)
var PDBlocks = [1, 3, 4, 5, 6, 7]; /* excludes central AC block */
else if (HasCentralAC != 1 && HasElectricWaterHeater != 1)
var PDBlocks = [1, 3, 4, 6, 7]; /* excludes both */

console.log("PDBlocks: ");
console.log(PDBlocks);

/* what is the maximum number of blocks in the array? */

var GroupPop = PDBlocks.length;

console.log("GroupPop: ");
console.log(GroupPop);

/* randomly select two elements of the array, but make sure they are different from each other */

do {
var ChosenArrayIndex1 = Math.floor(Math.random() * GroupPop);
var ChosenArrayIndex2 = Math.floor(Math.random() * GroupPop);
}
while (ChosenArrayIndex1==ChosenArrayIndex2);

console.log("Randomly selected array indexes (remember index starts with 0): ");
console.log(ChosenArrayIndex1);
console.log(ChosenArrayIndex2);

console.log("Which blocks were randomly chosen?");
console.log(PDBlocks[ChosenArrayIndex1]);
console.log(PDBlocks[ChosenArrayIndex2]);

/* set flags for the selected blocks */
/* first set all flags to 0 (false) for all 7 blocks */
/* then set flag to 1 (true) for the two randomly selected blocks */

var ShowBlockFlags = [0, 0, 0, 0, 0, 0, 0]

ShowBlockFlags[PDBlocks[ChosenArrayIndex1]-1] = 1;
ShowBlockFlags[PDBlocks[ChosenArrayIndex2]-1] = 1;

console.log("ShowBlockFlags: ");
console.log(ShowBlockFlags);

/* now set embedded data variables according to flags */

Qualtrics.SurveyEngine.setEmbeddedData('ShowBlock1', ShowBlockFlags[0]);
Qualtrics.SurveyEngine.setEmbeddedData('ShowBlock2', ShowBlockFlags[1]);
Qualtrics.SurveyEngine.setEmbeddedData('ShowBlock3', ShowBlockFlags[2]);
Qualtrics.SurveyEngine.setEmbeddedData('ShowBlock4', ShowBlockFlags[3]);
Qualtrics.SurveyEngine.setEmbeddedData('ShowBlock5', ShowBlockFlags[4]);
Qualtrics.SurveyEngine.setEmbeddedData('ShowBlock6', ShowBlockFlags[5]);
Qualtrics.SurveyEngine.setEmbeddedData('ShowBlock7', ShowBlockFlags[6]);


});

 

Best answer by vgayraud

Are you running your tests in preview mode? If so your code will run twice with different random selections. Maybe you’re not looking at the right console log lines.

3 replies

gPandey_715
Level 4 ●●●●
Forum|alt.badge.img+9
  • Level 4 ●●●●
  • February 19, 2026

Hi ​@DanBausch ,

This usually happens due to the timing of when Qualtrics saves Embedded Data.

Even though setEmbeddedData() runs in addOnLoad and the console shows correct values, Qualtrics only saves Embedded Data when the page is submitted. On the same page, Qualtrics can overwrite those values during page submit, which leads to random or shifted results.

The fix is to set the Embedded Data on one page and add a page break so the values are committed before they’re used later in the survey.


vgayraud
QPN Level 7 ●●●●●●●
Forum|alt.badge.img+62
  • QPN Level 7 ●●●●●●●
  • Answer
  • February 19, 2026

Are you running your tests in preview mode? If so your code will run twice with different random selections. Maybe you’re not looking at the right console log lines.


Forum|alt.badge.img+1
  • Author
  • February 19, 2026

Thank you so much vgayraud!  That was the answer.  Once I ran it outside of preview, it worked fine.  Whew!