How to send a Triggered Send email with server-side Javascript

How to send a Triggered Send email with server-side Javascript

Using server-side Javascript to send Triggered Send emails in Marketing Cloud is a very flexible and simple solution. When compared to the same code made with AMPscript, it appears to be more structured and clear.

Not a fan of reading? Jump to the code snippet.

What do you need?

There are 3 steps that you need to follow in this exact order:

  1. Create a Triggered Send Data Extension
  2. Create an email with Content Builder
  3. Create a Triggered Send

Let’s examine each step separately.

Step 1: Create a Triggered Send Data Extension

In order to create a Triggered Send Data Extension, you need to use Contact Builder.

Choose Create From Template as the creation method and choose TriggeredSendDataExtension as the template.

You’ll notice that 2 fields are now mandatory: SubscriberKeyand EmailAddress. For the sake of this tutorial, let’s add 2 more fields:

FirstNameText50Nullable
LanguageText2Nullable

Step 2: Create an email

In order to create a Triggered Send Data Extension, you need to use Content Builder. Make it as simple as possible and use the following code for later customization:

%%[ 
	VAR @Greeting
	IF Language == "EN" THEN 
		SET @Greeting = "Hello"
	ELSE
		SET @Greeting = "Bonjour"
	ENDIF 
]%%
<h1>%%=v(@Greeting)=%% %%=v(FirstName)=%%</h1>

Step 3: Create a Triggered Send

In order to create a Triggered Send, you need to use Email Studio.

The process is straightforward, just make sure to choose the Email and the Triggered Send Data Extension you created previously.

If you get any errors when trying to save, it usually means that your Email contains some AMPscript that uses different field names than what you have in your Triggered Send Data Extension.

The last step is to Start the Triggered Send by clicking on the Start/Restart button on the top.

Now, let’s take a note of the External Key of our Triggered Send and move forward.

Triggering an email with server-side Javascript

At first, let’s define some AMPscript variables, where the SubsciberKey is a simple timestamp for the purpose of this tutorial and the TriggerSendExternalKey is the External Key generated by the creation of our Triggered Send.

%%[
	SET @TriggeredSendExternalKey = "123456"
	SET @SubscriberKey = Format(SystemDateToLocalDate(NOW()), "yyddMMhhmmss")
    SET @FirstName = "Borat"
    SET @EmailAddress = "youremailaddress@mail.com"
	SET @Language = "EN"
]%%

Then, proceed with the server-side Javascript as follows.

Create a Javascript object divided in 2 sub-objects that include the name/value pairs of the fields from the Triggered Send Data Extension (without quotes) and the subscriber data we are going to be using.

Use Init and Send functions to load the Triggered Send and provide it with 2 sub-objects that we defined earlier.

<script runat="server">

    Platform.Load("core","1.1");
  
    var data = {
       	attributes : {
      		FirstName: Platform.Variable.GetValue("@FirstName"),
			Language: Platform.Variable.GetValue("@Language")
    	},
      	subscriber : {
    		EmailAddress: Platform.Variable.GetValue("@EmailAddress"),
      		SubscriberKey: Platform.Variable.GetValue("@SubscriberKey")
    	}
    }
    
  	var TSD = TriggeredSend.Init(Platform.Variable.GetValue("@TriggeredSendExternalKey"));
	var Status = TSD.Send(data.subscriber,data.attributes);
   
</script>

As for the finishing touches, it’s highly advised to use try…catch to display any possible errors and avoid disrupting the flow of any javascript that follows.

And we are done!

Just execute this code from an empty Cloud Page and see the magic happen.

Full code

%%[
	SET @TriggeredSendExternalKey = "123456"
	SET @SubscriberKey = Format(SystemDateToLocalDate(NOW()), "yyddMMhhmmss")
    SET @FirstName = "Borat"
    SET @EmailAddress = "youremailaddress@mail.com"
	SET @Language = "EN"
]%%
<script runat="server">

    Platform.Load("core","1.1");
  
    var data = {
       	attributes : {
      		FirstName: Platform.Variable.GetValue("@FirstName"),
			Language: Platform.Variable.GetValue("@Language")
    	},
      	subscriber : {
    		EmailAddress: Platform.Variable.GetValue("@EmailAddress"),
      		SubscriberKey: Platform.Variable.GetValue("@SubscriberKey")
    	}
    }
    
  	try {
      
      	var TSD = TriggeredSend.Init(Platform.Variable.GetValue("@TriggeredSendExternalKey"));
		var Status = TSD.Send(data.subscriber,data.attributes);
      
      	if(Status != "OK") {
   			Write("(!) Error: " + Stringify(Status));
		} else {
        	Write("(+) Great success!");
        }
      
    } catch(err) {
     	
		Write("(!) Error: " + Stringify(err));
      
    }
    
</script>

Troubleshooting

Your code doesn’t work? Let’s figure out why!

Can’t create a Triggered Send?

  1. Did you specify the correct Email, Data Extension and choose the correct List with an add/update subscribers option?
  2. Check your Email and test it with a dummy record from your Triggered Send Data Extension.

Email not sent?

  1. Check the Triggered Send Data Extension. If your record exists, the email should arrive shortly, unless it got queued for some reason, in which case I advise you to restart the Triggered Send from the Email Studio.
  2. Check the status of your Triggered Send, it might be on Pause.
  3. The person might be blacklisted or unsubscribed from the List you chose when creating the Triggered Send.

Error 500?

  1. Does the External Key exist? Please check the Trigger Send in the Email Studio.
  2. The Trigger Send object property name doesn’t exist in the Triggered Send Data Extension or is in an incorrect format (in short, don’t try to write letters in a Boolean type field).
  3. Syntax error. Please, refer to the error message from the try…catch.

Have I missed anything?

Please poke me with a sharp comment below or use the contact form.

Pay me a coffee

Want to say thanks? Pay me a coffee! Remember, I turn coffee into code.

Leave a Reply

Your email address will not be published. Required fields are marked *

ampscript
Up Next:

How to pre-fill a form with AMPscript

How to pre-fill a form with AMPscript