https://www.myampscript.com Wed, 25 May 2022 23:32:54 +0000 en hourly 1 https://wordpress.org/?v=6.3.2 Dynamic Sender Profiles with AMPscript https://www.myampscript.com/dynamic-sender-profiles-with-ampscript/ https://www.myampscript.com/dynamic-sender-profiles-with-ampscript/#comments Wed, 25 May 2022 18:44:11 +0000 https://www.myampscript.com/?p=963 Overview

Have you ever wanted to customize the from name and from email in your Marketing Cloud emails? If so this demo is meant for you. This feature uses AMPscript mixed with some account configuration in order to make this work. For this demo, we want to pull dynamic from name and from email from our content DE and use that within our new custom Dynamic Sender Profile. Objects you’ll be building are:

  • Create 1 test sendable DE.
  • Create 1 test content DE.
  • Create 1 Dynamic From Name code snippet.
  • Create 1 Dynamic From Email code snippet.
  • Create 1 Dynamic Sender Profile.
  • Create 1 test email to preview and send with.

Sendable Data Extension

This is the sendable data extension. You’ll notice that each subscriber has a language preference. We’ll match the subscribers language preference against the language field in the content DE so that we can return the correct From Name and From Email to use in our Dynamic Sender Profile. You can download the sample data for this tutorial by clicking here.


Content Data Extension

This is the content data extension. You’ll notice that each row contains custom content for dynamic from name and from email. We’ll use the subscribers language preference to return the correct from name and from email to use inside our Dynamic Sender Profile. You can download the sample data for this tutorial by clicking here.


Dynamic From Name Code Snippet

This code snippet uses the subscriber’s language preference to do a lookup on the content DE to pull back the dynamic from name, which is then used in the new Dynamic Sender Profile. You’ll also notice that immediately following the lookup we are outputting that value to the snippet via %%=v(@fromName)=%%.

%%[
/**
* Dynamic From Name Logic
* Use subscribers [language] preference from the entry DE to pull back dynamic fromName from the content DE.
*/
SET @language = AttributeValue("language")
SET @fromName = Lookup("ENT.04_DynamicSenderProfile_Content","fromName","language", @language)
]%%%%=v(@fromName)=%%
View this gist on GitHub

Dynamic From Email Code Snippet

This code snippet uses the subscriber’s language preference to do a lookup on the content DE to pull back the dynamic from email, which is then used in the new Dynamic Sender Profile. You’ll also notice that immediately following the lookup we are outputting that value to the snippet via %%=v(@fromEmail)=%%.

%%[
/**
* Dynamic From Email Logic
* Use subscribers [language] preference from the entry DE to pull back dynamic fromEmail from the content DE.
*/
SET @language = AttributeValue("language")
SET @fromEmail = Lookup("ENT.04_DynamicSenderProfile_Content","fromEmail","language", @language)
]%%%%=v(@fromEmail)=%%
View this gist on GitHub

Dynamic Sender Profile Setup

This is wher you will create your new custom dynamic sender profile. You can create a new sender profile by navigating to: Admin > Send Management > Sender Profiles > Create. This is where we want to use the TreatAsContent() and ContentByKey() to insert our DynamicFromName and DynamicFromEmail code snippets into the From Name and From Email fields. Please use the two strings below:

  • %%=TreatAsContent(ContentBlockByKey(‘DynamicFromName’))=%%
  • %%=TreatAsContent(ContentBlockByKey(‘DynamicFromEmail’))=%%
%%=TreatAsContent(ContentBlockByKey('DynamicFromName'))=%%
%%=TreatAsContent(ContentBlockByKey('DynamicFromEmail'))=%%
View this gist on GitHub

Important Note!

It’s important that your dynamic from email addresses use a authenticated @domain address. Otherwise, your email sends will more than likely fail on the backend. For example, if the authenticated @domain for your account is @company.com and your dynamic from email addresses use some other @domain.com then you’ll run into deliverability issues. Please make sure to consider the following items below before using Dynamic Sender Profiles.

  • Please ensure all your dynamic from email addresses use an authenticated @domain address.
  • Please consult your deliverability specialist beforehand to minimize risk.
  • Please consult your account executive to ensure enhanced sender profiles is enabled on your account.

Gallery

]]>
https://www.myampscript.com/dynamic-sender-profiles-with-ampscript/feed/ 1
Looping through XML with AMPscript Advanced Level https://www.myampscript.com/looping-through-xml-with-ampscript-advanced-level/ https://www.myampscript.com/looping-through-xml-with-ampscript-advanced-level/#comments Thu, 20 Jan 2022 16:47:37 +0000 https://www.myampscript.com/?p=919 AMPscript Looping

This example shows how to loop through xml payloads using AMPscript. We’ll break this process into few different steps.

  1. Review the xml data structured.
  2. Review the data extension.
  3. Writing logic that loads the xml into the email.
  4. Write logic to include safeguards for error handling.
  5. Write logic to loop through xml nodes.
  6. Review the code output.

Reviewing the XML Data Structure

The goal is to write AMPscript logic that will loop through all nodes. This is a screenshot of what the XML payload looks like. You’ll notice that the entire <catalog> contains multiple <book> nodes that we’ll eventually loop through with AMPscript. Additionally, you’ll also notice that for every <book> node it contains data for that specific book’s ID, Author, Title, and Genre. You can download the sample data for this tutorial by clicking here.

<?xml version='1.0' encoding='UTF-8'?>
<catalog>
<book>
<id>001</id>
<author>Liu Cixin</author>
<title>The Three-Body Problem</title>
<genre>Science Fiction</genre>
</book>
<book>
<id>002</id>
<author>Andy Weir</author>
<title>The Martian</title>
<genre>Science Fiction</genre>
</book>
<book>
<id>003</id>
<author>Ernest Cline</author>
<title>Ready Player One</title>
<genre>Science Fiction</genre>
</book>
</catalog>
View this gist on GitHub

Reviewing the Data Extension

The goal is to write AMPscript logic that will load and loop through this XML from within the email. You’ll notice that we have three use cases mocked up below. The 2nd and 3rd use case represent negative test scenarios. This means that our AMPscript safeguards should raise an error whenever there is missing and or incomplete data. You can download the sample data for this tutorial by clicking here.

  1. Subscriber with complete XML data.
  2. Subscriber with no XML data.
  3. Subscriber with incomplete XML data.

Looping through XML with AMPscript

This example shows you how you can loop through XML with AMPscript. In this example you’ll notice that we are doing the following:

  1. Taking the XML data from the [XML] field in the sendable DE and assigning that to a variable called @XML
  2. Write AMPscript logic that uses the BuildRowSetFromXML() function to then build rows based on however many <book> nodes are available.
  3. Write AMPscript logic that sets up safeguards to handle missing XML data.
  4. Write AMPscript logic that sets up safeguards to handle incomplete XML data.
  5. Write AMPscript logic so that the the BuildRowSetFromXML() function and the AMPscript for loop only run if the XML data exists.
