Using Python to fetch data from D365 Finance & operations

 


This short article can help you fetch data from D365FinOps, which
a. You can subsequently analyse
b. You can perfrom further predictive modelling based on this data 
c. Understanding and forcasting the direction in which business is going and so on.

And it's quite straight forward.
Step 1. Define your Azure app registrations (AKA Microsoft Entra-ID).
Step 2. Enasure that the Azure App is registered in D365FO end by enabling the same through System Admin \\Setup\\Microsoft Entra ID Applications 

Step 3. We are going to use Jupyter Notebook here, for our code. This could be easily launched, if you have installed Anaconda as your IDE and then simply going to Windows \\ Jupyter. This will prompt you with an intermittent DOS prompt like this:


And will eventually open the Jupyter browser which looks lot like this:


Step 4: We are going to use the following modules for our code:

import requests

import json 

The former handles the requests and responses from API calls while the latter contains Newtonsoft based various intefcaes and class for decoding. 

Step 5: Define your app registration details under various variables:

CLIENT_SECRET = ' ##Your client secret here##'

TENANT_ID = '##Your tenant here##'

tokenendpoint = 'https://login.microsoftonline.com/<Give your tenant Id>/oauth2/token'

CLIENT_ID = '##Your client ID##'

GRANT_TYPE = 'Client_credentials'

RESOURCE =  '<D365fO base URL>/.default'

BaseURL = '<D365fO base URL>/.'

Step 6:
These variables now need to be passed on as a Dictionary element and posetd for Token generation:
tokenpost = {
    'client_id':CLIENT_ID,
    'scope':RESOURCE,
    'client_secret':CLIENT_SECRET,
    'grant_type': GRANT_TYPE
}
For more clarity on dictionary elements, please follow:
https://www.blogger.com/blog/post/edit/2520883737187850604/2026928301557204228

Step 7:

Get the token and store it in a variable:

tokenres = requests.get(tokenendpoint, data=tokenpost)

Step 8:
Here we are going to call D365FO data entities. For our demo purpose we are going to call SalesOrderPool data entity:
try:
    accesstoken = tokenres.json()['access_token']
except(KeyError):
    print('Could not get bearer access token')

api_call_headers = {
        'Authorization': 'Bearer ' + accesstoken,
        'Content-Type': 'application/json'
    }


test_api_url = f"{basUrl}/data/SalesOrderPoolsOrderPools"
api_call_response = str(requests.get(test_api_url, headers=api_call_headers))

try:
    if not api_call_response:
        data = json.loads(api_call_response)
        print(data)
    else:
        print ('No response');
except json.JSONDecodeError as e:
    print("Invalid JSON syntax:", e)

This will give entire SalesOrderPool JSON records.
If you want any speific details for any specific reord, you can use expaded JSOn query in the above code:
PoolVal = 'SalesOrder00001'
test_api_url = f"{basUrl}/data/SalesOrderPoolsOrderPools?$filter=PoolId eq {PoolVal}" 

In our previous discussion (https://subsd365.blogspot.com/2024/03/an-example-of-predictive-modelling.html) we saw how can we create a sampling based predicitve modeling strucure, where we can train our machine learning model with data and can find how easily it can predict any answer to a question. In the example we used, we were training the data with viewers, their genders, age and their fascination for movie-genres. So we asked the program what could be a man of age 40 would prefer to watch. And our code very smartly replied: Family drama. 

Likewise, we can use the data from D365FO to train our models to predict various outcomes and statistical manipulations.


Comments

Popular posts from this blog

X++ : mistakes which developers commit the most

Make your menu items visible on main menu, conditionally,, using this cool feature of D365FO

Speed up your execution performance by using SysGlobalCaches