AMPscript 301

このシナリオには、AMPscript の使用に関する次の情報が含まれます。

  • 一意の行に基づいたコンテンツの検索
  • 行情報を使用したメール内の複数のフィールドへの入力
  • AMPscript で予期せぬデータが検出された場合のエラーチェックの設定

次の AMPscript は、メールメッセージ全体のコンテンツを提供します。

1%%[/* 301 */
2
3Var @memid, @fname, @lname, @prefname, @address, @city, @state, @zip, @mempref, @plat, @rows, @row, @ship
4
5Set @memid = MemberID
6Set @fname = FirstName
7Set @lname = LastName
8Set @prefname = PrefName
9Set @address = Address
10Set @zip = Zip
11Set @mempref = MemPref
12Set @plat = Plat
13
14/* 301 */ ]%%
15
16<div style="border-bottom: 1px solid #EEE; padding: 15px 0;">**%%[ if not empty(@prefname) then ]%%%%= v(@prefname) =%%%%[ else ]%%%%= v(@fname) =%%%%[ endif ]%%**, below are your account details</div>
17<div style="border-bottom: 1px solid #EEE; padding: 15px 0;">
18<table width="100%" cellpadding="0" cellspacing="0" border="0">
19    <tr>
20        <td height="15" valign="bottom"><font size="2" color="#444444">**Name**</font></td>
21        <td>&nbsp;</td>
22    </tr>
23    <tr>
24        <td height="30"><font size="2">First Name: **%%= v(@fname) =%%**</font></td>
25        <td height="30"><font size="2">Last Name: **%%= v(@lname) =%%**</font></td>
26    </tr>
27    %%[ if not Empty(@prefname) then ]%%
28    <tr>
29        <td height="30"><font size="2">Preferred Name: **%%= v(@prefname) =%%**</font></td>
30        <td></td>
31    </tr>
32%%[ endif ]%%
33    <tr>
34        <td height="30" valign="bottom"><font size="2" color="#444444">**Address**</font></td>
35        <td>&nbsp;</td>
36    </tr>
37    <tr>
38        <td height="30"><font size="2">Address: **%%= v(@address) =%%**</font></td>
39%%[ /* We have a zip code to look up on */
40
41if Not Empty(@zip) then
42
43/* Lets find our zip code in the ShipTimes Data Extension */
44Set @rows = LookupRows("ShipTimes","zip",@zip)
45
46/* We found one row of data */
47if RowCount(@rows) == 1 then
48
49/* Set the row - This now exposes all columns to our variable */
50Set @row = Row(@rows,1)
51
52/* Set the City */
53Set @city = Field(@row,"city")
54
55/* Set the State */
56Set @state = Field(@row,"state")
57
58/* Set the @ship variable to the ShipTime column */
59Set @ship = Field(@row,"ShipTime")
60
61endif
62
63endif ]%%
64
65        <td height="30"><font size="2">City: **%%= v(@city) =%%**</font></td>
66    </tr>
67    <tr>
68        <td height="30"><font size="2">State: **%%= v(@state) =%%**</font></td>
69        <td height="30"><font size="2">Zip: **%%= v(@zip) =%%**</font></td>
70    </tr>
71    <tr>
72        <td height="30"><font size="2">
73%%[ /* We found a record for the zip code */
74
75if not empty(@ship) then ]%%
76
77Shipping Time: **%%= v(@ship) =%% %%[ if @ship > 1 then ]%%days%%[ else ]%%day%%[ endif ]%%**
78
79%%[ /* We either didn't have a zip code or couldn't find one, prompt for profile update */
80
81else ]%%
82
83We couldn't find a shipping preference. Please <a href="%%= RedirectTo(Concat('http://example.com?s=',_subscriberkey)) =%%" title="Update Your Profile" alias="Update Your Profile">update your profile</a>
84
85%%[ endif ]%%</font></td>
86
87        <td height="30"><font size="2">&nbsp;</td>
88    </tr>
89    <tr>
90        <td height="30" valign="bottom"><font size="2" color="#444444">**Member Preferences**</font></td>
91        <td>&nbsp;</td>
92    </tr>
93    <tr>
94        <td height="30"><font size="2">Shopping Preference: **%%= v(@mempref) =%%**</font></td>
95        <td height="30"><font size="2">Platinum Member: <strong style="color: #000;">%%[ if @plat == "N" then ]%%<a href="%%= RedirectTo(Concat('http://example.com?s=',_subscriberkey)) =%%" alias="Platinum Sign-Up">Sign-Up</a>%%[ else ]%%%%= v(@plat) =%%%%[ endif ]%%</strong></font></td>
96    </tr>
97</table>
98</div>