<!-- Example Code -->
%%[
/**
* Looping Through XML Payload Data Demo
* This example consumes the XML payload that's stored in the [XML] field.
* This example loops through all <book> nodes and renders book details in the email.
* This example uses RaiseError() when there is no xml payload data to parse.
* This example uses RaiseError() when there are no <book> in the xml payload.
*/
SET @XML = [XML]
IF NOT EMPTY(@XML) THEN
/**
* Looping Through Books
* This block of code examins all <book> nodes from the XML payload.
* This block of code counts the number of <book> nodes from the XML payload.
* This block of code creates a loop that runs for however many <books> are in the payload.
*/
SET @books = BuildRowsetFromXML(@XML,"//catalog/book",1)
SET @book_rows = RowCount(@books)
IF @book_rows > 0 THEN
/**
* Loop Start
* This loop runs for however many books were identified with RowCount()
*/
FOR @i = 1 to @book_rows DO
SET @book = concat("<root>", Field(Row(@books,@i),"Xml"), ",</root>")
/**
* Book Details
* Desc: For each loop iteration identify what the Book ID is.
* Mapping: <catalog><book><id></id></book></catalog>
*/
SET @item_id = BuildRowsetFromXML(@book,"//id",1)
IF RowCount(@item_id) >= 1 THEN
SET @book_id = Field(Row(@item_id,1),"Value")
ELSE
SET @book_id = ""
ENDIF
/**
* Book Details
* Desc: For each loop iteration identify what the book author is.
* Mapping: <catalog><book><author></author></book></catalog>
*/
SET @item_author = BuildRowsetFromXML(@book,"//author",1)
IF RowCount(@item_author) >= 1 THEN
SET @book_author = Field(Row(@item_author,1),"Value")
ELSE
SET @book_author = ""
ENDIF
/**
* Book Details
* Desc: For each loop iteration identify what the book title is.
* Mapping: <catalog><book><title></title></book></catalog>
*/
SET @item_title = BuildRowsetFromXML(@book,"//title",1)
IF RowCount(@item_title) >= 1 THEN
SET @book_title = Field(Row(@item_title,1),"Value")
ELSE
SET @book_title = ""
ENDIF
/**
* Book Details
* Desc: For each loop iteration identify what the book genre is.
* Mapping: <catalog><book><genre></genre></book></catalog>
*/
SET @item_genre = BuildRowsetFromXML(@book,"//genre",1)
IF RowCount(@item_genre) >= 1 THEN
SET @book_genre = Field(Row(@item_genre,1),"Value")
ELSE
SET @book_genre = ""
ENDIF
]%%
<!-- Book Details -->
<p>
Book ID: %%=v(@book_id)=%%<br>
Book Author: %%=v(@book_author)=%%<br>
Book Title: %%=v(@book_title)=%%<br>
Book Genre: %%=v(@book_genre)=%%
</p>
%%[
/* next book */
next @i
ELSE
/**
* Error Handling Safeguard
* Throw a RaiseError() when the xml payload has no book data to show.
* Kill the send for that one subscriber and not the entire job.
*/
RaiseError("The xml payload has no book data.", true)
ENDIF
ELSE
/**
* Error Handling Safeguard
* Throw a RaiseError() if there is no xml data to consume.
* Kill the send for that one subscriber and not the entire job.
*/
RaiseError("There is no xml payload to use.", true)
ENDIF
]%%
<!-- Example Output -->
Book ID: 001
Book Author: Liu Cixin
Book Title: The Three-Body Problem
Book Genre: Science Fiction
Book ID: 002
Book Author: Andy Weir
Book Title: The Martian
Book Genre: Science Fiction
Book ID: 003
Book Author: Ernest Cline
Book Title: Ready Player One
Book Genre: Science Fiction
View this gist on GitHub

Gallery

]]>
https://www.myampscript.com/looping-through-xml-with-ampscript-advanced-level/feed/ 3
AMPscript LookupRows Function Intermediate Level https://www.myampscript.com/ampscript-lookuprows-function-intermediate-level/ https://www.myampscript.com/ampscript-lookuprows-function-intermediate-level/#comments Thu, 30 Dec 2021 22:10:31 +0000 https://www.myampscript.com/?p=873 Content LookupRows

This function returns a rowset from a data extension where the field matches the specified value. This function returns a maximum of 2000 rows. To better control the number or order of rows returned please use the LookupOrderedRows function instead. Using the LookupOrderedRows function enhances script performance. But for this example, we will focus on how to use the standard LookupRows function.

  • LookupRows(1, 2, 3)

Content Lookup Benefits

Here are some reasons why you might want to consider using a Content Lookup approach:

  • The content lookup approach works best when emails need to support multiple languages and or segments.
  • The content lookup approach allows the dynamic email to scale to evolving business needs.
  • The content lookup approach allows your email to scale to evolving business needs.
  • The content lookup approach allows us to separate email content from the email framework which makes it easier to add, remove, or modify content without having to touch code.

Sendable Data Extension

This is the sendable data extension. You’ll notice that each subscriber has a language preference. We’ll use the language preference from the sendable DE to match against the language field from the content DE so that we can return the correct row in our LookupRows() script. You can download my sample data for the sendable DE by clicking here.


Content Data Extension

This is the content data extension. You’ll notice that each row contains content that is specific to a language. We’ll use the subscribers language preference to return the correct translated content to use within the email. Simply add more rows so that your email can scale to support additional languages. You can download my sample data for the content DE by clicking here.


LookupRows Function

This example returns copy from the content DE based on subscribers language preference.

