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.