この AMPscript コードの大部分は AMPscript 201 にあるコードと似ていますが、1 つの新しいセクションは AMPscript がデータ内の潜在的エラーを検出し、不正確な情報が表示されるのを防ぐために役立ちます。また、この AMPscript はオンラインプロファイルに移動して不正確な郵便番号や不足している郵便番号情報を更新するように購読者に促すことで、送信者を支援します。

1Address:%%= v(@address) =%%
2%%[ /* We have a zip code to look up on */
3if Not Empty(@zip) then
4/* Lets find our zip code in the ShipTimes Data Extension */
5Set @rows = LookupRows("ShipTimes","zip",@zip)
6/* We found one row of data */
7if RowCount(@rows) == 1 then
8/* Set the row - This now exposes all columns to our variable */
9Set @row = Row(@rows,1)
10/* Set the City */
11Set @city = Field(@row,"city")
12/* Set the State */
13Set @state = Field(@row,"state")
14/* Set the @ship variable to the ShipTime column */
15Set @ship = Field(@row,"ShipTime")
16endif
17endif ]%%City:%%= v(@city) =%%
18State:%%= v(@state) =%%Zip:%%= v(@zip) =%%
19%%[ /* We found a record for the zip code */
20if not empty(@ship) then ]%%
21Shipping Time:%%= v(@ship) =%%%%[ if @ship > 1 then ]%%days%%[ else ]%%day%%[ endif ]%%
22%%[ /* We either didn't have a zip code or couldn't find one, prompt for profile update */
23else ]%% We couldn't find a shipping preference. Please
24%%= RedirectTo(Concat('http://example.com?s=',_subscriberkey)) =%%
25update your profile

Address フィールドのすぐ下のコードは @zip 変数を取り、正しい情報を挿入するコードが続きます。そのコードは ShipTimes データエクステンションを参照し、関数は @zip 変数に含まれる値と一致する郵便番号値が含まれる 1 行を検索します。

1Set @rows = LookupRows("ShipTimes","zip",@zip)

1 行のみに郵便番号値が含まれる必要があるため、このコードは RowCount() 関数が 1 の値を検出した場合にのみ有効になります。

1if RowCount(@rows) == 1 then

それ以外の場合、ユーザーに情報を更新するように要求します。

1else ]%% We couldn't find a shipping preference. Please
2%%= RedirectTo(Concat('http://example.com?s=',_subscriberkey)) =%%
3update your profile

1 行のみを検出した場合、その行を選択してデータを取得します。

1Set @row = Row(@rows,1)

行が確定したら、Field() 関数を使用して、@city, @state, @zip 変数に値を割り当てます。

1/* Set the City */
2Set @city = Field(@row,"city")
3/* Set the State */
4Set @state = Field(@row,"state")
5/* Set the @ship variable to the ShipTime column */
6Set @ship = Field(@row,"ShipTime")

これらの値はメール送信にも出力されます。

1%%City:%%= v(@city) =%%
2State:%%= v(@state) =%%Zip:%%= v(@zip) =%%

選択可能な配送時間値もメール送信に表示されます。

1%%[ /* We found a record for the zip code */
2if not empty(@ship) then ]%%
3Shipping Time:%%= v(@ship) =%%%%[ if @ship > 1 then ]%%days%%[ else ]%%day%%[ endif ]%%

最後の 1 つの if/else ステートメントは、@ship 変数の数値に応じてメッセージに単語 day と days のどちらを含めるかを判断します。