Pulling Multiple Variables into an Email via JavaScript

Jan 11, 2023

I am building a reflection in Storyline 360 where participants answer 5 questions. Each response is attached to a text variable (i.e., Question1; Question2; etc.). At the end, I want to pull the questions and responses into an email. I've pieced my JavaScript together using other posts, but I'm stuck and have come here with 3 asks:

1: I am able to pull the responses into the email, but it all displays in one continuous paragraph. How can I add a return after each?

2: Is it possible to pull the questions in, or does this need to be typed into the JavaScript? Either way, how can this be done?

3: I would like to pull two email addresses in (the participant and their supervisor). I can currently only get one to pull in through JavaScript. Is it possible to pull both (I tried var email=player.GetVar('SupEmail','YourEmail'); but it didn't work)? Or, how can I remove that piece of script to leave the To field blank? My backup plan for this is to simply have the participant key it into the email itself.

Attached is the file I'm working with. THIS IS BARELY A DRAFT so please don't judge the look of it!

Here is the current JavaScript which runs when the participant selects Generate Email on the last slide:

var player = GetPlayer();
var email=player.GetVar('SupEmail');
var subject="Essential Soft Skills 101 Reflection";
var body_start=new Array("Opening text to introduce email");
body_start[1]=player.GetVar('Question1');
body_start[2]=player.GetVar('Question2');
body_start[3]=player.GetVar('Question3');
body_start[4]=player.GetVar('Question4');
body_start[5]=player.GetVar('Question5');
var mailto_link='mailto:'+email+'?subject='+subject+'&body='+escape(body_start);
emailwin=window.open(mailto_link,'emailWin');
emailwin=window.close();

Thank you in advance!

10 Replies
Sandeep Gadam

Hi Jennifer,

I was able to modify your code to get multiple variables into email. Please refer to the below code.

var player = GetPlayer();
var email=player.GetVar('YourEmail');
var email2=player.GetVar('SupEmail');
var email_address = email+','+email2;
var subject="Essential Soft Skills 101 Reflection";
var body_start=new Array("Opening text to introduce email");
body_start[1]=player.GetVar('Question1');
body_start[2]=player.GetVar('Question2');
body_start[3]=player.GetVar('Question3');
body_start[4]=player.GetVar('Question4');
body_start[5]=player.GetVar('Question5');
var mailto_link='mailto:'+email_address+'?subject='+subject+'&body='+escape(body_start);
emailwin=window.open(mailto_link,'emailWin');
emailwin=window.close();

I used the bold formatting for the revised line of code in the code above.

I was unable to fetch the question text and linebreaks for the user inputs because I am a novice at JavaScript.

Hope someone can assist you with that.

Please review and let me know if you have any questions.

Thank you!!

Jennifer Rowlands

Okay, the emails are working now, Thank you! I have also figure dout how to separate the paragraphs. Here's the latest code:

 var player = GetPlayer();
var email=player.GetVar('YourEmail');
var email2=player.GetVar('SupEmail');
var email_address = email+'; '+email2;
document.write("\n");
var subject="Essential Soft Skills 101 Reflection";
var body_start=new Array("Opening text to introduce email");
body_start[1]= "\n" + ('Question 1: ') +"\n" + (player.GetVar('Question1'));
body_start[2]= "\n" + ('Question 2: ') +"\n" + (player.GetVar('Question2'));
body_start[3]= "\n" + ('Question 3: ') +"\n" + (player.GetVar('Question3'));
body_start[4]= "\n" + ('Question 4: ') +"\n" + (player.GetVar('Question4'));
body_start[5]= "\n" + ('Question 5: ') +"\n" + (player.GetVar('Question5'));
var mailto_link='mailto:'+email_address+'?subject='+subject+'&body='+escape(body_start);
emailwin=window.open(mailto_link,'emailWin');
emailwin=window.close();

Nedim Ramic

Here is a solution to the course closing:

var player = GetPlayer();
var email = player.GetVar('YourEmail');
var email2 = player.GetVar('SupEmail');
var email_address = email + '; ' + email2;
var subject = "Essential Soft Skills 101 Reflection";
var body_start = "Opening text to introduce email\n";
body_start += "\nQuestion 1:\n" + player.GetVar('Question1');
body_start += "\nQuestion 2:\n" + player.GetVar('Question2');
body_start += "\nQuestion 3:\n" + player.GetVar('Question3');
body_start += "\nQuestion 4:\n" + player.GetVar('Question4');
body_start += "\nQuestion 5:\n" + player.GetVar('Question5');
var encoded_body_start = encodeURIComponent(body_start);
var mailto_link = 'mailto:' + email_address + '?subject=' + encodeURIComponent(subject) + '&body=' + encoded_body_start;
window.open(mailto_link, '_blank');

Earl Kreuzer

Hi Nedim, Is there a character count limit? 

I can get 3 out of 5 questions with answers to work but it stalls after 3. Any insight or workaround would be helpful.

My word count is 350 plus ( writers love to be wordy). Plus the user may need to add to answers.

FYI: It does work for all 5 if i cut the count down to short answers.

TY>

Nedim Ramic

Hi Earl,

As far as I know there should not be a character count limit. What I suspect is the URL length. The URL generated using the "mailto" scheme can be limited in length, and this limitation may vary between different email clients or web browsers. Excessive URL length can result in the email client refusing to open or in the loss of some information. For instance, I can get this to work if my "mailto_link" variable is less then 2030 characters in length. My default email client (Outlook) will open but refuse to open if "mailto_link" variable exceeds 2030 characters.  If I force open my Gmail instead I'm able to send 8134 characters. The Gmail would return some sort of bad request for anything over 8134 characters. Again, I can't say it's some kind of general rule, but rather just result of my tests run on two different email clients. If you're encountering issues with long URLs in your email client, consider shortening the URLs or finding alternative methods for handling data transmission, such as using attachments or server-side processing.

Earl Kreuzer

Hi Nedim, Thank you for the quick reply and great insight. I too have been testing and encountering limitations based on URL and client. I have not seen or found a workaround other than limiting the variable entries.  I have a few alternate routes to test. When I come across a viable solution I will advise this thread. Again thank you for the code and insight.

FYI: I have been testing ChatGPT and zzzcode.ai to write or adjust the code. I have had good success with both for other projects.

Best.