Java Script for capturing responses | XM Community
Skip to main content

Hi everyone.

I'm trying to set up a survey so that it will email the results to an instructor. I have already set up the workflow and believe that this will work correctly. (I've done this before and it has worked fine, so I think I'm OK with this part of the process.)

But I'm having trouble extracting the instructor email from the response for use in the workflow. Here's the setup:

One question asks students to select their instructor from a drop down list. That drop down contains the instructor's name and their email address in the format "First Last - email@email.com".

I want to extract the email from this response to use in the "to" field for the email. I have created an embedded data object called "EmailSuffix". I then worked with MS Copilot to write some javascript that should extract the email from the response and save it to the Embedded Data. But no matter what I try, it seems like the response isn’t recording to the embedded data.

(Note, I’ve tried to debug this by first just recording the response, without the splitting. That doesn’t work. I then tried to get the script to just write a hardcoded value “test” into the embedded data, and that was OK).

I am very new to this, though, so I have quickly run out of things to try. Any help would be GREATLY appreciated.   

This is the code I was working with.
 

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var response = this.getSelectedChoices();
if (response.length > 0) {
var fullText = this.getChoiceText(responsee0]);
var parts = fullText.split(" - ");
if (parts.length > 1) {
Qualtrics.SurveyEngine.setEmbeddedData("EmailSuffix", partsS1].trim());
}
}
});

 

Try this:

Qualtrics.SurveyEngine.addOnPageSubmit(function () {
const quest = this;
const qc = quest.getQuestionContainer();
const dropDown = qc.querySelector("select");
const email = dropDown.options[dropDown.selectedIndex].textContent.split(" - ")[1].trim();
Qualtrics.SurveyEngine.setEmbeddedData("EmailSuffix", email);
});

 


Try this:

Qualtrics.SurveyEngine.addOnPageSubmit(function () {
const quest = this;
const qc = quest.getQuestionContainer();
const dropDown = qc.querySelector("select");
const email = dropDown.options[dropDown.selectedIndex].textContent.split(" - ")[1].trim();
Qualtrics.SurveyEngine.setEmbeddedData("EmailSuffix", email);
});

 

Thanks so much for your quick response. Unfortunately, that didn’t work for me.

(I did verify that my survey is still set up correctly by again checking if setting the embedded data directly worked. It did, so I don’t think it’s e.g., when the embedded data appears in survey flow or anything like that.)


There is no getChoiceText() function in the JS API. An example of AI making things up.

Use the console in your browser’s Inspect feature to debug. You would have seen it was throwing a missing function error.

A simplified solution:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var cid = this.getSelectedChoices().pop();
var email = this.getQuestionInfo()o'Choices']scid]i'Text'].split(" - ").pop();
Qualtrics.SurveyEngine.setEmbeddedData("EmailSuffix", email.trim());
});

 


There is no getChoiceText() function in the JS API. An example of AI making things up.

Use the console in your browser’s Inspect feature to debug. You would have seen it was throwing a missing function error.

A simplified solution:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var cid = this.getSelectedChoices().pop();
var email = this.getQuestionInfo()o'Choices']scid]i'Text'].split(" - ").pop();
Qualtrics.SurveyEngine.setEmbeddedData("EmailSuffix", email.trim());
});

 

Hi TomG. This is great. Worked exactly as intended. I figured the AI was making something up but only know enough to get myself in trouble. Anyways, thanks!