Constant Sum Total Must Be Less than or Equal to Embedded Data Value | XM Community
Question

Constant Sum Total Must Be Less than or Equal to Embedded Data Value

  • 16 May 2019
  • 4 replies
  • 595 views

Userlevel 1
Badge
I need to validate the total of a constant sum question with three rows against the value of an embedded data value. If the sum of the constant question exceeds the value, then an error message should appear and prompt the respondent to revise their answers.

Essentially, ${q://QID16/TotalSum} should be less than or equal to ${e://Field/S5b}.

However, I can't call the total sum in drop downs in custom validation and I am unsure how to move forward from there.

The equation I am looking at is as follows, but I do not know how to communicate this into Qualtrics.
${q://QID18/ChoiceNumericEntryValue/1} + ${q://QID18/ChoiceNumericEntryValue/2} + ${q://QID18/ChoiceNumericEntryValue/3} <= ${e://Field/S5b}, otherwise error.

4 replies

Userlevel 3
Badge +3
[Edited] Previous Answer not fullproof.
Hello @RandomKraft,

You can do this using custom JS.
Userlevel 3
Badge +4
I realise this was asked a couple of months ago and this answer is likely too late. Hopefully it helps somebody else. It is frustrating that the Constant Sum question does not accept anything except totalling to a specific numeric entry. It would be very useful if you could reference an embedded data field or even a piped text value. It would be even better if you could also specify that the constant sum total must =, <, >, <=, >=.

Anyway, this can be achieved by using custom coding on the question i.e. adding JavaScript. You can pull the value of an embedded data field in JavaScript and you can also find the fields that make up the constant sum question and sum them together (Use Inspect on the question to find the correct reference). You can then compare the two values. The script below should work for you. Essentially you are hiding the next button to replace with your own button that runs the check function when clicked. If a valid condition is met then the page is submitted, if not then a custom error message is displayed.

Assuming you already have an embedded data variable set, then choose the option to add JavaScript on the Constant Sum Question. Use the following code as a guide (I have commented the bits that will be specific to your requirements and will need to be adjusted).

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var warnings = [];

function hideEl(element) {
if($(element)) $(element).hide();
}

function createNextButton() {
var nextButtonHTML = '<input id="CustomNextButton" class="NextButton Button" title="→" type="button" name="NextButton" value="→" aria-label="Next">';
jQuery('#Buttons').append(nextButtonHTML);
}

hideEl.defer('NextButton');
createNextButton();

});

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

jQuery('#CustomNextButton').on('click', function() {

/* This is where you reference the Embedded Data field */
var xED_Field = Number(Qualtrics.SurveyEngine.getEmbeddedData('xED_Field'));
/* This is where you sum the values entered on the current page - Inspect the HTML to find the correct Element ID */
var xtotal =Number(document.getElementById('QR~QID37~1').value) + Number(document.getElementById('QR~QID37~2').value) + Number(document.getElementById('QR~QID37~3').value) + Number(document.getElementById('QR~QID37~4').value);

/* This is where you set your condition to be accepted */
if (xtotal <= xED_Field) {
jQuery('#NextButton').click();
jQuery('#CustomNextButton').hide();
} else {
/* This is where you set your error message text */
var errorMsg = "Responses must sum to " + xED_Field + ".";
var x = document.createElement("DIV");
var t = document.createTextNode(errorMsg);
x.className = "custom-warning";
x.appendChild(t);
document.getElementById('Questions').parentElement.appendChild(x);
jQuery('.custom-warning').css("background", "pink");
jQuery('.custom-warning').css("color", "red");
jQuery('.custom-warning').css("font-size", "12px");

}
});

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
});
Userlevel 2
Badge +1

@GrayMatterThinking, I attempted to apply your solution to call values from a previous question and it is not working. Might you please review my adjusted script to see if you can identify the issue? 

 

Note: I only adjusted the input for var xED_Field and var xtotal. I have not adjusted the error messages. The condition should be as you’ve already established, which is to accept if xED_Field <= xtotal. 

 

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var warnings = [];

function hideEl(element) {
if($(element)) $(element).hide();
}

function createNextButton() {
var nextButtonHTML = '<input id="CustomNextButton" class="NextButton Button" title="→" type="button" name="NextButton" value="→" aria-label="Next">';
jQuery('#Buttons').append(nextButtonHTML);
}

hideEl.defer('NextButton');
createNextButton();

});

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

jQuery('#CustomNextButton').on('click', function() {

/* This is where you reference the Embedded Data field */
var xED_Field = Number(document.getElementById('QR~QID4').value);
/* This is where you sum the values entered on the current page - Inspect the HTML to find the correct Element ID */
var xtotal =Number(document.getElementById('QID12~1_Total').value);

/* This is where you set your condition to be accepted */
if (xtotal <= xED_Field) {
jQuery('#NextButton').click();
jQuery('#CustomNextButton').hide();
} else {
/* This is where you set your error message text */
var errorMsg = "Responses must sum to " + xED_Field + ".";
var x = document.createElement("DIV");
var t = document.createTextNode(errorMsg);
x.className = "custom-warning";
x.appendChild(t);
document.getElementById('Questions').parentElement.appendChild(x);
jQuery('.custom-warning').css("background", "pink");
jQuery('.custom-warning').css("color", "red");
jQuery('.custom-warning').css("font-size", "12px");

}
});

});

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

Leave a Reply