function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PallavDPallavD 

Consuming RSS feeds from Apex

Hi All,

 

I have a question regarding consuming RSS feeds from Apex. I have a requirement where I need to get the data extracted from RSS feeds on Exchange rates and insert them in the Dated Exchange Rate table in salesforce and also to fixed exchange rate table.

 

I am aware that I need to write a Apex DOM class to read the feeds and apply the custom logic of matching out the diffrent currencies and upload in the relevent salesforce object for exchange rates. I am also aware that I need to get a subscription to the RSS feeds.

 

I faces problem in understanding how can I actully get the XML file and how to use or make a call to get the values using Apex class.

 

Please advice. Thanks a lot in advance for your responses.

 

Thanks and regards,

 

Pallav

Best Answer chosen by Admin (Salesforce Developers) 
Alok_NagarroAlok_Nagarro

Hi, i am giving you some sample code..It follows,

 

 

public class TestDemo1 {

public TestDemo1()
{
 this.url='http://troutco.com/blog/?feed=rss2';     // let's say this is url for rss feed
}

public String url{get; set;}
public XMLDom dom;
public List<RssFeed> getXmlContent()
{
String body='';
List<RssFeed> xml=new List<RssFeed>();
HttpRequest req=new HttpRequest();
req.setEndpoint(this.url);
req.setMethod('GET');
req.setHeader('Range','bytes=0-30000');
req.setCompressed(true);

HttpResponse res=null;
Http h=new Http();
res=h.send(req);

if(res.getBody()!=null)
{
body=res.getBody();
}
else
return xml;

dom=new XMLDom(body);
for( xmldom.element e: dom.getElementsByTagName('item')) 
        {
            RssFeed rf=new RssFeed();
            rf.title =e.getValue('title');
            rf.comments =e.getValue('comments');
            xml.add(rf);
        }   
        return xml;
}


public class RssFeed
{
public String title {get; set;}
public String comments {get; set;}
}

 

}

 

 

I hope, this code would solve your problem.

 

All Answers

Alok_NagarroAlok_Nagarro

Hi, i am giving you some sample code..It follows,

 

 

public class TestDemo1 {

public TestDemo1()
{
 this.url='http://troutco.com/blog/?feed=rss2';     // let's say this is url for rss feed
}

public String url{get; set;}
public XMLDom dom;
public List<RssFeed> getXmlContent()
{
String body='';
List<RssFeed> xml=new List<RssFeed>();
HttpRequest req=new HttpRequest();
req.setEndpoint(this.url);
req.setMethod('GET');
req.setHeader('Range','bytes=0-30000');
req.setCompressed(true);

HttpResponse res=null;
Http h=new Http();
res=h.send(req);

if(res.getBody()!=null)
{
body=res.getBody();
}
else
return xml;

dom=new XMLDom(body);
for( xmldom.element e: dom.getElementsByTagName('item')) 
        {
            RssFeed rf=new RssFeed();
            rf.title =e.getValue('title');
            rf.comments =e.getValue('comments');
            xml.add(rf);
        }   
        return xml;
}


public class RssFeed
{
public String title {get; set;}
public String comments {get; set;}
}

 

}

 

 

I hope, this code would solve your problem.

 

This was selected as the best answer
PallavDPallavD

Hi Alok,

 

Thanks a lot. Let me try the same.

 

regards,

 

Pallav

HoneyHoney

Hi,

I need a help regarding that. I am new to salesforce and need to get exchange rates from yahoo financia site and update it on my custom objects, so please tell me code regarding that.

Thanks in advance.

Alok_NagarroAlok_Nagarro

Hi,

 

Below code can help you, fist you need to include "http://pipes.yahoo.com/" url to Remote site setting in salesforce org.

goto setup--> Administration Setup--> security setting--> Remote Site Setting

 

public class TestDemo1 {

    public TestDemo1()
    {
     this.url='http://pipes.yahoo.com/pipes/pipe.run?_id=_kHVIdZ13hGXgt1VwmH_9A&_render=rss';     // this is url for getting list of currencies and their exchange rates relative to USD
    }

    public String url;
    public XMLDom dom;

    public void getConversionRateFromYahoo()
    {
        String body='';
        List<CustomObject> cusObg=new List<CustomObject>();  // let's say CustomObject is custom object holding conversion rates
        HttpRequest req=new HttpRequest();
        req.setEndpoint(this.url);
        req.setMethod('GET');
        HttpResponse res=null;
        Http h=new Http();
        res=h.send(req);

        if(res.getBody()!=null)
        {
            body=res.getBody();
            dom=new XMLDom(body);
            for( xmldom.element e: dom.getElementsByTagName('item'))
                    {
                        CustomObject co=new CustomObject();
                        // let assume CustomObject have two fields title and description to hold currency name and conversion rate repectively.
                        co.title__c =e.getValue('title');              
                        co.description__c =e.getValue('description');
                        cusObg.add(co);
                    }
            if(cusObg.size()>0)
            {
                insert cusObg;
            }           
        }       
    }



}

HoneyHoney

Thanks a lot, i will check that and let you know if it's work fine with me.

Shall i change URL or same URL works?

Alok_NagarroAlok_Nagarro

You can change url, but make sure response must be in the form of XML.

Then you also need to follow xml hierarchy  to access data from that.

HoneyHoney

Hi Alok, I have written the class with same URL. Please tell me how i run that now. I know i am asking too much, but i worked on triggers and test classes only, so not understand this class much.

Alok_NagarroAlok_Nagarro

Hi honey,

 

You can call this method from trigger also, but for that you need to define this method with future annotation (@future) in class. Since salesforce doesn't support callout from trigger.

example:

 

class className

{

 

 @future(callout=true)

 public static void method_name()

 {

  // paste code here
 }

}

 

Now you can invoke this method from trigger like this-

 

// since method with @future annotation must be static so just invoke using class name

className.method_name();

 

Note: There are many ways to execute class method, totaly depends on your requirement.

 

HoneyHoney

Hi, i am usuing both fields and wrote trigger as well and call this class after save but field data not update, how data come from yahoo?