<!-- Example Code -->
%%[
/**
* Basic Subscriber Variables
* Subscriber details from the sendable DE.
*/
SET @email_address = [email_address]
SET @language = [language]
SET @first_name = AttributeValue("first_name")
IF EMPTY(@first_name) THEN
SET @first_name = "Value Customer"
ENDIF
/**
* Email Content Variables
* Using subscribers language preference to pull email content from the content DE.
* Using ,0 to set the variable to null if the specific field is left empty in the content DE.
* Using RaiseError() to kill the send when that subscribers language pref is not represented in the content DE.
*/
SET @content = LookupRows("ENT.Demo-Matrix-Content-DE", "language", @language)
SET @contentCount = RowCount(@content)
IF @contentCount > 0 THEN
SET @contentRow = Row(@content, 1)
SET @subjectline = TreatAsContent(FIELD(@contentRow, "subjectline", 0))
SET @preheader = TreatAsContent(FIELD(@contentRow, "preheader", 0))
SET @copy1 = TreatAsContent(FIELD(@contentRow, "copy1", 0))
SET @copy2 = TreatAsContent(FIELD(@contentRow, "copy2", 0))
ELSE
RaiseError(Concat("The subscriber (", @email_address, ") uses (", @language, ") that is not represented in the content DE."), true)
ENDIF
]%%
<p>Subscriber Email Address: %%=v(@email_address)=%%</p>
<p>Subscriber Language Preference: %%=v(@language)=%%</p>
<p>Subscriber First Name: %%=v(@first_name)=%%</p>
<p>Subject Line: %%=v(@subjectline)=%%</p>
<p>Preheader: %%=v(@preheader)=%%</p>
<p>Copy1: %%=v(@copy1)=%%</p>
<p>Copy2: %%=v(@copy2)=%%</p>
<!-- Example Output (en_us) -->
Subscriber Email Address: subscriber_enus@blank.com
Subscriber Language Preference: en_us
Subscriber First Name: Nolan
Subject Line: Thank you for joining!
Preheader: We will send you AMPscript tips, tricks, and more.
Copy1: Thanks for subscribing, Nolan!
Copy2: Thank you for subscribing to our monthly newsletter. We will send you emails containing AMPscript tips, tricks, and more. We also enjoy sharing industry best practices as well. Thanks again for subscribing to our monthly newsletter.
<!-- Example Output (es_es) -->
Subscriber Email Address: subscriber_eses@blank.com
Subscriber Language Preference: es_es
Subscriber First Name: nTEST
Subject Line: ¡Gracias por unirte!
Preheader: Le enviaremos consejos, trucos y más de AMPscript.
Copy1: ¡Gracias por suscribirte, nTEST!
Copy2: Gracias por suscribirse a nuestro boletín mensual. Le enviaremos correos electrónicos con sugerencias, trucos y más de AMPscript. También disfrutamos compartir las mejores prácticas de la industria. Gracias nuevamente por suscribirse a nuestro boletín mensual.
<!-- Example Output (fr_fr) -->
Subscriber Email Address: subscriber_frfr@blank.com
Subscriber Language Preference: fr_fr
Subscriber First Name: Value Customer
Subject Line: Merci de nous rejoindre!
Preheader: Nous vous enverrons des conseils, des astuces et plus encore sur AMPscript.
Copy1: Merci pour votre subscription, Value Customer!
Copy2: Merci de vous être abonné à notre newsletter mensuelle. Nous vous enverrons des e-mails contenant des conseils, des astuces et plus encore sur AMPscript. Nous aimons également partager les meilleures pratiques de l'industrie. Merci encore de vous être abonné à notre newsletter mensuelle.
<!-- Example Output (ja_jp) -->
Subscriber Email Address: subscriber_jajp@blank.com
Subscriber Language Preference: ja_jp
Subscriber First Name: Nolo
Subject Line: ご参加ありがとうございました!
Preheader: AMPscriptのヒントやコツなどをお送りします。
Copy1: 購読していただきありがとうございます、Nolo!
Copy2: 月刊ニュースレターをご購読いただきありがとうございます。 AMPscriptのヒントやコツなどを記載したメールをお送りします。また、業界のベストプラクティスを共有することも楽しんでいます。月刊ニュースレターをご購読いただき、誠にありがとうございます。
View this gist on GitHub

Gallery

]]>
https://www.myampscript.com/ampscript-lookuprows-function-intermediate-level/feed/ 1
AMPscript Format Functions Beginner Level https://www.myampscript.com/ampscript-format-functions-beginner-level/ https://www.myampscript.com/ampscript-format-functions-beginner-level/#comments Tue, 21 Dec 2021 14:41:59 +0000 https://www.myampscript.com/?p=856 AMPscript Format Functions

AMPscript DateTime functions allow us to manipulate display format, perform math functions, and control email content using static, dynamic, and real-time values. Formatting functions provide a powerful tool set for converting various data types to formatted strings and numbers.

  • FormatDate(1, 2, 3, 4)
  • FormatCurrency(1, 2, 3, 4)
  • FormatNumber(1, 2, 3)
  • Format(1, 2, 3, 4)

Format Date Function

This function formats a specified string as a date value for a specified format pattern and locale.

Example
%%=FormatDate(1, 2, 3, 4)=%%

<!-- Example Code -->
%%[
/**
* Format Date Logic
* Formats a specified string as a date value for a specified format and locale.
*/
VAR @date, @date_english, @date_spanish, @date_french, @date_chinese
SET @date = "2012-10-05 03:30:34.567890"
SET @date_english = FormatDate(@date, "l", "HH:MM tt", "en-us")
SET @date_spanish = FormatDate(@date, "l", "HH:MM tt", "es-mx")
SET @date_french = FormatDate(@date, "l", "HH:MM tt", "fr-fr")
SET @date_chinese = FormatDate(@date, "l", "HH:MM tt", "zh-cn")
]%%
<p>English: %%=v(@date_english)=%%</p>
<p>Spanish: %%=v(@date_spanish)=%%</p>
<p>French: %%=v(@date_french)=%%</p>
<p>Chinese: %%=v(@date_chinese)=%%</p>
<!-- Example Output -->
English: Friday, October 5, 2012 03:30 AM
Spanish: viernes, 5 de octubre de 2012 03:30 a. m.
French: vendredi 5 octobre 2012 03:30
Chinese: 2012年10月5日 03:30 上午
View this gist on GitHub

Format Currency Function

This function formats a specified string as a currency value for a specified locale.

Example
%%=FormatCurrency(1, 2, 3, 4)=%%

<!-- Example Code -->
%%[
/**
* Format Currency Logic
* Formats a specified string as a currency value for a specified format and locale.
*/
VAR @price, @price_usa, @price_mexico, @price_france, @price_canada, @price_china
SET @price = "199.99"
SET @price_usa = FormatCurrency(@price, "en-us")
SET @price_mexico = FormatCurrency(@price, "es-mx")
SET @price_france = FormatCurrency(@price, "fr-fr")
SET @price_canada = FormatCurrency(@price, "fr-ca")
SET @price_china = FormatCurrency(@price, "zh-cn")
]%%
<p>USA: %%=v(@price_usa)=%%</p>
<p>Mexico: %%=v(@price_mexico)=%%</p>
<p>France: %%=v(@price_france)=%%</p>
<p>Canada: %%=v(@price_canada)=%%</p>
<p>China: %%=v(@price_china)=%%</p>
<!-- Example Output -->
USA: $199.99
Mexico: $199.99
France: 199,99 €
Canada: 199,99 $
China: ¥199.99
View this gist on GitHub

Format Number Function

This function formats a specified string as a number for a specified locale.

Example
%%=FormatNumber(1, 2, 3)=%%

