Showing posts with label Webservices. Show all posts
Showing posts with label Webservices. Show all posts

Tuesday, November 26, 2019

D365FO / C# - Accessing the InfoLog / InfoLogEntries Object within a custom webservice

While working on testing a custom web-service that originally tried to read the info log and report it back to a field in web service via custom logic I noticed that that error message being returned was actually pretty generic and told me nothing about the issue.

After debugging the code it looks like the info log itself is now being returned automatically with a SOAP / JSON web service which is great because this actually tells us why a record cant be created or what the issue is in detail with no extra code. However it appears nothing is logged online about this and it doesn't appear to work like a contract class response and which would allow you to use a foreach() on the object. Instead we need to treat it like an array based object as the code below shows

Whats cool about this is that it actually returns the type of message from the infolog as well (Info, Warning, Error) so you can add in logic that will tell you what exactly the issue is.

 //call out the infolog class  
 [webserviceName].Infolog messages = new [webservicename].Infolog();  
 //read in the infolog from the response  
 messages = response.Infolog;  
 //get the length of the log and read in each message  
 for(int messagePosition = 0; messagePosition <= messages.Entries.Length - 1; ++messagePosition)  
 {  
    AzureLog.AppendText(String.Format("Info Log Message {1}: {0}", messages.Entries[messagePosition].Message.ToString(), messages.Entries[messagePosition].Type.ToString()) + Environment.NewLine);  
 }  




The output will then look something like this


Info Log Message Info: A custom info statement based on an extension of salesTable.ValidateWrite()
Info Log Message Warning: Site [SITENAME] does not exist.
Info Log Message Warning: Warehouse [WAREHOUSENAME] is not connected to site [SITENAME]
Info Log Message Warning: Warehouse [WAREHOUSE] does not exist.
Info Log Message Warning: Site [SITE] does not exist.
Info Log Message Error: Update has been canceled.







Tuesday, August 28, 2018

D365FO - Azure authentication setup for web services (defined creds and interactive login)

When developing web services within D365FO for other applications/languages we need to follow a new authentication process compared to AX 2012 since everything is hosted on Azure. The following will show you the settings that need to be defined within your Azure dashboard and within D365FO to enabled authentication to execute a custom web service (SOAP/JSON) or Odata calls.

It is good to note that you may need to setup up a webapi or a native application depending on what you are trying to accomplish. Both are about the same just the native application does not require the key generation.

Before I go over the steps needed the following goes over the multiple types of authentication for Azure and explains the difference between Native vs Web API auth scenarios: https://docs.microsoft.com/en-us/azure/active-directory/develop/authentication-scenarios 


Azure Setup for defined credentials/web api

Register a web app / api

Step 1: In the Azure port go to Azure Active Directory > App registrations > New Application registration

Step 2: Enter in the environment information for D365 and hit create

Step 3: Once initialized click on the settings button


Step 4: Click on required permissions > add > Select API


Step 5: Choose "Microsoft Dynamics ERP"

Select which options you want to give it access to. (usually all of them)

Choose done.

Step 6:  choose "keys"

 Under password input a description and choose when the cert should expire






Hit the save button and save the "value" aka the key. This is what will be used as a "handshake"


Step 7 (optional): Do the same thing but for a native application (interactive login). Just enter the main login url as the redirect url. You will not need the Key generation part.



D365FO setup:

Step 8 : In D365FO go to System administration > Setup > Azure Active Directory applications and create a new record for the app registration with the client id(s) from the Azure setup. If you are using the defined cred's via the web app / api type as listed below the User Id listed in this screen is what the system will log an new records created via the webservice