TransWikia.com

AMPscript loop function Question

Salesforce Asked on January 4, 2022

Thanks for taking your time to review my question. We have a complex situation that we need to use ampscript to display different content based on the webinar attendance. The data is stored in Three data extensions named "audience"(send data),"event(event_ID and Email)","Webinar names(Event_ID,EVent_name)". We have our var set and used "IF" statement for different content display.
The issue is when certain audience who has multiple IDs(watched multiple webinar), we are not able to us "IF"statement to display multiple content. I am just wondering if we can achieve this with LOOP function ?

Our desired email would be:
Hello %%=v(@firstname)%%
Thanks for watching %%=v(@event_name)%%(multiple)
Here is more content you might like (Multiple based on the event.

Here’s the code we have so far `

%%[ var @subkey, @Fname, @email, @event_id, @rows, @row, @rowCount, @subKey, @counter, @Event_name

set @subKey = subscriberkey
set @Fname = FirstName
set @email = EmailAddress
set @event_id = lookup("event","Event_ID","Email_address", @email)
set @Event_name = Lookup("Webinar names", "Event_Name", "Event_ID", @event_id) 
]%%`



%%[IF @event_id != "54321" and @event_id != '135135'  THEN]%%
CONTENT FOR A 
  %%[ENDIF]%%              
  <br>

  %%[IF @event_id != "12345" and @event_id != '135135'  THEN]%%
CONTENT FOR B 
  %%[ENDIF]%% 
  <br>


  %%[IF @event_id != "12345" and @event_id != '54321'  THEN]%%
CONTENT FOR C 
  %%[ENDIF]%% 

One Answer

Here's how I'd approach this -- loop twice through the subscriber's events with the first loop qualifying the events with a matching event_name and looping again to build the output string.

In the recommendation section, you can use the indexOf() function to determine if a person has attended a particular series of events or not and then output the content that's in line with your requirements.

%%[ 
    set @debug = 1


    /* pull email address from system string */
    set @EmailAddress = AttributeValue("emailaddr")
    set @firstName = AttributeValue("FirstName")
    set @attendedEvents = LookupRows('event','Email_address', @EmailAddress)
    set @rowCount = rowcount(@attendedEvents)

    if @debug == 1 then
      output(concat("<br>rowCount: ", @rowCount))
    endif

    set @attendedEventNames = ""
    set @attendedEventIds = ""
    set @qualifiedEventCount = 0
     
    /* qualified event loop -- events with a webinar names match */ 
    for @i = 1 TO @rowCount do

        set @row = row(@attendedEvents,@i) 
        set @eventID = field(@row,'Event_ID')
        set @event_name = Lookup("Webinar names", "event_name", "Event_ID", @eventID)

        if @debug == 1 then
          output(concat("<br>eventID: ", @eventID))
          output(concat("<br>event_name: ", @rowCount))
        endif

        if not empty(@event_name) then 

            set @qualifiedEventCount = add(@qualifiedEventCount,1)
            set @attendedEventNames = concat(@attendedEventNames, "|", @event_name)
            set @attendedEventIds =  concat(@attendedEventIds, "|", @eventId)

            /* strip leading delimiter from first element */
            if @qualifiedEventCount == 1 then
                set @attendedEventNames = replace(@attendedEventNames,"|","")
                set @attendedEventIds = replace(@attendedEventIds,"|","")
            endif

        endif

    next @i

    if @debug == 1 then
      output(concat("<br>qualifiedEventCount: ", @qualifiedEventCount))
      output(concat("<br>attendedEventNames: ", @attendedEventNames))
      output(concat("<br>attendedEventIds: ", @attendedEventIds))
    endif    

    if @qualifiedEventCount == 0 then
        raiseError("No qualifed events for subscriber", 1) /* skip the send to this subscriber */
    endif

    set @attendedEventNamesOutput = ""
    set @qualifiedEventNamesRowset = buildrowsetfromstring(@qualifiedEventNamesRowset,"|")
    set @rowcount = rowCount(@qualifiedEventNamesRowset)

    if @debug == 1 then
      output(concat("<br>rowCount (2): ", @rowCount))
    endif 

    /* qualified event output loop -- build a punctuated output string */ 
    for @i = 1 to @rowcount do

        set @row = row(@qualifiedEventNamesRowset, @i) 
        set @eventName = field(@row,1)

        if @i == 1 then
          set @attendedEventNamesOutput = @eventName
        elseif @i == @qualifiedEventCount then
          set @attendedEventNamesOutput = concat(@attendedEventNamesOutput, " and ", @eventName)
        else 
          set @attendedEventNamesOutput = concat(@attendedEventNamesOutput, ", ", @eventName)  
        endif 

    next @i

]%%

Hello, %%=properCase(@firstName)=%%.  Thanks for watching %%=v(@attendedEventNamesOutput)=%%.
<br><br>Here is more content you might like: 

%%[ /* output content based on events attended  */ ]%%

%%[ if indexOf(@attendedEventIds, "12345") > 0 and indexOf(@attendedEventIds, "23456") == 0 and indexOf(@attendedEventIds, "9876") == 0 then ]%%

    %%[ /* attended 12345, but not 23456 and 9876  */ ]%%
    <p>CONTENT HERE</p>

%%[ elseif indexOf(@attendedEventIds, "2345") == 0 and indexOf(@attendedEventIds, "789") == 0 then ]%%

    %%[ /* attended 2345, but not 789    */ ]%%
    <p>CONTENT HERE</p>

%%[ endif ]%%

Answered by Adam Spriggs on January 4, 2022

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