<!-- Example Code -->
%%[
/**
* Format Number Logic
* Formats a specified string as a number value for a specified format or locale.
*/
VAR @number, @currency_english, @currency_french, @decimal, @fixed, @percent, @general
SET @number = "199"
SET @currency_english = FormatNumber(@number, "C", "en-us")
SET @currency_french = FormatNumber(@number, "C", "fr-fr")
SET @decimal = FormatNumber(@number, "D")
SET @fixed = FormatNumber(@number, "F")
SET @percent = FormatNumber(@number, "P")
SET @general = FormatNumber(@number, "G")
]%%
<p>Currency (English): %%=v(@currency_english)=%%</p>
<p>Currency (French): %%=v(@currency_french)=%%</p>
<p>Decimal: %%=v(@decimal)=%%</p>
<p>Fixed: %%=v(@fixed)=%%</p>
<p>Percent: %%=v(@percent)=%%</p>
<p>General: %%=v(@general)=%%</p>
<!-- Example Output -->
Currency (English): $199.00
Currency (French): 199,00 €
Decimal: 199
Fixed: 199.00
Percent: 19,900.00%
General: 199
View this gist on GitHub

Format Function

This function formats a string into a specified format pattern and locale. You’ll want to use this function for date and time formatting that requires a locale setting.

Example
%%=Format(1, 2, 3, 4)=%%

<!-- Example Code -->
%%[
/**
* Format Logic
* This function formats a string into a specified format pattern and locale.
* Use this function for date and time formatting that requires a locale.
*/
VAR @date, @long_date_english, @long_date_spanish, @short_date_english, @short_date_spanish, @full_long_date_english, @full_long_date_spanish, @full_short_date_english, @full_short_date_spanish, @general_long_date_english, @general_long_date_spanish, @general_short_date_english, @general_short_date_spanish
SET @date = "2012-10-05 03:30:34.567890"
SET @long_date_english = Format(@date,"D", "Date", "en-us")
SET @long_date_spanish = Format(@date,"D", "Date", "es-mx")
SET @short_date_english = Format(@date,"d", "Date", "en-us")
SET @short_date_spanish = Format(@date,"d", "Date", "es-mx")
SET @full_long_date_english = Format(@date,"F", "Date", "en-us")
SET @full_long_date_spanish = Format(@date,"F", "Date", "es-mx")
SET @full_short_date_english = Format(@date,"f", "Date", "en-us")
SET @full_short_date_spanish = Format(@date,"f", "Date", "es-mx")
SET @general_long_date_english = Format(@date,"G", "Date", "en-us")
SET @general_long_date_spanish = Format(@date,"G", "Date", "es-mx")
SET @general_short_date_english = Format(@date,"g", "Date", "en-us")
SET @general_short_date_spanish = Format(@date,"g", "Date", "es-mx")
]%%
<p>Long Date (English): %%=v(@long_date_english)=%%</p>
<p>Long Date (Spanish): %%=v(@long_date_spanish)=%%</p>
<p>Short Date (English): %%=v(@short_date_english)=%%</p>
<p>Short Date (Spanish): %%=v(@short_date_spanish)=%%</p>
<p>Full Long Date (English): %%=v(@full_long_date_english)=%%</p>
<p>Full Long Date (Spanish): %%=v(@full_long_date_spanish)=%%</p>
<p>Full Short Date (English): %%=v(@full_short_date_english)=%%</p>
<p>Full Short Date (Spanish): %%=v(@full_short_date_spanish)=%%</p>
<p>General Long Date (English): %%=v(@general_long_date_english)=%%</p>
<p>General Long Date (Spanish): %%=v(@general_long_date_spanish)=%%</p>
<p>General Short Date (English): %%=v(@general_short_date_english)=%%</p>
<p>General Short Date (Spanish): %%=v(@general_short_date_spanish)=%%</p>
<!-- Example Output -->
Long Date (English): Friday, October 5, 2012
Long Date (Spanish): viernes, 5 de octubre de 2012
Short Date (English): 10/5/2012
Short Date (Spanish): 05/10/2012
Full Long Date (English): Friday, October 5, 2012 3:30:34 AM
Full Long Date (Spanish): viernes, 5 de octubre de 2012 03:30:34 a. m.
Full Short Date (English): Friday, October 5, 2012 3:30 AM
Full Short Date (Spanish): viernes, 5 de octubre de 2012 03:30 a. m.
General Long Date (English): 10/5/2012 3:30:34 AM
General Long Date (Spanish): 05/10/2012 03:30:34 a. m.
General Short Date (English): 10/5/2012 3:30 AM
General Short Date (Spanish): 05/10/2012 03:30 a. m.
View this gist on GitHub

Gallery

]]>
https://www.myampscript.com/ampscript-format-functions-beginner-level/feed/ 1
AMPscript DateTime Functions Beginner Level https://www.myampscript.com/ampscript-datetime-functions-beginner-level/ https://www.myampscript.com/ampscript-datetime-functions-beginner-level/#comments Tue, 14 Dec 2021 20:09:53 +0000 https://www.myampscript.com/?p=822 AMPscript DateTime Functions

AMPscript DateTime functions allow us to manipulate display format, perform math functions, and control email content using static, dynamic, and real-time values with:

  • Now(1) Campaign Sends
  • Now(1) Triggered Sends
  • DateAdd(1, 2, 3)
  • DateDiff(1, 2, 3)
  • DateParse(1, 2)
  • DatePart(1, 2)

Now(1) Function – Campaign Sends

The Now() function is used to capture the system DateTime at the time of send. The system uses Central Standard Time and does not adjust for Daylight Savings Time. Now() returns a dynamic date that will update when using in an email VAWP (View As Web Page) link and or landing pages. You’ll want to use Now(1) to preserve the date at the time of send.

Example
%%=Now(1)=%%

<!-- Example Code -->
%%[
/**
* Now Logic
* Used to generate a dynamic date at the time of send
*/
VAR @date
SET @date = Now(1)
]%%
<p>%%=v(@date)=%%</p>
<!-- Example Output -->
12/14/2021 9:02:30 AM
View this gist on GitHub

Now(1) Function – Triggered Sends

Now(1) doesn’t work as expected for triggered sends. When used in a triggered send, it returns the last time the triggered-send-definition was published. If you need to maintain the accurate send time and date in a trigger (i.e. for VAWP links,) use this workaround: Add a date field to the triggered DE (name it something like “DateAdded”) and auto-populate it with the current date. This sets that field to the date/time the record was added to the DE (the date/time the triggered email was sent).

