Get Started with AMPscript Development—Lesson 3

This lesson builds on Lesson 2. It explores additional, more advanced functionality.

  • Looking up content in a data extension
  • Using information from the row to populate multiple fields in the email
  • Using error checking to handle unexpected data

In addition to the sendable data extension that we used in previous lessons, this lesson refers to a data extension called ShipTime, which contains the columns in this table.

Data Extension Column NameDescription
PostalCodeA postal code
CityThe city associated with the postal code
StateThe state, province, or other subnational unit associated with the postal code
ShipTimeThe amount of time required to ship items to the postal code

Our fictitious company, Northern Trail Outfitters (NTO), uses this data extension to look up each subscriber’s city and other location details based on their postal code. The company also uses this data to provide subscribers with an estimated shipping time based on their location.

This code example contains the message content for this lesson.

The block of AMPscript code under the Address field uses the @postcode variable to find the subscriber’s city, state, and estimated shipping time. It performs this lookup using the LookupRows() function to find the row in the ShipTimes data extension, for which the value in the PostalCode column equals the value of the @postcode variable. The LookupRows() function returns all of the rows that meet these criteria. The code example saves this data in the variable @rows.

We assume that only one row in the data extension contains the specific postal code value. We use the RowCount() function to make sure that the variable only contains a single row of data.

If the RowCount() function confirms that the @rows variable contains a single row of data, we set the variable @row to equal that row.

Now that we have a single row of data, we use the Field() function to parse the data fields from that row. We set the value of the @city variable to equal the value found in the city column of the matching row and the @state value to equal the value in the state column. We also set a new variable, @ship, to equal the value found in the ShipTime column.