---
schema_version: "1"
id: 02047dcb5debf436aed882a3d20db9faef6d4f6d046eb40a1ded7fa11cb8759c
title: Get Started with AMPscript Development—Lesson 3
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/marketing-cloud/ampscript/guide/get-started/mc-ampscript-get-started-learning-lesson-3.md
canonical_url: https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/guide/mc-ampscript-get-started-learning-lesson-3.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: ab63e1c56474fed050b3ea2d8cb194dd4ece720c488e709637b81676ad7dcbc5
products:
  - AMPscript for Marketing Cloud
  - Marketing Cloud
---
# Get Started with AMPscript Development—Lesson 3

This lesson builds on [Lesson 2](https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/guide/mc-ampscript-get-started-learning-lesson-2.md) by adding this 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 Name | Description                                                                    |
| -------------------------- | ------------------------------------------------------------------------------ |
| `PostalCode`               | A postal code                                                                  |
| `City`                     | The city associated with the postal code                                       |
| `State`                    | The state, province, or other subnational unit associated with the postal code |
| `ShipTime`                 | The 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.

## Message Content

This code example contains the message content for this lesson.

```
%%[
  Var @memid,
      @fname,
      @lname,
      @prefname,
      @address,
      @city,
      @state,
      @postcode,
      @mempref,
      @plat,
      @rows,
      @row,
      @ship

  Set @memid = MemberID
  Set @fname = FirstName
  Set @lname = LastName
  Set @prefname = PrefName
  Set @address = Address
  Set @postcode = PostalCode
  Set @mempref = MemPref
  Set @plat = Plat
]%%

%%[ if not empty(@prefname) then ]%%
  %%= v(@prefname) =%%
%%[ else ]%%
  %%= v(@fname) =%%
%%[ endif ]%%, below are your account details

<table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr>
    <td height="30">First Name: %%= v(@fname) =%%</td>
    <td height="30">Last Name: %%= v(@lname) =%%</td>
  </tr>
  %%[ if not Empty(@prefname) then ]%%
  <tr>
    <td height="30">Preferred Name: %%= v(@prefname) =%%</td>
    <td></td>
  </tr>
  %%[ endif ]%%
  <tr>
    <td height="30">Address: %%= v(@address) =%%</td>

    /* Use the postal code to look up the city, state, and shipping time. */
    %%[
    If not Empty(@postcode) then

      /* Find the postal code in the ShipTimes Data Extension */
      Set @rows = LookupRows("ShipTimes","PostalCode",@postcode)

      /* Make sure exactly one row of data was found */
      if RowCount(@rows) == 1 then

        /* Set the row - This now exposes all columns to our variable */
        Set @row = Row(@rows,1)

        /* Set the City */
        Set @city = Field(@row,"city")

        /* Set the State */
        Set @state = Field(@row,"state")

        /* Set the @ship variable to the ShipTime column */
        Set @ship = Field(@row,"ShipTime")

      EndIf
    EndIf ]%%

    <td height="30">City: %%= v(@city) =%%</td>
  </tr>
  <tr>
    <td height="30">State: %%= v(@state) =%%</td>
    <td height="30">Postal Code: %%= v(@postcode) =%%</td>
  </tr>
  <tr>
    <td height="30">
      %%[ If not Empty(@ship) then ]%%
      Shipping Time: %%= v(@ship) =%%
      %%[ If @ship > 1 then ]%%
      days
      %%[ Else ]%%
      day
      %%[ EndIf ]%%
      %%[ EndIf ]%%
    </td>
    <td height="30">&nbsp;</td>
  </tr>
  <tr>
    <td height="30">Shopping Preference: %%= v(@mempref) =%%</td>
    <td height="30">Platinum Member:
      %%[ if @plat == "False" then ]%%
      <a href="%%= RedirectTo(Concat('http://example.com?s=',_subscriberkey)) =%%">
        See if you're eligible
      </a>
      %%[ Else ]%%
      %%= v(@plat) =%%
      %%[ EndIf ]%%
    </td>
  </tr>
</table>
```

## Data Extension Lookup

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()](/docs/marketing/marketing-cloud-ampscript/references/mc-ampscript-data-extension/mc-ampscript-reference-data-extension-lookup-rows.html) 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 rows that meet these criteria. The code example saves this data in the variable `@rows`.

```
%%[
  If not Empty(@postcode) then

    /* Find the postal code in the ShipTimes Data Extension */
    Set @rows = LookupRows("ShipTimes","PostalCode",@postcode)
```

## Count the Number of Matching Rows from the Data Extension

We assume that only one row in the data extension contains the specific postal code value. We use the [RowCount()](/docs/marketing/marketing-cloud-ampscript/references/mc-ampscript-data-extension/mc-ampscript-reference-data-extension-row-count.html) function to make sure that the variable only contains a single row of data.

```
    /* Make sure exactly one row of data was found */
    if RowCount(@rows) == 1 then
```

## Parse Data from the Matching Row

If the `RowCount()` function confirms that the `@rows` variable contains a single row of data, we set the variable `@row` to equal that row.

```
      /* Set the row - This now exposes all columns to our variable */
      Set @row = Row(@rows,1)
```

Now that we have a single row of data, we use the [Field()](/docs/marketing/marketing-cloud-ampscript/references/mc-ampscript-data-extension/mc-ampscript-reference-data-extension-field.html) 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.

```
      /* Set the City */
      Set @city = Field(@row,"city")

      /* Set the State */
      Set @state = Field(@row,"state")

      /* Set the @ship variable to the ShipTime column */
      Set @ship = Field(@row,"ShipTime")

EndIf ]%%
```
