Hot to create a Plugin in Dynamics 365 Sales to Store Daily Market Prices from Yahoo Finance

by | Nov 14, 2022

As a sales professional, it’s important to keep track of the market prices of your accounts to make informed decisions about pricing and sales strategies. One way to do this is to fetch daily market prices from Yahoo Finance API and store them in Dynamics 365 Sales. In this article, we’ll walk you through the steps to create a plugin that fetches daily market prices from Yahoo Finance API and stores them in a custom time series entity related to the Account entity.

Step 1: Set up the integration with Yahoo Finance API

First, you need to set up the integration with the Yahoo Finance API to fetch the daily market prices of your accounts. You can use the REST API provided by Yahoo Finance to fetch the data. You will need to register for an API key and use it to authenticate your API requests.

Step 2: Create a custom time series entity

Next, you need to create a custom time series entity to store the daily market prices of your accounts. This entity will have a relationship with the Account entity to link each time series record with its corresponding account. You can create fields to store data such as date, closing price, high price, low price, and volume.

Step 3: Create a plugin to fetch daily market prices

Create a plugin that fetches the daily market prices of an account from Yahoo Finance API. This plugin should trigger when a new account is created or when an existing account is updated. You can use the HttpClient class in C# to make API requests and fetch the data from Yahoo Finance. Here’s an example code snippet that fetches the closing price of a stock from Yahoo Finance:

using System;

using System.Net.Http;

// Replace YOUR_API_KEY with your actual API key

string url = “https://apidojo-yahoo-finance-v1.p.rapidapi.com/market/v2/get-quotes?region=US&symbols=MSFT”;

string apiKey = “YOUR_API_KEY”;

HttpClient client = new HttpClient();

client.DefaultRequestHeaders.Add(“X-RapidAPI-Key”, apiKey);

HttpResponseMessage response = await client.GetAsync(url);

string responseBody = await response.Content.ReadAsStringAsync();

// Parse the JSON response to get the closing price

double closingPrice = 0;

if (response.IsSuccessStatusCode)

{

    dynamic json = Newtonsoft.Json.JsonConvert.DeserializeObject(responseBody);

    closingPrice = json.quoteResponse.result[0].regularMarketPrice;

}

Step 4: Store the daily market prices in the time series entity

Once you have fetched the daily market prices, store them in the time series entity related to the account. You can create a new time series record for each day and link it with the corresponding account record. Here’s an example code snippet that creates a new time series record and links it with the corresponding account record:

// Replace ACCOUNT_ID with the actual account ID

EntityReference accountRef = new EntityReference(“account”, new Guid(“ACCOUNT_ID”));

// Create a new time series record and set the fields

Entity timeSeriesRecord = new Entity(“new_time_series”);

timeSeriesRecord.Attributes[“new_date”] = DateTime.Today;

timeSeriesRecord.Attributes[“new_closing_price”] = closingPrice;

timeSeriesRecord.Attributes[“new_account”] = accountRef;

// Save the new time series record

service.Create(timeSeriesRecord);