<!-- Example Code -->
%%[
/**
* Now Logic (Triggered Send Workaround)
* Using Now(1) in a triggered send returns the latest TSD published time.
* Add a date field to your triggered DE and name it something like DateAdded.
* Add a default value that auto-populates that new field with current date.
* This captures the date/time the subscriber was added to the triggered DE.
* Use a simple lookup that returns the DateAdded for that subscriber.
*/
VAR @dateRow, @sendDate
SET @dateRow = LookupRows("Your Trigger DE Name Here", "EmailAddress", @emailAddress)
SET @sendDate = Field(@dateRow, "DateAdded")
]%%
<p>%%=v(@sendDate)=%%</p>
<!-- Example Output -->
12/14/2021 9:02:30 AM
View this gist on GitHub

Date Add Function

The DateAdd() function is used to increase or decrease the date time value. It can be used with a date object or with the Now() function. You need to date objects in order for this to work.

Example
%%=DateAdd(1, 2, 3)=%%

<!-- Example Code -->
%%[
/**
* Date Add Logic
* Used to increase or decrease date values.
*/
VAR @initialDate, @previousDay, @nextDay, @nextMonth, @nextMinute, @nextHour
SET @initialDate = "12/14/2021 9:00:00 AM"
SET @previousDay = DateAdd(@initialDate,"-1","d") /* Decreases by one day */
SET @nextDay = DateAdd(@initialDate,"1","d") /* Increases by one day */
SET @nextMonth = DateAdd(@initialDate,"1","m") /* Increases by one month*/
SET @nextYear = DateAdd(@initialDate,"1","y") /* Increases by one year*/
SET @nextMinute = DateAdd(@initialDate,"1","mi") /* Increases by one minute */
SET @nextHour = DateAdd(@initialDate,"1","h") /* Increases by one hour */
]%%
<p>Initial Date: %%=v(@initialDate)=%%</p>
<p>Previous Day: %%=v(@previousDay)=%%</p>
<p>Next Day: %%=v(@nextDay)=%%</p>
<p>Next Month: %%=v(@nextMonth)=%%</p>
<p>Next Year: %%=v(@nextYear)=%%</p>
<p>Next Minute: %%=v(@nextMinute)=%%</p>
<p>Next Hour: %%=v(@nextHour)=%%</p>
<!-- Example Output -->
Initial Date: 12/14/2021 9:00:00 AM
Previous Day: 12/13/2021 9:00:00 AM
Next Day: 12/15/2021 9:00:00 AM
Next Month: 1/14/2022 9:00:00 AM
Next Year: 12/14/2022 9:00:00 AM
Next Minute: 12/14/2021 9:01:00 AM
Next Hour: 12/14/2021 10:00:00 AM
View this gist on GitHub

Date Diff Function

The DateDiff() function returns the difference between two date values. Always use DateDiff() when comparing DateTime values otherwise they will be compared as string values.

Example
%%=DateDiff(1, 2, 3)=%%

<!-- Example Code -->
%%[
/**
* Date Diff Logic
* Used to return the difference between two date values.
* Always use the DateDiff() when comparing DateTime values otherwise they will be compared as strings.
*/
VAR @date1, @date2, @dateDiff_Days, @dateDiff_Months, @dateDiff_Years, @dateDiff_Minutes, @dateDiff_Hours
SET @date1 = "12/14/2021 9:00:00 AM"
SET @date2 = "12/14/2022 9:00:00 AM"
SET @dateDiff_Days = DateDiff(@date1,@date2, "d")
SET @dateDiff_Months = DateDiff(@date1,@date2, "m")
SET @dateDiff_Years = DateDiff(@date1,@date2, "y")
SET @dateDiff_Minutes = DateDiff(@date1,@date2, "mi")
SET @dateDiff_Hours = DateDiff(@date1,@date2, "h")
]%%
<p>Date 1: %%=v(@date1)=%%</p>
<p>Date 2: %%=v(@date2)=%%</p>
<p>Difference in Days: %%=v(@dateDiff_Days)=%%</p>
<p>Difference in Months: %%=v(@dateDiff_Months)=%%</p>
<p>Difference in Years: %%=v(@dateDiff_Years)=%%</p>
<p>Difference in Minutes: %%=v(@dateDiff_Minutes)=%%</p>
<p>Difference in Hours: %%=v(@dateDiff_Hours)=%%</p>
<!-- Example Output -->
Date 1: 12/14/2021 9:00:00 AM
Date 2: 12/14/2022 9:00:00 AM
Difference in Days: 365
Difference in Months: 12
Difference in Years: 1
Difference in Minutes: 525600
Difference in Hours: 8760
View this gist on GitHub

Date Parse Function

The DateParse() function is used to convert a string into a DateTime object. Date might be provided as a string in the incorrect format so DateParse() converts it into a DateTime object in the proper format.

Example
%%=DateParse(1, 2)=%%

<!-- Example Code -->
%%[
/**
* Date Parse Logic
* Used to convert a string into a DateTime object.
* Date might be provided as a string in the incorect format so DateParse() converts it into a DateTime object in the proper format.
*/
VAR @date1, @date2, @date3, @date4, @date5
SET @date1 = DateParse("12/14/2021 9:00:00 AM")
SET @date2 = DateParse("December 14, 2017 3:40PM",1)
SET @date3 = DateParse("22:11:44 12/14/2017")
SET @date4 = DateParse("14:22:33 14 December 2017")
]%%
<p>%%=v(@date1)=%%</p>
<p>%%=v(@date2)=%%</p>
<p>%%=v(@date3)=%%</p>
<p>%%=v(@date4)=%%</p>
<!-- Example Output -->
12/14/2021 9:00:00 AM
12/14/2017 9:40:00 PM
12/14/2017 10:11:44 PM
12/14/2017 2:22:33 PM
View this gist on GitHub

Date Part Function

The DatePart() function isolates specific elements of a date object. This function is useful when having to break apart a date to format it around specific business requirements. It’s especially helpful converting dates to support other languages.

Example
%%=DatePart(1, 2)=%%

<!-- Example Code -->
%%[
/**
* Date Part Logic
* Used to isolate specific elements of a date object.
*/
VAR @date, @date_day, @date_month, @date_year, @date_minutes, @date_hours
SET @date = "12/14/2021 9:30:00 AM"
SET @date_day = DatePart(@date,"d")
SET @date_month = DatePart(@date,"m")
SET @date_year = DatePart(@date,"y")
SET @date_minutes = DatePart(@date,"mi")
SET @date_hours = DatePart(@date,"h")
]%%
<p>Date: %%=v(@date)=%%</p>
<p>Day: %%=v(@date_day)=%%</p>
<p>Month: %%=v(@date_month)=%%</p>
<p>Year: %%=v(@date_year)=%%</p>
<p>Minutes: %%=v(@date_minutes)=%%</p>
<p>Hours: %%=v(@date_hours)=%%</p>
<!-- Example Output -->
Date: 12/14/2021 9:30:00 AM
Day: 14
Month: 12
Year: 2021
Minutes: 30
Hours: 9
View this gist on GitHub

Gallery

]]>
https://www.myampscript.com/ampscript-datetime-functions-beginner-level/feed/ 1
AMPscript Math Functions Beginner Level https://www.myampscript.com/ampscript-math-functions-beginner-level/ https://www.myampscript.com/ampscript-math-functions-beginner-level/#comments Fri, 10 Dec 2021 18:20:44 +0000 https://www.myampscript.com/?p=703 AMPscript Math Functions

The math functions below allow us to add, subtract, divide, and multiply numbers in Marketing Cloud Emails, Landing Pages, SMS/MMS Messages, and Push Notifications. You can write complex math equations to calculate numbers on the fly using these functions.

  • Add(N1,N2)
  • Subtract(N1, N2)
  • Multiply(N1, N2)
  • Divide(N1, N2)
  • Mod(N1, N2)

Add Function

The Add() function in AMPscript is used to add two numeric constants or two variables with a numeric value set.

Example
%%=Add(N1,N2)=%%

<!-- Example Code -->
%%[
/**
* Add Logic
* The Add() is used to add two numeric constants.
*/
VAR @number1, @number2, @results
SET @number1 = 1
SET @number2 = 2
SET @results = Add(@number1,@number2)
]%%
<p>%%=v(@results)=%%</p>
<!-- Example Output -->
3
View this gist on GitHub

Subtract Function

The Subtract() function in AMPscript is used to subtract two numeric constants or two variables with a numeric value set.

Example
%%=Subtract(N1,N2)=%%

<!-- Example Code -->
%%[
/**
* Subtract Logic
* The Subtract() is used to subtract two numeric constants.
*/
VAR @number1, @number2, @results
SET @number1 = 2
SET @number2 = 1
SET @results = Subtract(@number1,@number2)
]%%
<p>%%=v(@results)=%%</p>
<!-- Example Output -->
1
View this gist on GitHub

Multiply Function

The Multiply() function in AMPscript is used to multiply two numeric constants or two variables with a numeric value set.

Example
%%=Multiply(N1,N2)=%%

<!-- Example Code -->
%%[
/**
* Multiply Logic
* The Multiply() is used to multiply two numeric constants.
*/
VAR @number1, @number2, @results
SET @number1 = 2
SET @number2 = 2
SET @results = Multiply(@number1,@number2)
]%%
<p>%%=v(@results)=%%</p>
<!-- Example Output -->
4
View this gist on GitHub

Divide Function

The Divide() function in AMPscript is used to divide two numeric constants or two variables with a numeric value set.

Example
%%=Divide(N1,N2)=%%

<!-- Example Code -->
%%[
/**
* Divide Logic
* The Divide() is used to divide two numeric constants.
*/
VAR @number1, @number2, @results
SET @number1 = 10
SET @number2 = 5
SET @results = Divide(@number1,@number2)
]%%
<p>%%=v(@results)=%%</p>
<!-- Example Output -->
2
View this gist on GitHub

Mod Function

The Mod() function in AMPscript is used to find the remainder between two numeric constants or two variables with a numeric value set.

Example
%%=Mod(N1,N2)=%%

<!-- Example Code -->
%%[
/**
* Mod Logic
* The Mod() is used to find the remainder between two numeric constants.
*/
VAR @number1, @number2, @results
SET @number1 = 10
SET @number2 = 4
SET @results = Mod(@number1,@number2)
]%%
<p>%%=v(@results)=%%</p>
<!-- Example Output -->
2
View this gist on GitHub

Gallery

]]>
https://www.myampscript.com/ampscript-math-functions-beginner-level/feed/ 1
AMPscript Basics Part 2 https://www.myampscript.com/ampscript-basics-part-2/ https://www.myampscript.com/ampscript-basics-part-2/#comments Fri, 10 Dec 2021 14:23:23 +0000 https://www.myampscript.com/?p=684 AMPscript Functions

Functions are self contained blocks of code that perform specific tasks. Functions usually take in data, process it, and return a result. Learn more on the next slide. Functions are written like this: functionName(1)

Section functionName
This section is the unique name that describes the function.

Section ()
The values you insert into the parentheses is what the function evaluates.

Section 1
This section represents the parameters that can be used inside this function. Multiple parameters are separated by commas.


AMPscript Block Delimiters

To identify AMPscript, it must be surrounded by opening and closing delimiters or it will be ignored. AMPscript uses two types, Block Delimiters and Inline Delimiters. This section focuses on Block Delimiters. Block Delimiters are useful when you have several lines of AMPscript code. They can be placed anywhere in the email but most developers will include this at the top. This way AMPscript logic placed at the top of the email will cascade down the email when using it in the HTML markup.

<!-- Example Code -->
%%[
/* script goes here */
VAR @string
SET @string = "Hello World"
]%%
<p>%%=v(@string)=%%</p>
<!-- Example Output -->
Hello World
View this gist on GitHub

AMPscript Inline Delimiters

To identify AMPscript, it must be surrounded by opening and closing delimiters or it will be ignored. AMPscript uses two types, Block Delimiters and Inline Delimiters. This section focuses on Inline Delimiters. Inline Delimiters are useful when you need to call a function outside of an AMPscript block. You can do this by using the inline method. Simply insert %%= script goes here =%% into your HTML code.

<!-- Example Code -->
%%[
/* script goes here */
VAR @string
SET @string = "hello world"
]%%
<p>%%=Uppercase(@string)=%%</p>
<!-- Example Output -->
HELLO WORLD
View this gist on GitHub

AMPscript Comments

Every good developer knows why it’s important to add comments to your code. AMPscript comments are ignored by the application and do not show up in the final deployment. This differs from HTML comments. HTML comments can still be viewed after deployment by inspecting the source code. AMPscript comments are processed before the send and thus removed at the time of send. This can be useful if you want to ensure comments are absolutely hidden from subscribers.

<!-- Example Code -->
%%[
/* Insert Comment Here */
VAR @string1
SET @string1 = "Hello"
/**
* Insert Comment Here
*/
VAR @string2
SET @string2 = "Hello"
]%%
<p>%%=v(@string1)=%%</p>
<p>%%=v(@string2)=%%</p>
<!-- Example Output -->
Hello
World
View this gist on GitHub

Gallery

]]>
https://www.myampscript.com/ampscript-basics-part-2/feed/ 1
AMPscript Basics Part 1 https://www.myampscript.com/ampscript-basics-part-1/ https://www.myampscript.com/ampscript-basics-part-1/#comments Thu, 09 Dec 2021 00:17:03 +0000 https://www.myampscript.com/?p=614 What is AMPscript?

AMPscript is a Salesforce proprietary scripting language that interacts with Marketing Cloud emails, landing pages, SMS and MMS messages and push notifications. By learning the AMPscript language, you can do any of the following:

