TransWikia.com

Using AMPScript to pull in RSS feed content in SFMC

Salesforce Asked by Clyde W. on July 26, 2020

I am attempting to pull in content from a blog feed using AMPscript but am experiencing some issues in attempt to do so. It is probably worth noting that all I need to pull in is the title, description, and link of each blog post.

I’ve searched through other SFSE posts on this topic for a resolution but have not found one.

Here is my AMPScript:

%%[
Var @xml, @titles, @title, @descs, @desc, @links, @link, @cnt

Set @xml = ContentBlockByName("Content Builderz_SFMC_TestBlog_Feed")
Set @titles = BuildRowsetFromXML(@xml,"//item/title",1)
Set @descs = BuildRowsetFromXML(@xml,"//item/description",1)
Set @links = BuildRowsetFromXML(@xml,"//item/link",1)

If RowCount(@titles) > 5 THEN
SET @rows = 5
ELSE
SET @rows = RowCount(@titles)
ENDIF

IF @rows >= 1 THEN
for @cnt = 1 to 5 do
Set @title = Field(Row(@titles,@cnt),"Value")
Set @desc = Field(Row(@descs,@cnt), "Value")
Set @link = Field(Row(@links,@cnt), "Value") 
]%%

<div style="border: 1px solid #444; background-color: #F7F7F7; margin: 0.76em 0; padding: 0.76em;">
<h1 style="font: bold normal 1.0em Arial, Helvetica, sans-serif;"><a href="%%=RedirectTo(@link)=%%" alias="%%=v(@title)=%%" title="%%=v(@title)=%%" style="color: #000;">%%=v(@title)=%%</a></h1>
<span style="font: normal normal 0.76em Arial, Helvetica, sans-serif; color: #444;">%%=v(@desc)=%%</span>
</div>
%%[ 
NEXT @cnt 
ENDIF
]%%

Here’s the content block that calls in the blog feed. (Note: I have this saved outside the email):

%%before; httpget; 1 "https://developer.salesforce.com/blogs/feed"%%

I have also tried just calling in the blog feed directly in my ampscript block without using a separate content area/block. No success.

Set @xml = HTTPGet("https://developer.salesforce.com/blogs/feed")

I have tested other valid RSS feeds using this AMPScript and have pulled content into my email with success. I have been unable to get the AMPScript to work with the blog feed I’m using in my case–
http://developer.salesforce.com/blogs/feed
I’m not receiving any errors and am able to preview and test send my email just fine. It’s just that there is zero XML content being pulled in.

XML sample:

<channel>
    <title>Salesforce Developers Blog</title>
    <atom:link href="https://developer.salesforce.com/blogs/feed" rel="self" type="application/rss+xml" />
    <link>https://developer.salesforce.com/blogs</link>
    <description>Elevating developer skills and connecting with the Salesforce Developers community</description>
    <lastBuildDate>Mon, 25 Feb 2019 18:00:45 +0000</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>

    <item>
        <title>Features and Settings and Scratch Orgs, Oh My!</title>
        <link>https://developer.salesforce.com/blogs/2019/02/features-and-settings-and-scratch-orgs-oh-my.html</link>
        <comments>https://developer.salesforce.com/blogs/2019/02/features-and-settings-and-scratch-orgs-oh-my.html#respond</comments>
        <pubDate>Mon, 25 Feb 2019 18:00:45 +0000</pubDate>
        <dc:creator><![CDATA[Karen Fidelak]]></dc:creator>
                <category><![CDATA[Developer Experience]]></category>
        <category><![CDATA[February 2019]]></category>
        <category><![CDATA[salesforce dx]]></category>
        <category><![CDATA[scratch orgs]]></category>

        <guid isPermaLink="false">http://developer.salesforce.com/blogs/?p=190137</guid>
        <description><![CDATA[Scratch orgs are one of the really great capabilities provided with Salesforce DX. One of the benefits of scratch orgs is that they start empty, allowing you to configure them how you want and develop in a clean and known environment. But what is a benefit can also be a challenge. With all the optional […]]]></description>

Anyone have any recommendations or know of any workarounds? Am I missing something obvious? Anything would be appreciated!

Thanks!

2 Answers

Here's how I'd approach it with @Mark's fix:

<html>
<head>
  <style>
    body, a, input {font-family:sans-serif;}
  </style>
</head>
<body style="font-family:sans-serif">
%%=now(1)=%%

%%[

var @xml, @isXML, @nodes, @rowCount
Set @xml = HTTPGet("https://developer.salesforce.com/blogs/feed", false, 0, @CallStatus)
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))

  if @rowCount > 0 then

    for @i = 1 to @rowCount do

      var @title
      var @description
      var @link

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

      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

      ]%%

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


      %%[

    next @i

  else

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

  endif

else

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

endif

]%%

<br><br><br><a href="%%profile_center_url%%">Profile/Preference Center</a>
<br><a href="%%subscription_center_url%%">Subscription Center</a>
<br><a href="%%unsub_center_url%%">One-Click Unsubscribe</a>
<p>This email was sent by:
<b>%%Member_Busname%%</b>
<br>%%Member_Addr%%, %%Member_City%%, %%Member_State%%  %%Member_PostalCode%%  %%Member_Country%%</p>
<custom name="opencounter" type="tracking">
</body>
</html>

Output:

RSS XML parsing

Answered by Adam Spriggs on July 26, 2020

Running the feed's XML into a validation tool produced the following error message:

The prefix "dscblog" for element "dscblog:featured" is not bound.

If you don't have control over the original XML source, you could try removing the unbound namespace from the XML:

Set @xml = ContentBlockByName("Content Builderz_SFMC_TestBlog_Feed")
Set @xml = Replace(@xml, "dscblog:", "")

Answered by Mark G on July 26, 2020

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