Setting calculated embedded data based on previous answers | XM Community
Skip to main content
Solved

Setting calculated embedded data based on previous answers


Forum|alt.badge.img+3

Hello, I’m new to JS. I have been trying this for a long time but had no luck. Any advice would be very welcome.

I have a question in a survey asking for employment income. Respondents need to select the way they want to report their salary then enter a numeric value:

I wanted to calculate annual salary based on response to monthly/bi-weekly/hourly rates and show the calculated annual salary to respondents on the next page and confirm it’s the right amount. If it’s not correct, respondents will be able to go back and correct their answer. My JS on the salary question is like this:

And my survey flow is like this:

On the verification page, the calculated field doesn’t show up, and there is no error message either. What went wrong?

Thank you!

 

Best answer by vgayraud

Hi again, didn’t see at first that you needed to be able to go back and the math formula between 2 blocks will prevent that.

With javascript, you’ll need the new functions and ED naming since you’re using the simple layout.

Your script will look like this (in your input question).

Qualtrics.SurveyEngine.addOnPageSubmit(function () {
    var Monthly = this.getTextValue(2);
    var Biweekly = this.getTextValue(3);
    var Hourly = this.getTextValue(4);
    var HoursPerWeek = "${q://QID90/ChoiceTextEntryValue}";
    var CalculatedAnnualSalary = parseInt(Monthly * 12 + Biweekly * 26 + Hourly * HoursPerWeek * 52);

    Qualtrics.SurveyEngine.setJSEmbeddedData('CalculatedAnnualSalary', CalculatedAnnualSalary);
});

And your embedded data field must be named like that in the survey flow:

__js_CalculatedAnnualSalary

And piped in like that in your validation question:

${e://Field/__js_CalculatedAnnualSalary} 

 

View original

12 replies

vgayraud
QPN Level 5 ●●●●●
Forum|alt.badge.img+48
  • QPN Level 5 ●●●●●
  • 360 replies
  • February 26, 2025

Hi,

You don’t need javascript for that, you can use math operations in embedded data fields.

Yours would look something like this:

$e{ q://QID92/ChoiceTextEntryValue/4 * 12 + q://QID92/ChoiceTextEntryValue/5 * 26 + q://QID92/ChoiceTextEntryValue/6 * q://QID90/ChoiceTextEntryValue * 52 }

See this support section for more.


vgayraud
QPN Level 5 ●●●●●
Forum|alt.badge.img+48
  • QPN Level 5 ●●●●●
  • 360 replies
  • Answer
  • February 26, 2025

Hi again, didn’t see at first that you needed to be able to go back and the math formula between 2 blocks will prevent that.

With javascript, you’ll need the new functions and ED naming since you’re using the simple layout.

Your script will look like this (in your input question).

Qualtrics.SurveyEngine.addOnPageSubmit(function () {
    var Monthly = this.getTextValue(2);
    var Biweekly = this.getTextValue(3);
    var Hourly = this.getTextValue(4);
    var HoursPerWeek = "${q://QID90/ChoiceTextEntryValue}";
    var CalculatedAnnualSalary = parseInt(Monthly * 12 + Biweekly * 26 + Hourly * HoursPerWeek * 52);

    Qualtrics.SurveyEngine.setJSEmbeddedData('CalculatedAnnualSalary', CalculatedAnnualSalary);
});

And your embedded data field must be named like that in the survey flow:

__js_CalculatedAnnualSalary

And piped in like that in your validation question:

${e://Field/__js_CalculatedAnnualSalary} 

 


Forum|alt.badge.img+3
  • Author
  • Level 1 ●
  • 11 replies
  • February 26, 2025

Thank you Vincent. I tried this but see a 0 in the validation page. Did I miss something?

JS in the input question:

My survey flow:

Piped in text:

Validation page:

 


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5919 replies
  • February 26, 2025

In ​@vgayraud’s code, the variables need to be converted to numbers before they are used in the calculation instead of after (i.e., var CalculatedAnnualSalary = parseInt(...). For example:

var Monthly = Number(this.getTextValue(2));

 


Forum|alt.badge.img+3
  • Author
  • Level 1 ●
  • 11 replies
  • February 26, 2025

Thank you Tom. The textboxes have been set as numeric values.

I have figured out the issue: the variable index wasn’t 2, 3, or 4😓

 

Thanks all!


Forum|alt.badge.img+3
  • Author
  • Level 1 ●
  • 11 replies
  • February 26, 2025

Actually one last thing to ask, is there a way to add comma separator in the piped variable CalculatedAnnualSalary? Now the number shows like this:

 


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5919 replies
  • February 26, 2025


Add toLocaleString to CalculatedAnnualSalary:

var CalculatedAnnualSalary = parseInt(Monthly * 12 + Biweekly * 26 + Hourly * HoursPerWeek * 52).toLocaleString("en-CA", {style:"currency",currency:"CAD"});

 


Forum|alt.badge.img+3
  • Author
  • Level 1 ●
  • 11 replies
  • February 26, 2025

Hi Tom,

When I use this JS:

I have input displayed correctly:

When I used the new code:

I don’t have anything displayed:

What did I miss here?


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5919 replies
  • February 26, 2025

Oops, I forgot some quotes. I updated the code in the post above.


Forum|alt.badge.img+3
  • Author
  • Level 1 ●
  • 11 replies
  • February 26, 2025

It works! Thank you!

Actually, this is really one last thing...how can I remove the decimals?

 


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5919 replies
  • February 26, 2025

Add maximumFractionDigits:0 to the toLocaleString options object.


Forum|alt.badge.img+3
  • Author
  • Level 1 ●
  • 11 replies
  • February 26, 2025

Wow, thank you for your time and expertise! 


Leave a Reply