Can add personalized, unique content for each subscriber.
• Example Subject Line: Hello, First Name!

Display content based on certain conditions.
• Example Subject Line #1: Hello, First Name!
• Example Subject Line #2: Hello, Valued Customer!

Format Date Values
• Example Date: Date might provide a date as “MM/DD/YYYY” but we want it to show as “Month Day, Year” instead.


AMPscript Syntax

As mentioned, AMPscript is a scripting language, and when you learn any new language, you first have to master how it works. AMPscript uses the following langauge elements.

• Constants
• Attributes and Data Extension Values
• Variables (or Keywords)


AMPscript Constants

Numeric Values
Numeric constant values consist of one or more digits. They are formatted as unquoted numerals. They can include 1 decimal point and an introductory minus sign for negative numbers. Commas, are not permitted.

<!-- Example Code -->
%%[
/**
* Constant (Numeric Values)
* Numeric constant values consist of one or more digits. They are formatted as unquoted numerals. They can include 1 decimal point and an introductory minus sign for negative numbers. Commas, are not permitted.
*/
VAR @number1, @number2, @number3
SET @number1 = 123
SET @number2 = -123
SET @number3 = 123.456
]%%
%%=v(@number1)=%%<br>
%%=v(@number2)=%%<br>
%%=v(@number3)=%%
<!-- Example Output -->
123
-123
123.456
View this gist on GitHub

String Values
A string is a set of one or more letters, numbers, spaces or special characters. These constant values must be surrounded (delimited) by double or single quotes. They can also include the same type of delimiting quote inside by doubling it.

<!-- Example Code -->
%%[
/**
* Constant (String Values)
* A string is a set of one or more letters, numbers, spaces or special characters. These constant values must be surrounded (delimited) by double or single quotes. They can also include the same type of delimiting quote inside by doubling it.
*/
VAR @string1, @string2, @string3, @string4
SET @string1 = 'String text goes here'
SET @string2 = "String text goes here"
SET @string3 = "My string's inside quotes!"
SET @string4 = 'My string''s inside quotes!'
]%%
%%=v(@string1)=%%<br>
%%=v(@string2)=%%<br>
%%=v(@string3)=%%<br>
%%=v(@string4)=%%
<!-- Example Output -->
String text goes here
String text goes here
My string's inside quotes!
My string's inside quotes!
View this gist on GitHub

Boolean Values
Boolean constant values must use the words true or false. They are not surrounded by quotes. They are also NOT case sensitive.

<!-- Example Code -->
%%[
/**
* Constant (Boolean Values)
* Boolean constant values must use the words true or false. They are not surrounded by quotes. They are also NOT case sensitive.
*/
VAR @toggle1, @toggle2, @toggle3, @toggle4, @toggle5, @toggle6, @toggle7, @toggle8
SET @toggle1 = true
SET @toggle2 = false
SET @toggle3 = True
SET @toggle4 = False
SET @toggle5 = TRUE
SET @toggle6 = FALSE
SET @toggle7 = 1
SET @toggle8 = 0
]%%
%%=v(@toggle1)=%%<br>
%%=v(@toggle2)=%%<br>
%%=v(@toggle3)=%%<br>
%%=v(@toggle4)=%%<br>
%%=v(@toggle5)=%%<br>
%%=v(@toggle6)=%%<br>
%%=v(@toggle7)=%%<br>
%%=v(@toggle8)=%%<br>
<!-- Example Output -->
True
False
True
False
True
False
1
0
View this gist on GitHub

NULL Values
A NULL value represents the absence of a value. The use of a NULL value is just a pointer, which is pointing to nowhere in memory.

<!-- Example Code -->
%%[
/**
* Constant (Null Values)
* etaphysical.
*/
VAR @string1, @string2, @string3, @string4
SET @string1 = "String1 Copy Here"
SET @string2 = ""
SET @string3 = ''
SET @string4 = 'String4 Copy Here'
]%%
%%=v(@string1)=%%<br>
%%=v(@string2)=%%<br>
%%=v(@string3)=%%<br>
%%=v(@string4)=%%
<!-- Example Output -->
String1 Copy Here
String4 Copy Here
View this gist on GitHub

Attributes & Data Extension Values

Attributes and data extension values refer to any content held inside a subscriber list or a data extension. If the name includes a space or special character, you must surround it with opening and closing brackets. Best practice to always include the opening and closing brackets when using attributes. This adds consistency when troubleshooting.

Example Use Case
Let’s say you’re building an email and the data extension you’re sending to has two fields called “Birthdate” and “FirstName”. The example below demonstrates how you can pull the values from those fields to use within your email.

<!-- Example Code -->
%%[
/**
* Attributes & Data Extension Values
* Below is an example how you can use attributes to personalize your email.
*/
VAR @Birthdate, @FirstName
SET @Birthdate = [Birthdate]
SET @FirstName = [FirstName]
]%%
<p>Hi %%=v(@FirstName)=%%, your birthdate is %%=v(@Birthdate)=%%.</p>
<!-- Example Output -->
Hi John, your birthdate is 12/12/1985.
View this gist on GitHub

Variables (or Keywords)

A variable is a storage location for information you want to reference later on. A variable includes two parts, an identifier and a value. The Identifier is a symbolic name you want to use. The Value is the information you want to store.

Declaring Variables
When you declare a variable, you are telling the system that the variable exists. By default, whenever you declare a variable that variable will be set to NULL.
• Example: VAR @FullName

Setting Variables
When you’re ready to set a variable you can assign values to it.
• Example: SET @FullName = “John Doe”

<!-- Example Code -->
%%[
/**
* Declaring & Setting Variables
* Below is an example how to declare and set variables with AMPscript.
*/
VAR @FullName
SET @FullName = "John Doe"
]%%
<p>Hi %%=v(@FullName)=%%, your shipment has arrived!</p>
<!-- Example Output -->
Hi John Doe, your shipment has arrived!
View this gist on GitHub

Gallery

]]>
https://www.myampscript.com/ampscript-basics-part-1/feed/ 1
AMPscript String Functions Intermediate Level https://www.myampscript.com/ampscript-string-functions-intermediate/ https://www.myampscript.com/ampscript-string-functions-intermediate/#comments Tue, 07 Dec 2021 20:37:36 +0000 https://www.myampscript.com/?p=594 Overview

As we dive into more complex AMPscript logic we may come across situations where we need to manipulate string values to achieve different things. We can do this by using the following functions listed below:

Replace(1, 2, 3)
Length(1)
IndexOf(1, 2)
Substring(1, 2, 3)
Concat(1, 2)


Examples

