+ Start a Discussion

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.

Is there any public url available free to get stock price?
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
    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();
    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