Skip to main content
Build the future with Agentforce at TDX in San Francisco or on Salesforce+ on March 5–6. Register now.

Create a Loyalty Email

Learning Objectives

After completing this unit, you’ll be able to:

  • Code a loyalty email using math functions.
  • Display content using the IndexOf function.

Solution 2: Create a Loyalty Email

Now that the birthday email is set, Michele moves on to her next task: creating a loyalty rewards email. Here are the content and requirements Michele has been given. 

Email Content:

Hi, FIRST NAME! You’re so close to your next reward! You’ve earned XX% of the points needed to reach your next reward. Hurry! Earn just XX points to earn your next reward.

Requirements:

  • Display NTO Rockstar if a customer’s first name is null.
  • Show 150 as the total points needed to earn a reward.
  • Use variables from the NTO Customer data extension with field names:
    • First-Name
    • Points_Earned

Here’s how Michele sets up the solution.

Math and Formatting Functions

Michele knows she needs to use math functions to get the percentage and the number of points needed to earn a reward. It’s a good idea to think about math functions from the inside out, as math functions can be grouped using parentheses. The innermost functions are completed first. 

Also, using the format function is a good way to display numbers in the desired way, such as percentages. Here are a few formatting string values that come in handy when doing math functions.

Formatting String

What It Displays

Example

P or  

Shows percentage

For a value of .854, displays 85.4%

C or c

Displays as a currency

For a value of .854, displays $0.85

N or n

A rounded number 

For a value of .854, displays .85

To get the percentage, Michele knows she must divide points by points needed. To convert that number to a percentage, she uses the FormatNumber function and the pattern P, to display a number with a percentage: FormatNumber(Divide(@points,@ptsneeded),"P")

For reference, here’s the full code she builds.

Example Code

%%[
/* Get data from customer DE and set as variables */
VAR @firstName, @points, @earnedPoints
SET @firstName = [First-Name]
IF Empty(@firstName) THEN SET @firstName = "NTO Rockstar" ENDIF
SET @points = [Points_Earned]
/* Set math variables */
SET @ptsneeded = 150
SET @earnedpoints = FormatNumber(Divide(@points,@ptsneeded),"P")
SET @nextreward = Subtract(@ptsneeded,@points)
]%%
Hi, %%=ProperCase(@firstname)=%%!
You’re so close to your next reward! You’ve earned %%=v(@earnedpoints)=%% of the points needed to reach your next reward. Hurry! Earn just %%=v(@nextreward)=%% points to earn your next reward. 

Solution 3: Create a Premium Reward Email

Now that a loyalty email is set, Michele moves on to her next task: updating an existing loyalty newsletter to include a special promo for customers. Here are the email’s content and requirements.

Email Content:

No changes need to be made to the email content. The only change is displaying a new content block graphic. 

Requirements:

  • When a customer’s status is premium, show the premium coupon graphic above the main banner image.
    • Display Special Coupon Graphic:
      <img src="http://fpoimg.com/640x100?text=Special%20Coupon%20For%20Premium%20Users"border="0"width="640" style="display:block;">
  • Use variables from the NTO Customer data extension with field name:
    • cust_status

Use Strings

To do this, Michele decides to use conditional logic with the IndexOf string function. As mentioned in the previous unit, this string can be used to hide or show content based on keywords in data. Michele wants to see if the keyword premium is found in the customer data extension to determine if she should show the special graphic. To do this, she uses this specific code:  IF IndexOf(@status,"premium")>0 THEN

Let’s review how she codes the full email.

%%[
/* Search data from customer DE and set variables */
VAR @status
SET @status = [Cust_status]
IF IndexOf(@status,"premium")>0 THEN
]%%
<tr>
<td align="left">
<img alt="premium coupon"width="640" src="http://fpoimg.com/640x100?text=Special%20Coupon%20For%20Premium%20Users" style="display:block" border="0">
</td>
</tr>
%%[
/*If not premium, just show hero image */
ELSE
]%%
<tr>
<td align="left">
<img alt="people standing" width="100%" src="http://image.s6.exacttarget.com/lib/fe9012747463007e73/m/1/demo-shared-header.jpg" style="display: block" border="0">
</td>
</tr>
%%[
ENDIF
]%%

With those two solutions in place, we are ready to move on to another function of AMPscript: data retrieval. 

Resources

在 Salesforce 帮助中分享 Trailhead 反馈

我们很想听听您使用 Trailhead 的经验——您现在可以随时从 Salesforce 帮助网站访问新的反馈表单。

了解更多 继续分享反馈