Straightforward Method of Sending A Triggered Email via AMPscript

By Naveen Murugan

Triggered email interaction is very important for building a constant dialog with the subscribers. More importantly, imagine the hours of manual effort a marketer can avoid in drafting emails just to send updates about a product!

Triggered emails from a tech standpoint, benefits users that are looking at receiving and tracking timely, data backed metrics to decide the next step of engagement. If you are looking at leveraging Salesforce Marketing Cloud (SFMC) for this purpose after ticking the check boxes from a basic list of priorities (such as send classification, options, destination, etc,), you many want to try out the below straightforward method via AMPscript.

In this blog, we are going to see how we can do that!

Let’s Imagine a Use Case: A customer has signed up with his basic information (Email Address, First Name & Last Name) and the subscriber wants to send a triggered email immediately from Marketing Cloud.

You Can Start with Creating a Triggered Send Data extension:

· Go to Email Studio → Email → Data extensions → Create data extension.

· Select ‘Standard Data extension’.

· In creation method, select ‘create from template’ and select ‘TriggeredSendDataExtension’.

· Add few other attributes according to requirement as shown in the below screenshot:

%%[
if RequestParameter("submitted") == "submitted" then
Set @EmailAddr = RequestParameter("Email")
Set @FirstName = RequestParameter("FirstName")
Set @LastName = RequestParameter("LastName")
/* Trigger Send Object Creation */
SET @ts = CreateObject("TriggeredSend")
SET @tsDef = CreateObject("TriggeredSendDefinition")
SET @ts_subkey = @EmailAddr
/* Set the External Key of the Trigger Send Definition */
SetObjectProperty(@tsDef, "CustomerKey", "30057")
SetObjectProperty(@ts, "TriggeredSendDefinition", @tsDef)

/* Create the Subscriber Object */
SET @ts_sub = CreateObject("Subscriber")
SetObjectProperty(@ts_sub, "EmailAddress", @EmailAddr)
/* Set SubscriberKey to EmailAddress */
SetObjectProperty(@ts_sub, "SubscriberKey", @EmailAddr)
/* Create and Set Attributes */
SET @attr = CreateObject("Attribute")
SetObjectProperty(@attr, "Name", "ChannelMemberID")
SetObjectProperty(@attr, "Value", "100021500")
AddObjectArrayItem(@ts_sub, "Attributes", @attr)
SET @attr = CreateObject("Attribute")
SetObjectProperty(@attr, "Name", "EmailAddress")
SetObjectProperty(@attr, "Value", @EmailAddr)
AddObjectArrayItem(@ts_sub, "Attributes", @attr)
SET @attr = CreateObject("Attribute")
SetObjectProperty(@attr, "Name", "FirstName")
SetObjectProperty(@attr, "Value", @FirstName)
AddObjectArrayItem(@ts_sub, "Attributes", @attr)

SET @attr = CreateObject("Attribute")
SetObjectProperty(@attr, "Name", "LastName")
SetObjectProperty(@attr, "Value", @LastName)
AddObjectArrayItem(@ts_sub, "Attributes", @attr)
/* Add all Attributes into Array */
AddObjectArrayItem(@ts, "Subscribers", @ts_sub)
/*Complete the Web API call to trigger send */
SET @ts_statusCode = InvokeCreate(@ts, @ts_statusMsg, @errorCode)
/* Raise Error if Trigger fails */
IF @ts_statusCode != "OK" THEN
RaiseError(@ts_statusMsg, 0, @ts_statusCode, @errorCode)
ENDIF
ENDIF
]%%

<form action="%%=RequestParameter('PAGEURL')=%%" method="POST">
Email: <input type="EmailAddress" name="Email" value=""><br/>
First Name: <input type="text" name="FirstName" value=""><br/>
Last Name: <input type="text" name="LastName" value=""><br/>
<input id="submitted" type="hidden" name="submitted" value="submitted"><input type="submit" value="Submit">
</form>

Then, Create a Triggered send UI in SFMC:

· Go to Email Studio → Email → Interactions → Triggered Sends.

· Select create and give a name for triggered email.

· Select the email which needs to be sent.

· Select the triggered data extension wherein the records must be added and select all subscriber list to add the records in the same list.

· Click on Save and select ‘Start/Restart’ to activate the triggered sends.

Add AMPscript in Cloud Page:

· Below is AMPscript code for Triggered email with simple form:

%%[
if RequestParameter("submitted") == "submitted" then
Set @EmailAddr = RequestParameter("Email")
Set @FirstName = RequestParameter("FirstName")
Set @LastName = RequestParameter("LastName")
/* Trigger Send Object Creation */
SET @ts = CreateObject("TriggeredSend")
SET @tsDef = CreateObject("TriggeredSendDefinition")
SET @ts_subkey = @EmailAddr
/* Set the External Key of the Trigger Send Definition */
SetObjectProperty(@tsDef, "CustomerKey", "30057")
SetObjectProperty(@ts, "TriggeredSendDefinition", @tsDef)

/* Create the Subscriber Object */
SET @ts_sub = CreateObject("Subscriber")
SetObjectProperty(@ts_sub, "EmailAddress", @EmailAddr)
/* Set SubscriberKey to EmailAddress */
SetObjectProperty(@ts_sub, "SubscriberKey", @EmailAddr)
/* Create and Set Attributes */
SET @attr = CreateObject("Attribute")
SetObjectProperty(@attr, "Name", "ChannelMemberID")
SetObjectProperty(@attr, "Value", "100021500")
AddObjectArrayItem(@ts_sub, "Attributes", @attr)
SET @attr = CreateObject("Attribute")
SetObjectProperty(@attr, "Name", "EmailAddress")
SetObjectProperty(@attr, "Value", @EmailAddr)
AddObjectArrayItem(@ts_sub, "Attributes", @attr)
SET @attr = CreateObject("Attribute")
SetObjectProperty(@attr, "Name", "FirstName")
SetObjectProperty(@attr, "Value", @FirstName)
AddObjectArrayItem(@ts_sub, "Attributes", @attr)

SET @attr = CreateObject("Attribute")
SetObjectProperty(@attr, "Name", "LastName")
SetObjectProperty(@attr, "Value", @LastName)
AddObjectArrayItem(@ts_sub, "Attributes", @attr)
/* Add all Attributes into Array */
AddObjectArrayItem(@ts, "Subscribers", @ts_sub)
/*Complete the Web API call to trigger send */
SET @ts_statusCode = InvokeCreate(@ts, @ts_statusMsg, @errorCode)
/* Raise Error if Trigger fails */
IF @ts_statusCode != "OK" THEN
RaiseError(@ts_statusMsg, 0, @ts_statusCode, @errorCode)
ENDIF
ENDIF
]%%

<form action="%%=RequestParameter('PAGEURL')=%%" method="POST">
Email: <input type="EmailAddress" name="Email" value=""><br/>
First Name: <input type="text" name="FirstName" value=""><br/>
Last Name: <input type="text" name="LastName" value=""><br/>
<input id="submitted" type="hidden" name="submitted" value="submitted"><input type="submit" value="Submit">
</form>

In Action:

Simple form for Demo:

Source: SalesforceFan(https://www.salesforcefan.com/post/sending-triggered-email-via-ampscript)

Upon clicking submit, you can see that same records are inserted into the data extension and the customer shall receive an email to the respective email address:

Source: Salesforce Fan (https://www.salesforcefan.com/post/sending-triggered-email-via-ampscript)

That was easy, wasn’t it? Share your thoughts in the comment section below.

Happy Learning!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store