Request for JavaScript Help in Qualtrics – Updating and Calculating Embedded Data Scenario | XM Community
Skip to main content
Solved

Request for JavaScript Help in Qualtrics – Updating and Calculating Embedded Data Scenario

  • February 27, 2025
  • 2 replies
  • 29 views

Forum|alt.badge.img+3

Your urgent assistance would be so much appreciated, I tried so many things and didn’t get it done :(

I am designing a survey where participants provide updated information about authorized signatories in their business. Here’s how the flow works:

  1. First Question:

    • The participant is shown a number (let's call it current_number which already comes as am embedded data from the panel) representing the number of authorized signatories we have on record.
    • They choose between:
      • "Yes, this number is still correct."
      • "No, the correct number is different." (If they choose this, they enter a new number in a text box - **with force response**, how do I force response on the text box AND make validation of number only would be great to know.)
    • The new value (if provided) should be saved as an embedded data field called actual_number.
  2. Second Question (Matrix or Side-by-Side Table - Please advise the type of question, I tried both with no success):

    • The participant sees a list of signatories from our records and is asked to indicate whether each is still valid or not (so 2 options, Yes, No)
    • If they mark a name as not valid, we count how many are no longer relevant and store this count (sum of No) as an embedded data field called not_relevant.
  3. Logic Needed:

    • Using JavaScript, I need to calculate how many new signatories the participant needs to provide in the next step.
    • Formula: \text{New Rows Needed} = \text{actual_number} - (\text{current_number} - \text{not_relevant})
    • This value will determine how many rows appear in the next question using display logic and filters.

Example to Illustrate the Need:

  • We have 5 signatories on file (current_number = 5).
  • The participant answers:
    • "No, this number is incorrect, the correct number is 4." (actual_number = 4)
  • In the next question, they mark 2 signatories as still valid, and 3 as not valid.
    • not_relevant = 3
  • We calculate:
    • 4−(5−3)=24 - (5 - 3) = 24−(5−3)=2 → The survey should now ask for 2 new signatories.

Additional Cases:

  • If the participant confirms that all 5 are still correct, no new input is needed.
  • If they confirm 5 but say 1 name changed, we need to request 1 new name.

Please if you can help me:

Recommend the question type for the second question where they mark relevant or not and guide me how to structure the JavaScript in a way that it listen to the choices when moving to the next question. I can also put a hidden next question that only calculates this if needed (BTW users can come back to the survey and they have back button, so if I need to prevent something let me know…). In general the JS should:

  1. Save not_relevant as embedded data.
  2. Perform the calculation for New Rows Needed dynamically.
  3. Use this value to control the display logic of the next question.

THANK YOU!!! The community here rocks!

@TomG - you always have great ideas on such issues. I tried so many options :(

Best answer by Chee Heng_SZ

First Question:

  • The participant is shown a number (let's call it current_number which already comes as am embedded data from the panel) representing the number of authorized signatories we have on record.
  • They choose between:
    • "Yes, this number is still correct."
    • "No, the correct number is different." (If they choose this, they enter a new number in a text box - **with force response**, how do I force response on the text box AND make validation of number only would be great to know.)
  • The new value (if provided) should be saved as an embedded data field called actual_number.

You can use MCQ with text entry on the “No” or make a text entry question to appear through display logic. Then, set custom validation and the input to match regex ^\d{1,2}$ (for number for up to 2 digits). Then, pipe this input into embedded data actual_number.

 

Second Question (Matrix or Side-by-Side Table - Please advise the type of question, I tried both with no success):

  • The participant sees a list of signatories from our records and is asked to indicate whether each is still valid or not (so 2 options, Yes, No)
  • If they mark a name as not valid, we count how many are no longer relevant and store this count (sum of No) as an embedded data field called not_relevant.

Not sure about JavaScript.

But if your current_number isn’t high and you don’t mind having multiple branch logic (which prevents back button), you can set branch logic after second question (matrix question) with condition to check for “No” count equals to number. Then, set embedded data not_relevant= current_number - number. (Remember to include the math operation for embedded data) Each branch checks for 1 number. 

Logic Needed:

  • Using JavaScript, I need to calculate how many new signatories the participant needs to provide in the next step.
  • Formula: \text{New Rows Needed} = \text{actual_number} - (\text{current_number} - \text{not_relevant})
  • This value will determine how many rows appear in the next question using display logic and filters.

Continuing after the part above, you can set an embedded data newrow= actual_number - current number + not_relevant. (Remember to include the math operation for embedded data).

Then, use this newrow value to display the number of questions based on the condition of newrow>=number.

  • If the participant confirms that all 5 are still correct, no new input is needed.
  • If they confirm 5 but say 1 name changed, we need to request 1 new name.

Based on your “Logic Needed” section, the formula should have accounted for additional cases.

Case 1: newrow= 5 - (5 - 0) = 5 - 5 + 0 = 0

Case 2: newrow= 5 - (5 - 1) = 5 - 5 + 1 = 1.

 

A tedious alternative that has a similar result but less optimal, in the event that you can’t figure out how to do it with JavaScript.

View original

2 replies

Forum|alt.badge.img+16

First Question:

  • The participant is shown a number (let's call it current_number which already comes as am embedded data from the panel) representing the number of authorized signatories we have on record.
  • They choose between:
    • "Yes, this number is still correct."
    • "No, the correct number is different." (If they choose this, they enter a new number in a text box - **with force response**, how do I force response on the text box AND make validation of number only would be great to know.)
  • The new value (if provided) should be saved as an embedded data field called actual_number.

You can use MCQ with text entry on the “No” or make a text entry question to appear through display logic. Then, set custom validation and the input to match regex ^\d{1,2}$ (for number for up to 2 digits). Then, pipe this input into embedded data actual_number.

 

Second Question (Matrix or Side-by-Side Table - Please advise the type of question, I tried both with no success):

  • The participant sees a list of signatories from our records and is asked to indicate whether each is still valid or not (so 2 options, Yes, No)
  • If they mark a name as not valid, we count how many are no longer relevant and store this count (sum of No) as an embedded data field called not_relevant.

Not sure about JavaScript.

But if your current_number isn’t high and you don’t mind having multiple branch logic (which prevents back button), you can set branch logic after second question (matrix question) with condition to check for “No” count equals to number. Then, set embedded data not_relevant= current_number - number. (Remember to include the math operation for embedded data) Each branch checks for 1 number. 

Logic Needed:

  • Using JavaScript, I need to calculate how many new signatories the participant needs to provide in the next step.
  • Formula: \text{New Rows Needed} = \text{actual_number} - (\text{current_number} - \text{not_relevant})
  • This value will determine how many rows appear in the next question using display logic and filters.

Continuing after the part above, you can set an embedded data newrow= actual_number - current number + not_relevant. (Remember to include the math operation for embedded data).

Then, use this newrow value to display the number of questions based on the condition of newrow>=number.

  • If the participant confirms that all 5 are still correct, no new input is needed.
  • If they confirm 5 but say 1 name changed, we need to request 1 new name.

Based on your “Logic Needed” section, the formula should have accounted for additional cases.

Case 1: newrow= 5 - (5 - 0) = 5 - 5 + 0 = 0

Case 2: newrow= 5 - (5 - 1) = 5 - 5 + 1 = 1.

 

A tedious alternative that has a similar result but less optimal, in the event that you can’t figure out how to do it with JavaScript.


Forum|alt.badge.img+3
  • Author
  • Level 2 ●●
  • 29 replies
  • March 5, 2025

Thank you!! I got this done and I appreciate it!


Leave a Reply