Looking for JS to append a string to an Embedded Data variable | XM Community
Solved

Looking for JS to append a string to an Embedded Data variable

  • 9 March 2021
  • 8 replies
  • 175 views

Userlevel 3
Badge

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!

icon

Best answer by pogi 9 March 2021, 14:51

View original

8 replies

Userlevel 3
Badge +8

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);

Userlevel 3
Badge

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.

Userlevel 3
Badge +8

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.

Userlevel 7
Badge +27

\\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.

Userlevel 3
Badge

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.

Userlevel 3
Badge

You need a 


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

Userlevel 3
Badge +8

That should work in HTML.

Userlevel 3
Badge

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