0

I'm trying to output a list of names from a DE into and email and format with commas and "and"

Example Batman, Super Man and Spider Man

Code

%%[
var @rows, @rowCount, @numRowsToReturn, @i

set @numRowsToReturn = 0 /* all */
set @rows = LookupOrderedRows("191204-NYC-Marketers-Advisor-XREF", @numRowsToReturn,"FullName asc","SC_ContactID", @SC_ContactID)
set @rowCount = rowcount(@rows)

]%%

%%[

if @rowCount > 0 then


    for @i = 1 to @rowCount do

        var @First, @AdvisorName

        set @row = row(@rows,@i)
        set @First = Field(@row,"FirstName")
        set @AdvisorName = Field(@row,"Advisor Full Name")



        IF @rowCount == 1 THEN
          outputline(concat(@AdvisorName))
        ELSEIF @rowCount > 1 THEN
        outputline(concat(@AdvisorName, ","))
        ENDIF
        ]%%


        %%[ 



    next @i 

]%%

%%[ else ]%%

No rows found

%%[ endif ]%%

RESULTS I'm getting is

Batman, Super Man, Spider Man,

4

You can use the @i variable and compare with the rowcount to determine whether to use a comma or an 'and'.

IF @rowCount == 1 THEN
    outputline(concat(@AdvisorName))
ELSE
    IF @rowCount == @i THEN
        outputline(concat(" and ",@AdvisorName))
    ELSE
        outputline(concat(", ",@AdvisorName))
    ENDIF
ENDIF
  • Josh you provided me the key to make this work - for that I thank you. – Jason Lepes Dec 6 '19 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.