still pretty new salesforce developer, but I was curious what the best approach was to add a companies stock price to their account page in salesforce?

I've been referring to the articles below:



Both of these articles are pretty old though and not sure if this is still the best way to go about it. To be clear I have made a currency field on the account object called "Stock Price". I want this field to display the current accounts stock price if they have one. And using the scheduler i want to try to make it update every hour if possible. I'm stuck at trying to figure out what information to pass to get the companies current stock price. If the companies don't have a current ticker symbol listed than would you pass the account name to the API? Or would you require the user to enter in a ticker symbol in order to display the stock price.

Feel free to add any tags, I'm not sure which best describe this.

This is the mock code from website above:


public class StockPrices 
    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;  

global class getStockPrice implements Schedulable

   global void execute(SchedulableContext ctx)
     <span><span style="font-size: 13px">
      //call a future method so we can do callouts within a schedule<br>    
global class StockPriceJobScheduler
   global void StockPriceJobScheduler() {}

   public static void start()
     //Seconds Minutes Hours Day_of_month Month Day_of_week optional_year

     System.schedule('Stock Price Update', '0 0 1-23 * * ?', new getStockPrice());

  • 2
    salesforce classic or Lightning? (the latter gives you more options to do client side with LWC). Also, do you want to persist a stock price in the database for use in reporting or listviews? If yes, then some sort of scheduled job makes sense (yesterday's closing price). Do you want near real time stock price displayed whenever an Account is viewed? – cropredy Aug 27 '19 at 17:54
  • lightning, maybe, and yes – Tyler Aug 27 '19 at 18:00
  • if you persist, you have to define the price's date. Many systems use the market's closing price (so your job would run at the end of the market). If you want real time as well, you'll need to build a call to a remote service that provides stock prices. This would most likely be done wrapped in a LWC – cropredy Aug 27 '19 at 18:29
  • I think the initial idea was to use a batch process that grabs all the stock prices for all the companies we have and keeps that information stored so that when someone views the companies profile the stock price is already there. And also allows them to do reports over it. This process will run every hour through scheduler. I'm just not sure how to grab the stock prices for these companies. – Tyler Aug 27 '19 at 19:59

Based on a cursory search, it appears the Yahoo Finance API is no longer available

One alternative, and there are many is Intrinio. Free APIs tend to be severely rate-limited but low cost monthly paid APIs are available from vendors

If the companies don't have a current ticker symbol listed than would you pass the account name to the API? Or would you require the user to enter in a ticker symbol in order to display the stock price.

If the Account doesn't have a ticker symbol, you would need to use a second service to look up the ticker based on Account Name. You also have to worry about markets as not all companies are listed on, say, United States exchanges

But otherwise ...

The scheduled job approach is correct and you can run it hourly. The scheduled job should start a batchable. There are limits you have to be aware of in the batchable's execute() - which is a transaction

  • Total number of callouts (HTTP requests or Web services calls) in a transaction = 100
  • Maximum cumulative timeout for all callouts (HTTP requests or Web services calls) in a transaction = 120 sec

Hence set the scope size for the batchable to something less than 100 (or even smaller depending on how long the callouts take). The batchable.execute() can do callouts provided the class implements Database.AllowsCallouts


Using the Intrinio V2 API to fetch stock prices


you will get back an object with an array of stock prices (by date) as per the doc link above

To make processing this array easier, declare a custom apex type

class IntrinioPriceResult {
  IntrinioPriceDetail[] stock_prices;

class InrinioPriceDetail {
  Date date;
  Decimal close;
  ... other fields from API that you care about

Then deserialize the HttpResponse into the custom type object

IntrinioPriceResult result = 
  (IntrinioPriceResult) JSON.deserialize(myHttpResponseBody,IntrinioPriceResult.class);

and then you can traverse the array of prices fetched by date for the one(s) you care about

for ( IntrinioPriceDetail detail: result.stock_prices) {
    if (detail.date == ...) {do something}
| improve this answer | |
  • I appreciate the feedback, after talking with management more it appears they are going to give a list of ticker symbols they want stock prices for. So I would theoretically be able to just put this list in StockPrices and be able to call them all, as long as there is less that 100 in a single callout, correct? Then the issue is getting them to the correct accounts. – Tyler Sep 4 '19 at 14:11
  • @Tyler depends on the API. If API can only accept a single ticker, then you will do n callouts and be subject to the limits above. – cropredy Sep 4 '19 at 14:18
  • Using Intrinio I don't use a use case for apex. how would i retrieve the stock price and update a field? – Tyler Sep 4 '19 at 19:06
  • @Tyler - not sure I follow; Intrinio API would replace the yahoo finance api in your OP. Give it a try and and post a new question once you've written the code and tried it. – cropredy Sep 5 '19 at 6:11
  • Oh sorry, I meant see*. I dont see* a use case for Intrinio with apex. They support java and c# but i cant import those java packages from apex. – Tyler Sep 5 '19 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.