Replace Function
There may be instances where a client is using dynamic email copy to deliver highly personalized emails to its users. The client realizes they did not update their data and accidently left references to 2007 in their dynamic email copy. To save the client time, we can dynamically convert 2007 to 2008 using the Replace function.

<!-- Example Code -->
%%[
/**
* Replace Logic
* Replaces 2007 with 2008 from the original string
*/
VAR @value1, @value2
SET @value1 = "The 2007 model is better"
SET @value2 = Replace(@value1,"2007","2008")
]%%
%%=v(@value2)=%%
<!-- Example Output -->
The 2008 model is better
View this gist on GitHub

Length Function
There may be an instance where the client wants to show the last four characters of a subscriber’s credit card number. In this situation, we would use the Length function to determine how many characters a subscriber’s credit card number is. Then you’d use Length in conjunction with the Substring function to show the last four digits.

<!-- Example Code -->
%%[
/**
* Length Logic
* The length function returns a number value
*/
VAR @value1, @value2
SET @value1 = "Hello World!"
SET @value2 = Length(@value1)
]%%
%%=v(@value2)=%%
<!-- Example Output -->
12
View this gist on GitHub

Substring Function
Returns the portion of the specified string starting with the specified character position and no longer than the specified length. There may be an instance where the client wants you to show a portion of the string value. In this case, you’d want to use Substring to handle that requirement.

<!-- Example Code -->
%%[
/**
* Substring Logic
* Only show two characters starting with the second character position
*/
VAR @value1, @value2
SET @value1 = "ABCDEF"
SET @value2 = Substring(@value1,2,2)
]%%
%%=v(@value2)=%%
<!-- Example Output -->
BC
View this gist on GitHub

Concat Function
Concatenates the strings specified in the arguments. Include as many values as necessary. It’s common for clients to ask us to append certain tracking variables to the end of a click thru URL. We can use the Concat function to simply attach query string variables to links.

<!-- Example Code -->
%%[
/*
* Concat Logic
* This combines three string values into a single string to show
*/
VAR @value1, @value2, @value3
SET @value1 = "This "
SET @value2 = "is "
SET @value3 = "cool"
SET @final = Concat(@value1,@value2,@value3)
]%%
%%=v(@final)=%%
<!-- Example Output -->
This is cool.
View this gist on GitHub

IndexOf Function
Sometimes clients ask us to hide and show email content based off keywords in their dynamic data. For example, the client sends a monthly newsletter to their subscribers. They want us to show a special coupon for premium users. Premium users are identified when the word premium is used in the cust_status field. The client wants you to show this special coupon when cust_status contains the word premium.

<!-- Example Code -->
%%[
/**
* IndexOf Logic
* Checks to see if the word Premium exists in the string value.
*/
VAR @cust_status, @cust_coupon
SET @cust_status = "Premium Retail Customer"
IF IndexOf(@cust_status,"Premium") > 0 THEN
SET @cust_coupon = "Show Premium Coupon"
ELSE
SET @cust_coupon = "Show Standard Coupon"
ENDIF
]%%
%%=v(@cust_coupon)=%%
<!-- Example Output -->
Show Premium Coupon
View this gist on GitHub

Gallery

]]>
https://www.myampscript.com/ampscript-string-functions-intermediate/feed/ 1
AMPscript String Functions Beginner Level https://www.myampscript.com/ampscript-string-functions-beginner/ https://www.myampscript.com/ampscript-string-functions-beginner/#comments Wed, 11 Mar 2020 03:00:03 +0000 http://localhost/mira/?p=33 Overview

As we dive into more complex AMPscript logic we may come across situations where we need to manipulate string values to achieve different things. We can do this by using the following functions listed below:

  • Lowercase(1)
  • Uppercase(1)
  • Propercase(1)
  • Trim(1)
  • Empty(1)

Examples

Lowercase Function
An example of when we might use this function is if a client wants us to render email copy in all lowercase letters. For example, let’s say that the client’s data contains values in in all uppercase letters. We can use the Lowercase function to convert the client’s data from uppercase to lowercase without having to manually update data in the data extension.

<!-- Example Code -->
%%[
/*
* Lowercase logic
* Converts the uppercase string to lowercase.
*/
VAR @name
SET @name = Lowercase("JOHN DOE")
]%%
%%=v(@name)=%%
<!-- Example Output -->
john doe
View this gist on GitHub

Uppercase Function
An example of when we might use this function is if a client wants us to render email copy in all uppercase letters. For example, let’s say that the client’s data contains values in all lowercase letters. We can use the Uppercase function to convert the client’s data from lowercase to uppercase without having to manually update data in the data extension.

<!-- Example Code -->
%%[
/*
* Uppercase logic
* Converts the lowercase string to uppercase.
*/
VAR @name
SET @name = Uppercase("john doe")
]%%
%%=v(@name)=%%
<!-- Example Output -->
JOHN DOE
View this gist on GitHub

Propercase Function
An example of when we might use this function is if a client wants us to render email copy using proper case formatting. For example, let’s say that the client’s data contains values in all uppercase letters. We can use the ProperCase function to convert the client’s data from uppercase to proper case without having to manually update data in the data extension.

<!-- Example Code -->
%%[
/*
* Propercase logic
* Converts any case to propercase.
*/
VAR @name
SET @name = Uppercase("jOhN dOe")
]%%
%%=v(@name)=%%
<!-- Example Output -->
John Doe
View this gist on GitHub

Trim Function
There may be instances where the client provides data with undesired spaces. An example might be if the client has a field in the sendable data extension called fname. The client accidently uses “  John  ” in that field rather than “John”. In order to clean this up prior to using it in the email we use the Trim function to strip out unwanted spaces before and after the data value.

<!-- Example Code -->
%%[
/*
* Trim Logic
* Removes the white space before and after the text.
*/
VAR @value1, @value2
SET @value1 = " Hello World "
SET @value2 = Trim(@value1)
]%%
%%=v(@value2)=%%
<!-- Example Output -->
Hello World
View this gist on GitHub

Empty Function
This function is used often to evaluate whether a variable is empty or not. You’ll notice that this function is combined with other functions to evaluate and personalize content. In this example, we check to see if @name is empty or not. If so, then set the @name variable to “Valued Customer” to use as a fallback.

<!-- Example Code -->
%%[
/*
* Empty logic
* If subscriber FirstName exists then set the name var to FirstName.
* If subscriber FirstName does not exist, then set the name var to Valued Customer.
*/
VAR @name
SET @name = ""
IF NOT EMPTY(@name) THEN
SET @name = ProperCase(@name)
ELSE
SET @name = "Valued Customer"
ENDIF
]%%
%%=v(@name)=%%
<!-- Example Output -->
Valued Customer
View this gist on GitHub

Gallery

]]>
https://www.myampscript.com/ampscript-string-functions-beginner/feed/ 1