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

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 

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 = ''];
       //where f stands for the fields to be displayed (in this case: s - symbol; l1 - last price; c1 - change; d1 = last date)
       // String url = ''+acct.TickerSymbol+'&f=sl1c1d1&e=.csv';
     String url = ''+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