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

Practice Using AMPscript

Learning Objectives

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

  • Write basic AMPscript code.
  • Test your knowledge of AMPscript.

Practice AMPScript Using the Trailhead Simulator

Now that you understand why you would use AMPscript, along with some of the key functions, we want to give you some practice using AMPscript.

Before you get started, familiarize yourself with the Trailhead Simulator for Marketing Cloud Engagement. You use the simulator instead of a Trailhead Playground to complete the steps in this project. 

The Trailhead Simulator is different from a Trailhead Playground. The simulator doesn’t store your progress or any data you enter. If you close your browser, you start from the beginning of the simulation again. You can always use the navigation controls at the bottom of the simulator to get back to where you left off. 

Simulator navigation controls callouts.

Navigation Controls
Description
(1) Left Arrow
Go backward in the simulator.
(2) Right Arrow
Go forward in the simulator.
(3) Scrub Bar
View your progress, plus you can use the progress arrow to quickly move to a different location within the simulator.
(4) Close Button
Exit the simulator. Remember that if you close the simulator, you start from the beginning the next time you launch it.

AMPscript Exercises

Now that you are familiar with the simulator, let’s get started! There are three exercises for you to complete using the simulator.

Exercise 1:

Most marketers know of at least one field in their data that is never entered correctly or is in the incorrect format. Let’s use AMPscript to fix inconsistencies or minor errors in your data.  

  1. Launch the Trailhead Simulator.
  2. Click Exercise 1: Use AMPscript to Fix Capitalization, and then click Begin.
  3. From the Marketing Cloud Engagement Apps bar, click Content Builder and select Content Builder.
  4. Under Local Folders, click Simulator.
  5. Click the email named Welcome to Cloud Kicks.
  6. Click Edit and select Edit Content.
  7. Click the Preview and Test tab.
  8. Click Recipient list folder icon., expand the Data Extensions folder, and then expand the Simulator folder.
  9. Select the data extension, Sample_AMPscript.
  10. Click the line entry for MemberID 345678 (Tyler Oliver), and then click Select. Review the recipient information and notice:
    • The last name renders with no capitalization.
  11. Next, click the Content tab.
  12. Hover over the Code Snippet section of the email, and then click the grey Code Snippet (Click to edit) area.
  13. Locate  %%=v(@lname)=%%and replace with %%=ProperCase(@lname)=%% and then Enter.
  14. Click Done Editing.
  15. Return to Preview and Test and view the updated last name.

Voila! The customer's last name is now in the correct case.

Exercise 2: 

Now for another handy exercise. It’s February and you realize that your copyright year hasn’t been updated for your infrequently updated transactional messages. Let’s use inline AMPscript to update a copyright line for your email footers. This simulation shows a specific date, but this AMPscript string always shows the current date when used in your content.

  1. Click Next Exercise and then Begin.
  2. Click Back button iconto return to the Content Builder screen.
  3. Under Local Folders, click Simulator.
  4. Click Create, select Content Blocks, and then select Free Form.
  5. Click inside the body of the Content Window and enter Copyright: and today’s year in yyyy format, then press Enter.
  6. On the next line, enter: Copyright: %%=Now()=%% and Enter.
  7. On the next line, enter: Copyright: %%=Format(Now(), "yyyy")=%% and Enter.
  8. Click Save Save pulldown menu icon, and then click Save and Exit .
  9. In the Name field, enter Copyright Year Sample, then press Enter.
  10. Click Save.
  11. From the list of emails, click Cloud Kicks Loyalty.
  12. Click Edit and select Edit Content.
  13. Click the Content tab.
  14. From the Content section, drag Copyright Year Sample to the bottom of the Content Block and click Done Editing .
  15. Select Preview and Test to view your results.
  16. Click Save.

Never have an outdated copyright date again!

Exercise 3: 

In the next scenario, we focus on creating a personalized image based on a customer’s preference, in this case for their preferred sport. Let’s walk through how to use variables and the lookup function to load and display content stored in a data extension.

  1. Click Next Exercise and then Begin.
  2. From the list of data extensions, click Exercise_Image.
  3. Review the info on the Properties tab and notice the names under Fields.
  4. Click the Records tab and review the records to see how the data maps between Hero_Image and PrefSport.
  5. Now, navigate to the Content tab, and then click the Simulator folder.
  6. Click the template-based email CloudKicks Preferred Sport.
  7. Click Edit and select Edit Content.
  8. In the email, hover over the Code Snippet section of the email, and then click once to edit. Edit Code snippet screenshot
  9. In the Code Snippet window, select the portion of code that looks up data from the data extension, then click Done Editing.
  10. Next, click Preview and Test.
  11. Click Recipient Folder iconto view the list of recipients.
  12. Click Data Extensions, the Simulator folder, and then click Sample_AMPscript.
  13. Click the entry with MemberID 345678, FirstName Tyler and then click Select.
  14. Review the recipient information and notice:
    • The name in the email salutation is Tyler, derived from the FirstName value.
    • The image is the running image and corresponds to the Prefsport value of running.
  15. Click Next Recipient iconto view the next recipient.
  16. Review the recipient information and notice:
    • The name in the email salutation is brienne (notice the first name in the data extension isn't capitalized), derived from the FirstName value.
    • The image is the horseback riding image and corresponds to the Prefsport value of horseback riding.
  17. Click Next Recipient iconto view the third recipient.
  18. Review the recipient information and notice:
    • The name in the email salutation is Jamie, derived from the FirstName value.
    • The image is of weight lifting and corresponds to the Prefsport value of weight lifting.
  19. Click Save.
  20. To stay in the Simulator and retry the exercises, click Home. Otherwise, click Exit to exit the Simulator.

The images for the three recipients change because the lookup code Lookup("Exercise_Image","Hero_Image","PrefSport",@PrefSport), finds a specific image to be displayed for a user based on the data extension identified (hint: the  Hero_Image and PrefSport fields we previously reviewed).

Congratulations! Even if you don't feel fluent in AMPscript quite yet, you now have the basics to go out on your own and explore. Experiment with AMPscript in your email communications and then if you are inspired to learn more, delve into our developer documentation to gain even further knowledge. Happy AMPscripting!  

Resources

在 Salesforce 帮助中分享 Trailhead 反馈

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

了解更多 继续分享反馈