
When working with batch jobs in Microsoft Dynamics 365 Finance and Operations (D365FO), it’s often helpful to capture the system messages (infolog) generated during the batch’s execution. By retrieving these messages at runtime, you can then repurpose them—such as sending them via email notifications or storing them in a custom table for auditing or reporting purposes. In this article, we’ll explore how to extract infolog messages directly from inside a batch job and provide practical examples for dispatching the results.
Accessing Infolog Messages Within the Batch Job
During a batch job’s run() method or any related logic, calls to info(), warning(), or error() accumulate messages in the global infolog. To retrieve these messages at the end of the batch’s processing:
- Count the Messages:
Useinfolog.line()to determine how many messages are currently stored. - Copy the Messages:
Useinfolog.copy(start, count)to extract messages as a container. This requires both a start line (typically 1 for the first message) and a count (equal to the total number of messages). - Parse the Messages:
Each infolog entry is stored as a container with details like message type and text. By iterating through these containers, you can build a single string or a structured record for further use.
Example:
public static str retrieveInfologMessages()
{
str result = "";
container infologContainer, infologMessage;
int i, messageCount;
int lastLine = infolog.line();
str msgText;
int messageType;
if (lastLine == 0)
{
return "No infolog messages.";
}
// Copy all messages from line 1 to the last line
infologContainer = infolog.copy(1, lastLine);
messageCount = conlen(infologContainer);
for (i = 1; i <= messageCount; i++)
{
infologMessage = conpeek(infologContainer, i);
messageType = conpeek(infologMessage, 1); // The first element is message type
msgText = conpeek(infologMessage, 2); // The second element is the message text
// Append each message followed by a newline
// If you plan to send via email or store in a table, this formatting can be adjusted
result += msgText + "\r\n";
}
return result;
}
This method gives you a single string containing all infolog messages generated during the batch run. You can call retrieveInfologMessages() near the end of your batch logic to capture everything that has occurred.
Practical Applications
Once you have your infolog messages retrieved as a single string, there are multiple ways to utilize them:
1. Sending the Messages via Email
If you want to notify someone about the outcome of the batch job, you can send the string of messages in an email. For example:
str messages = retrieveInfologMessages();
// Assuming you have a mail utility class or SysEmail framework in place
// For HTML emails, consider replacing "\r\n" with "<br>" for proper line breaks.
EmailUtility::sendEmail(
"recipient@example.com",
"Batch Job Results",
messages, // Body of the email
false // Indicates plain text email; set to true or adjust for HTML if needed
);
If line breaks don’t appear correctly in the email, try the following:
- Use
\r\nfor new lines in plain text emails. - Use
<br>tags if you’re sending HTML emails. Make sure the email is formatted as HTML in that case.
2. Storing Messages in a Custom Table
You might prefer to archive these results for auditing or reporting. Let’s assume you have a custom table named CustomBatchJobLog with fields for BatchJobId, LogMessage, and CreatedDateTime:
public static void storeMessagesInTable(RecId _batchJobRecId)
{
CustomBatchJobLog logRecord;
str messages = retrieveInfologMessages();
container lines = strSplit(messages, "\r\n"); // Split into separate messages if needed
int lineCount = conlen(lines);
int i;
for (i = 1; i <= lineCount; i++)
{
str singleMessage = conpeek(lines, i);
if (singleMessage != "")
{
logRecord.clear();
logRecord.BatchJobId = _batchJobRecId;
logRecord.LogMessage = singleMessage;
logRecord.CreatedDateTime = DateTimeUtil::utcNow();
logRecord.insert();
}
}
}
This code takes the retrieved messages, splits them by line, and inserts each message as a separate record into your custom table. You can then run reports, queries, or analyses on these stored logs later.
Conclusion
By accessing the infolog from within the batch job, you gain full control over how you leverage the messages generated during processing. Whether you choose to send them as email notifications to your stakeholders or store them in a custom table for long-term analysis, retrieving the infolog at runtime gives you invaluable insights and flexibility.
This approach simplifies troubleshooting, auditing, and communication regarding the batch job’s execution, ensuring you make the most of the messages D365FO provides.
Leave a comment