TransWikia.com

AMPScript IF statement or Loop?

Salesforce Asked by Bryce Mi on December 12, 2021

I am hoping to get some help from you guys. We have a Scenario that we wan to send different content to people who attended our Web_Event. In this case, we have Event A, Event B, and Event C. For some reason that we have three data extensions for all the information. First Data extension (Send) contents Subscriber key, first name, and email address. The second one (Event) contains Event ID, Email address, and First name, Third one (Event detail) contains Event ID and Event Name. The issue we are facing is, we tried to show different content based on which event they attended, but for people that attended two or more events, we will send them combined content( Fro example, if a person watched Event A, show (1), Event B, show (2), or Event A and B, show 1 and 2.)

Here is the code we have but we are not able to combine IF statements, if there any other ways to do this?

%%[ 

var @Subkey, @EmailAddress, @FirstName, @Event_ID, @Event_Name 

Set @Subkey = Subscriberkey 
Set @EmailAddress = EmailAddress
Set @FirstName = FirstName
Set @Event_ID = Lookup('event','Event_ID','Email_address',@EmailAddress)
Set @Event_name = Lookup('Event detail', 'Event_Name', 'Event_ID', @Event_ID)

]%%

Hello %%= v(@FirstName)=%%<br>
Thansk for watching %%= v(@Event_Name)=%%<br>

Here are more content you might be interested in<br>

%%[IF @Event_ID == '12345' THEN ]%%

    THANKS FOR WATCHING WEBINAR A 

%%[ELSEIF @Event_ID == '54321' THEN ]%%

    THANKS FOR WATCHING WEBINAR B 

%%[ELSE]%%

    THANKS FOR WATCHING WEBINAR C
                
    Thank you 

%%[ENDIF]%%

2 Answers

General pointer: Don't use IF / ELSEIF / ELSE here. This will only process one leg of the IF-statement so you cannot combine multiple. In other words: It's "either/or".

Solution 1)

You don't need "either/or", you need: "maybe A and maybe B and maybe C"

So: Just chain IFs.

Translated to pseudocode:

IF A THEN ... ENDIF; 
IF B THEN ...ENDIF; 
IF C THEN ...ENDIF;

Very simple, but very dumb, not elegant at all, hard to scale.


var @Subkey, @EmailAddress, @FirstName, @Event_ID, @Event_Name 

Set @Subkey = Subscriberkey 
Set @EmailAddress = EmailAddress
Set @FirstName = FirstName
Set @Event_ID = Lookup('event','Event_ID','Email_address',@EmailAddress)
Set @Event_name = Lookup('Event detail', 'Event_Name', 'Event_ID', @Event_ID)

]%%

Hello %%= v(@FirstName)=%%<br>
Thansk for watching %%= v(@Event_Name)=%%<br>

Here are more content you might be interested in<br>

%%[
IF @Event_ID == '12345' THEN 
]%%

    THANKS FOR WATCHING WEBINAR A 

%%[
ENDIF
IF @Event_ID == '54321' THEN 
]%%

    THANKS FOR WATCHING WEBINAR B 

%%[
ENDIF
IF @Event_ID == 'webinarC' THEN ]%%

    THANKS FOR WATCHING WEBINAR C
                
    Thank you 

%%[
ENDIF
]%%

Solution 2:

for loop. The "right" way to do this, short and concise, scales automatically with more records per person in your "event" table.

%%[ 
SET @EmailAddress = EmailAddress
Set @allAttendedEventsAsRowset = LookupRows('event','Email_address',@EmailAddress)
SET @rowCount = rowcount(@allAttendedEventsAsRowset)
 
IF @rowCount > 0 THEN
 
  FOR @i = 1 TO @rowCount DO
 
    SET @row = row(@allAttendedEventsAsRowset,@i) /*get row based on loop counter */
    SET @eventID = field(@row,'Event_ID')
    SET @eventName = Lookup('Event detail', 'Event_Name', 'Event_ID', @eventID)

]%%

THANKS FOR WATCHING %%=v(@eventName)=%%

%%[
 
  NEXT @i
 
ENDIF
]%% 

Code is from memory. Should work, but no guarantee. Hope it helps!

Answered by Jonas Lamberty on December 12, 2021

I have slightly updated conditions that you have used, you can try out the below script and if it works make sure to mark as answered so this might help someone down the road.

%%[ var @Subkey, @EmailAddress, @FirstName, @Event_ID, @Event_Name

Set @Subkey =Subscriberkey 
Set @EmailAddress = EmailAddress 
Set @FirstName = FirstName 
Set @Event_ID = Lookup('event','Event_ID','Email_address',@EmailAddress) 
Set @Event_name = Lookup('Event detail', 'Event_Name', 'Event_ID', @Event_ID)

]%%


Hello %%= v(@FirstName)=%%
Thansk for watching %%= v(@Event_Name)=%%

Here are more content you might be interested in

%%[IF @Event_ID == '12345' THEN]%% THANKS FOR WATCHING WEBINAR A

%%[ELSEIF @Event_ID == '54321' THEN]%% THANKS FOR WATCHING WEBINAR B

%%[ELSEIF @Event_ID != '12345' or @Event_ID != '54321' THEN]%% THANKS FOR WATCHING WEBINAR C

%%[ENDIF]%%

Answered by Vinoth Kumar on December 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP