0

I am using some AMPScript to SET specific subject lines based on certain date ranges

I have SET the @Subject to include the @month value but the output is not displaying correctly

%%[
Set @month = DatePart(NOW(), 'month')
IF (@month == 11 AND @day > 1) or (@month == 1 AND @day < 31)
        THEN
        SET @Subject= 'Test 1 %%=v(@month)=%% ABC'
        ELSE
        SET @Subject= 'Test 2 %%=v(@month)=%% DEF'
ENDIF]%%

The month variable is not pulling through so I am getting the following output Test 1 %%=v(@month)=%% ABC instead of Test 1 11 ABC

The solution I am using is to have two separate subject lines and printing all three variables separately

eg. %%=v(@SubjectA)=%% %%=v(@month)=%% %%=v(@SubjectB)=%%

using this code

%%[
Set @month = DatePart(NOW(), 'month')
IF (@month == 11 AND @day > 1) or (@month == 1 AND @day < 31)
        THEN
        SET @SubjectA= 'Test 1'
        ELSE
        SET @SubjectA= 'Test 2'
ENDIF]%%
%%[
Set @month = DatePart(NOW(), 'month')
IF (@month == 11 AND @day > 1) or (@month == 1 AND @day < 31)
        THEN
        SET @SubjectB= 'Test 3'
        ELSE
        SET @SubjectB= 'Test 4'
ENDIF]%%

The above does the job but it is really unclean and I am not sure if I am missing something.

Any help is greatly appreciated.

Cheers

2 Answers 2

0

Hope below is what you are trying to achieve!

%%[
Set @month = DatePart(NOW(), 'month')
SET @day = Datepart(Now(),'day')
IF (@month == 11 AND @day > 1) or (@month == 11 AND @day < 31)
       THEN
       SET @Subject= Concat('Test 1 ',@month,' ABC')
       ELSE
       SET @Subject= Concat('Test 2 ',@month,' DEF')
ENDIF
]%%

%%=v(@Subject)=%%    \\Test 1 11 ABC 

Two things to highlight,

  1. You missed to use the Concat function
  2. You missed to create the @day variable
SET @day = Datepart(Now(),'day')
2
  • Thank you for the response. The above works correctly! I did have the SET day but forgot to include it in my example. Am I assuming correctly with the Set Month and Set Day variables that any dates within that range will show the first subject? I know MC is by default UTC but I am in Australia and want to stick to those specific date ranges but not exclusively to a year as I want this to be set and forget and not have to update the year every year
    – Kane Elias
    Nov 18, 2021 at 22:17
  • Try similar to this in AMPscript ... salesforce.stackexchange.com/questions/210751/… Nov 19, 2021 at 4:27
0

You might be able to use something like this. It basically sets a subject line when current date falls within a specific date range (holiday event etc) and then falls back on a default subject line for all other dates outside that range. I hope this helps in some way :)

Original Version

%%[
/**
* Current Date Logic
* Setting date variables to use in our subject line logic below.
*/
SET @currentDate = Format(Now(), "MM/dd/yyyy", "Date", "en-US")
SET @currentMonth = Datepart(@currentDate, "month")
SET @currentDay = Datepart(@currentDate, "day")
SET @eventStart = "11/1/2021"
SET @eventEnd = "01/31/2022"

/**
* Subject Line Logic
* Setting a custom subject line to use during event dates.
* Setting a fallback subject line to use outside event dates.
*/
IF (DateDiff(@eventStart,@currentDate,"D") >= 0) AND (DateDiff(@eventEnd,@currentDate,"D") <= 0) THEN
  /* Subject line to show during event dates */
  SET @subject = Concat("Subject Line A Test: You're current month is [", @currentMonth,"]. Show the holiday subject line.")
ELSE
  /* Subject line to show for outside of event dates */
  SET @subject = Concat("Subject Line B Test: You're current month is [", @currentMonth,"]. Show the regular subject line.")
ENDIF
]%%
Current Date: <span style="color:red;"> %%=v(@currentDate)=%%</span><br>
Current Month: <span style="color:red;"> %%=v(@currentMonth)=%%</span><br>
Current Day: <span style="color:red;"> %%=v(@currentDay)=%%</span><br>
Event Start Date: <span style="color:red;"> %%=v(@eventStart)=%%</span><br>
Event End Date: <span style="color:red;"> %%=v(@eventEnd)=%%</span><br>
Subject: <span style="color:red;"> %%=v(@subject)=%%</span><br><br>

Modified Version

%%[
/**
* Current Date Logic
* Setting date variables to use in our subject line logic below.
*/
SET @currentDate = Format(Now(), "MM/dd/yyyy", "Date", "en-US")
SET @currentMonth = Datepart(@currentDate, "month")
SET @currentDay = Datepart(@currentDate, "day")
SET @currentYear = xtyear
SET @nextYear = Add(xtyear,1)
SET @eventStart = Concat("11/1/", @currentYear)
SET @eventEnd = Concat("01/31/", @nextYear)

/**
* Subject Line Logic
* Setting a custom subject line to use during event dates.
* Setting a fallback subject line to use outside event dates.
*/
IF (DateDiff(@eventStart,@currentDate,"D") >= 0) AND (DateDiff(@eventEnd,@currentDate,"D") <= 0) THEN
  /* Subject line to show during event dates */
  SET @subject = Concat("Subject Line A Test: You're current month is [", @currentMonth,"]. Show the holiday subject line.")
ELSE
  /* Subject line to show for outside of event dates */
  SET @subject = Concat("Subject Line B Test: You're current month is [", @currentMonth,"]. Show the regular subject line.")
ENDIF
]%%
Current Date: <span style="color:red;"> %%=v(@currentDate)=%%</span><br>
Current Month: <span style="color:red;"> %%=v(@currentMonth)=%%</span><br>
Current Day: <span style="color:red;"> %%=v(@currentDay)=%%</span><br>
Current Year: <span style="color:red;"> %%=v(@currentYear)=%%</span><br>
Next Year: <span style="color:red;"> %%=v(@nextYear)=%%</span><br>
Event Start Date: <span style="color:red;"> %%=v(@eventStart)=%%</span><br>
Event End Date: <span style="color:red;"> %%=v(@eventEnd)=%%</span><br>
Subject: <span style="color:red;"> %%=v(@subject)=%%</span><br><br>
3
  • Hi @Nolan thanks for the reply. I am trying to avoid using a specific year as this will be a set and forget message and don't want to update the AMPScript every year. Can it be modified to only use Day and Month? Also, is there an offset that can be used as I am in Australia? Thank you
    – Kane Elias
    Nov 18, 2021 at 22:22
  • Hi @Kane Elias, you might be able to leverage xtyear to control current and next year. This way you can still evaluate the difference in days without having to touch your start/end date. Above is the modified code where I've basically appended currentYear and nextYear to your event start and end date. It appears to be working on my side. Simply hard code currentDate to something like "10/01/2021" to test the logic. I can't think of a solution for your offset question. There might be other articles here how to convert date/time based on region. I hope this helps and happy coding! Nov 23, 2021 at 8:02
  • Thanks @Nolan I will have a try with this and let you know how it works. Appreciate your time on this.
    – Kane Elias
    Nov 25, 2021 at 0:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .