E-mail Trigger Formatting: Replacing Comma with Line Break | XM Community
Skip to main content
I have an e-mail trigger in which part of the piped text are multiple selections that have been chosen. In the e-mail, the selections appear straight across the screen, separated by commas. This is unattractive, and I would like for each of the selections to appear on a new line. How can I do this?
Replace the commas with breaks and save to an embedded variable. Then pipe the embedded variable into your email trigger.

```

Qualtrics.SurveyEngine.setEmbeddedData("myVar", "${q://QID1/ChoiceGroup/SelectedChoices}".replace(/, /g, "<br>"));

```
@TomG

I copied and pasted your code in the JS for the specific question (unless I was supposed to do it in the e-mail trigger itself).

I set the variable (myVar) in the Survey Flow; then referenced it in the e-mail trigger: I got nothing.
> @rhonnied1 said:

> @TomG

> I copied and pasted your code in the JS for the specific question (unless I was supposed to do it in the e-mail trigger itself).

> I set the variable (myVar) in the Survey Flow; then referenced it in the e-mail trigger: I got nothing.

>



The JS has to go in a question on a page after the question you are piping.
@TomG

IT WORKED!!!!!

THANK YOU SO VERY MUCH! This is a game-changer!
Tom - or anyone else following this thread - I've tried to follow along and it seems fairly simple but I must be doing something wrong. I created the embedded data variable in survey flow at the beginning of the survey. Here is what I have for the javascript code on a question long after the one I'm trying to test out formatting (I put it on a question in a completely different block.) My variable just isn't getting populated upon submission :s



`Qualtrics.SurveyEngine.addOnload(function()

{



Qualtrics.SurveyEngine.setEmbeddedData("64-1", "${q://QID2/ChoiceGroup/SelectedChoices}".replace(/, /g, "<br>"));



});



Qualtrics.SurveyEngine.addOnReady(function()

{

/*Place your JavaScript here to run when the page is fully displayed*/



});



Qualtrics.SurveyEngine.addOnUnload(function()

{

/*Place your JavaScript here to run when the page is unloaded*/



});`
@jrkinsella,



Check to make sure your pipe is correct. It is best to use "Piped text" to pipe it into the question text, then copy it to your JavaScript. If that isn't the problem, try renaming your variable with a letter. I don't know that you can't use just numbers, but most platforms don't like it, so I never do it.
> @TomG said:

> Replace the commas with breaks and save to an embedded variable. Then pipe the embedded variable into your email trigger.

> ```

> Qualtrics.SurveyEngine.setEmbeddedData("myVar", "${q://QID1/ChoiceGroup/SelectedChoices}".replace(/, /g, "<br>"));

> ```



Hi @TomG ,



Can you clarify what you mean by "replace the commas with breaks and save to an embedded variable"? What do I set the variable "myvar" in the survey flow as?



For ex, in the survey flow do I put

myVar = ${q://QID1/ChoiceGroup/Displayedchoices}".replace(/, /g, "<br>" as an embedded field?



I have a similar issue except that I would like the "displayed choices" from a MC question to appear on the email trigger without commas.
> @Thebika said:

> > @TomG said:

> > Replace the commas with breaks and save to an embedded variable. Then pipe the embedded variable into your email trigger.

> > ```

> > Qualtrics.SurveyEngine.setEmbeddedData("myVar", "${q://QID1/ChoiceGroup/SelectedChoices}".replace(/, /g, "<br>"));

> > ```

>

> Hi @TomG ,

>

> Can you clarify what you mean by "replace the commas with breaks and save to an embedded variable"? What do I set the variable "myvar" in the survey flow as?

>

> For ex, in the survey flow do I put

> myVar = ${q://QID1/ChoiceGroup/Displayedchoices}".replace(/, /g, "<br>" as an embedded field?

>

> I have a similar issue except that I would like the "displayed choices" from a MC question to appear on the email trigger without commas.



@Thebika,



You have to do it in JavaScript. Add the JavaScript to a question on a page after the MC question you want to pipe the answer to. Define "myvar" embedded data field at the top of the survey flow. No need to assign a value to it.
Hi @TomG,



Thank you for your response.



I did the following:

1) Inputted "myvar" into the survey flow as an embedded data field.

2) Pasted the javascript below in a question that is in the same block and after the MC question



!



However, it did not work.



Do I have to input a page break between the two questions?



I should also note that all my questions in the block have a display logic on them, including the MC question. Also, I did not pipe anything into the email message (which has been created as a task in "Actions")
> @Thebika said:

> Hi @TomG,

>

> Thank you for your response.

>

> I did the following:

> 1) Inputted "myvar" into the survey flow as an embedded data field.

> 2) Pasted the javascript below in a question that is in the same block and after the MC question

>

> !

>

> However, it did not work.

>

> Do I have to input a page break between the two questions?

>

> I should also note that all my questions in the block have a display logic on them, including the MC question. Also, I did not pipe anything into the email message (which has been created as a task in "Actions")

1. Is the JS line inside the addOnload or addOnReady function? If not, it needs to be.

2. Yes, you need a page break between the MC and the question with the JS.

3. You need to pipe myvar into the Email Action.
This solution looks very promising for us, but my multi-selection question is the final one of the survey. Is there an alternative solution in this case, as I don't have a subsequent question to add the javascript code?



Thanks!
> @KellyT said:

> This solution looks very promising for us, but my multi-selection question is the final one of the survey. Is there an alternative solution in this case, as I don't have a subsequent question to add the javascript code?

The easiest solution is to add a descriptive text question to the next page, attach the JS to it, and have the JS click the next button.
> @TomG said:

> > @KellyT said:

> > This solution looks very promising for us, but my multi-selection question is the final one of the survey. Is there an alternative solution in this case, as I don't have a subsequent question to add the javascript code?

> The easiest solution is to add a descriptive text question to the next page, attach the JS to it, and have the JS click the next button.

>

>



Thank you! The entire solution is working for me now.

Leave a Reply