Looking for JS to append a string to an Embedded Data variable | XM Community
Skip to main content

I have an Embedded Data variable ${e://Field/ My_multiline_text }, which will store a mutiline text

I need a JS code which appends a string and put it on the new line, depending on survey answers. Something like the following:
var p2Text = "${q://QID1/ChoiceGroup/SelectedChoices}";
if (p2Text =="Yes") {
 Qualtrics.SurveyEngine.Append_EmbeddedData("My_multiline_text", "Hello baby" );
}

p2Text = "${q://QID2/ChoiceGroup/SelectedChoices}";
if (p2Text =="Yes") {
 Qualtrics.SurveyEngine.Append_EmbeddedData("My_multiline_text", "I love you baby" );
}
p2Text = "${q://QID3/ChoiceGroup/SelectedChoices}";
if (p2Text =="Yes") {
 Qualtrics.SurveyEngine.Append_EmbeddedData("My_multiline_text", "I miss you baby" );
}

So at the end of the survey, ${e://Field/ My_multiline_text } would contain a multiline text:
Hello baby
I love you baby
I miss you baby

I am going to pipe in ${e://Field/ My_multiline_text } into my email trigger message body.
Thanks!

You can concatenate Embedded Data simply by adding
new_text = ${e://Filed/My_multiline_text} + "\\n" + "Hello baby";
Qualtrics.SurveyEngine.setEmbeddedData( 'My_multiline_text', new_text);


It works. The only problem with such an approach is that the JS code to check the answer ${q://QID1/ChoiceGroup/SelectedChoices}" may not be placed into the question itself, but has to be placed into subsequent question.
BTW, I had to put double quotes for "${e://Filed/My_multiline_text}"
var new_text = "${e://Filed/My_multiline_text}" + "Hello baby";
and, + "\\n"  is not working. All the strings are appended to the same line. But it's not a big deal for me.


Yes, it is a string so the quotes. Good catch.
Can you explain what you mean about the SelectedChoices? If I am understanding what you mean. You will have to separate the JS to capture the question validation into the next page so you can either add a new block for the next question or add a page break. The other option is to use JS to do the validation for you on the question. I prefer to use Qualtrics for my validation and just create a page break.
I'm not an expert in JS. I assume the "\\n" is just being treated as a string rather than a new line. I'm sure google would help or maybe some of our wizards (TomG or ahmedA ) can quickly answer.


\\n is a newline character. It might or might not be what you want. If you want to show the text in html, \\n is meaningless and won't have any impact on the display. You need a


tag instead. If you want a new line in a textbox value or in your data then \\n is appropriate.


Can you explain what you mean about the SelectedChoices? 

I am checking answers to Question-1 , Question-2, Question-3, etc. and depending on these answers I do or do not append certain information to the embedded data variable ${e://Field/ My_multiline_text }. At the end, the embedded data is piped into the message body of the email trigger (triggered email) . So ${e://Field/ My_multiline_text } will depend on- and reflect the answers to the questions, but will not be just the collection of those answers.
So it would be nice if the JS that captures the answer to Question-1 could be placed into Question-1 and the JS that captures the answer to Question-2 could be placed into Question-2 and so on.


You need a 


tag instead.
So, what will be the JS code for that?
var new_text = "${e://Filed/My_multiline_text}" + "
" + "Hello baby";


That should work in HTML.


In my survey,  most of the questions are binary. For the binary questions, if the answer is affirmative that is positive, it should be  reported and recorded either into ${e://Filed/My_multiline_text} or piped-in directly into the triggered email message.
If the answer is negative, it should be simply discarded and not recorded.
Technically, the negative answer could  be represented by an empty string or non-visible character.
Is there a way to configure binary questions in such a way that, for the negative answer, variable "${q://QIDxxx/ChoiceGroup/SelectedChoices}" will return an empty string?
It will greatly simplify my job because I will not have to add the code to discard negative answer for each such question individually.
I would also be able to pipe-in ${q://QIDxxx/ChoiceGroup/SelectedChoices} directly into the triggered email message, without the need of the embedded data ${e://Filed/My_multiline_text}
For example,
Capture.PNGIf the respondent selects "I am a Physics major", then ${q://QID1/ChoiceGroup/SelectedChoices}" should return "I am a Physics major". 
If the respondent selects "I am not a Physics major", then ${q://QID1/ChoiceGroup/SelectedChoices}" should return an empty string.
The returned string will be piped-in into the triggered email message.


Leave a Reply