0

I am trying to create a content block in email-studio, using xml/rss feed to import content for display.

I've used both (1) creating a standalone content block, and (2) inserting a code snippet content block into an email. The 'preview' function in the editor for the former just shows me part of the code itself, no output. The 'preview email' function in the latter just shows a blank slot (or, truncates the message).

I'm using:

  1. [https://salesforce.stackexchange.com/questions/277948/rss-feed-via-ampscript]
  2. [https://salesforce.stackexchange.com/questions/254076/ampscript-rss-feed]
  3. https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-programmatic-content.meta/mc-programmatic-content/rssFeedEmail.htm

I tried using the developer help code first, and that didn't work. I'm now using a slight edit from the first one (as below). I did use https://www.ampscript.io/ to review the code, but I haven't been able to fix it from that.

/* adjust if pubDate conditional to -14 for last 14 days, -300 last 300 etc */

%%=now(1)=%%

%%[

var @xml, @isXML, @nodes, @rowCount
set @xml = HTTPGet("https://lorem-rss.herokuapp.com/feed", false, 0, @CallStatus)

/* strip out invalid elements if needed */
/* set @xml = Replace(@xml, "dscblog:","") */

output(concat("<br>Call Status: ", @CallStatus))

if indexOf(@xml,"<channel>") > 0 then

  set @nodes = BuildRowsetFromXML(@xml,"/rss/channel/item",0)
  set @rowCount = rowcount(@nodes)

  output(concat("<br>rowCount: ", @rowCount))

  set @rowCount = iif(@rowCount > 5, 5, @rowCount)

  output(concat("<br>rowCount (adj): ", @rowCount))

  if @rowCount > 0 then

    for @i = 1 to @rowCount do

      var @pubDate
      var @title
      var @description
      var @link

      set @nodepath = concat("/rss/channel/item[",@i,"]/")

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"pubDate"))) > 0 then
          set @pubDate = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"pubDate"),0),1),'Value')
          set @pubDate = SystemDateToLocalDate(@pubDate)
      endif 

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"title"))) > 0 then
          set @title = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"title"),0),1),'Value')
      endif

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"description"))) > 0 then
          set @description = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"description"),0),1),'Value')
      endif

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"link"))) > 0 then
          set @link = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"link"),0),1),'Value')
      endif


      if @pubDate >= DateAdd(Now(1), -300, "D") then

        ]%%

         <h4>%%=v(@title)=%%</h4>
         %%=v(@pubDate)=%%
         <p>
           %%=v(@description)=%%
           <br><a href="%%=redirectto(@link)=%%">%%=v(@link)=%%</a>
         </p>


        %%[

      endif

    next @i

  else

   outputline(concat("<br>no items found"))

  endif

else

  outputline(concat("<br>no XML found"))

endif

]%%
1
  • It looks like the comments broke it, so I do now have it working in a clear HTML email (although it's not rendering in our template, separate issue), but it wasn't rendering with the comments in.
    – sjgknight
    Oct 29, 2021 at 6:24

1 Answer 1

0

If you create a Content Block of type HTML and add this code to it, then set the Customer/External Key to RSSModule. I made some adjustments around your pubDate criteria and fixed the comment:

%%[

/* adjust if pubDate conditional to -14 for last 14 days, -300 last 300 etc */

var @xml, @isXML, @nodes, @rowCount
set @xml = HTTPGet("https://lorem-rss.herokuapp.com/feed", false, 0, @CallStatus)

/* strip out invalid elements if needed */
/* set @xml = Replace(@xml, "dscblog:","") */
output(concat("<br>",now(1)))
output(concat("<br>Call Status: ", @CallStatus))

if indexOf(@xml,"<channel>") > 0 then

  set @nodes = BuildRowsetFromXML(@xml,"/rss/channel/item",0)
  set @rowCount = rowcount(@nodes)

  output(concat("<br>rowCount: ", @rowCount))

  set @rowCount = iif(@rowCount > 5, 5, @rowCount)

  output(concat("<br>rowCount (adj): ", @rowCount))

  if @rowCount > 0 then

    for @i = 1 to @rowCount do

      var @pubDate
      var @title
      var @description
      var @link

      set @nodepath = concat("/rss/channel/item[",@i,"]/")

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"pubDate"))) > 0 then
          set @pubDate = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"pubDate"),0),1),'Value')
          set @pubDate = SystemDateToLocalDate(@pubDate)
      endif 

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"title"))) > 0 then
          set @title = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"title"),0),1),'Value')
      endif

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"description"))) > 0 then
          set @description = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"description"),0),1),'Value')
      endif

      if rowcount(BuildRowsetFromXML(@xml,concat(@nodepath,"link"))) > 0 then
          set @link = Field(Row(BuildRowsetFromXML(@xml,concat(@nodepath,"link"),0),1),'Value')
      endif

      if format(@pubDate,"yyyy-MM-dd") >= format(DateAdd(Now(1), -14, "D"),"yyyy-MM-dd") then

        ]%%

         <h4>%%=v(@title)=%%</h4>
         %%=v(@pubDate)=%%
         <p>
           %%=v(@description)=%%
           <br><a href="%%=redirectto(@link)=%%">%%=v(@link)=%%</a>
         </p>
         <hr>

        %%[

      endif

    next @i

  else

   output(concat("<br>no items found"))

  endif

else

  output(concat("<br>no XML found"))

endif

]%%

...Then you can include it either as a Reference block or via AMPscript like this:

%%=ContentBlockByKey("RSSModule")=%%

You must log in to answer this question.

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