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
kabukabu 

how to get stock price using Apex and update to a field?

I wanted to read stock price Yahoo or any other site like google finance or msn but cant get any sample code. I tried the following sample  and it is not working now.
https://developer.salesforce.com/blogs/developer-relations/2010/02/spring-10-saw-the-general-availability-of-one-of-my-favorite-new-features-of-the-platform-the-apex-schedulerwith-the-apex-s.html

Is there any public url available free to get stock price?
JitukawaleJitukawale
See this link https://stackoverflow.com/questions/10040954/alternative-to-google-finance-api 

Hope this helps you.
Shailesh RawteShailesh Rawte
Hi kabu.

I got some code, I think it is work for you
global class CallYahooQuotes
{
    @future(callout=true)
    public static void getQuotes()
    {   //for our simple example we are going to just update the quotes on one Account
       Account acct = [select id, TickerSymbol, stock_price__c from account where name = 'Salesforce.com'];
   
       //where f stands for the fields to be displayed (in this case: s - symbol; l1 - last price; c1 - change; d1 = last date)
       // String url = 'http://quote.yahoo.com/d/quotes.csv?s='+acct.TickerSymbol+'&f=sl1c1d1&e=.csv';
     
     String url = 'http://download.finance.yahoo.com/d/quotes.csv?s='+acct.TickerSymbol+'&f=l1&e=.csv';
    
    Http h = new Http();
    
    HttpRequest req = new HttpRequest();
    req.setEndpoint(url);
    req.setMethod('GET');
    
    HttpResponse res = h.send(req);
    
    //omitting error handling for example
    acct.stock_price__c = decimal.valueOf(res.getBody().trim());  
    update acct;  
   }
}


Thanks & Regards
Shailesh Rawte