Codeunit 4582 SOA Retrieve Emails

Sales Order Agent, Microsoft.Agent.SalesOrderAgent

src/Apps/W1/SalesOrderAgent/app/src/Integration/SOARetrieveEmails.Codeunit.al424 lines, Copyright (c) Microsoft Corporation. MIT

// ------------------------------------------------------------------------------------------------// Copyright (c) Microsoft Corporation. All rights reserved.// Licensed under the MIT License. See License.txt in the project root for license information.// ------------------------------------------------------------------------------------------------#pragma warning disable AS0007namespace Microsoft.Agent.SalesOrderAgent;using System.Agents;using System.Email;using System.Telemetry;codeunit 4582 "SOA Retrieve Emails"{    Access = Internal;    Permissions = tabledata "Email Inbox" = rd;    InherentEntitlements = X;    InherentPermissions = X;    TableNo = "SOA Setup";    trigger OnRun()    begin        RetrieveEmails(Rec);    end;    var        SOAImpl: Codeunit "SOA Impl";        SOASetupCU: Codeunit "SOA Setup";        SOAMailSetup: Codeunit "SOA Email Setup";        SOATaskMessage: Codeunit "SOA Task Message";        FeatureTelemetry: Codeunit "Feature Telemetry";        TelemetryNoEmailsFoundLbl: Label 'No emails found.', Locked = true;        TelemetryEmailsFoundLbl: Label 'Emails found.', Locked = true;        AgentTaskTitleLbl: Label 'Email from %1', Comment = '%1 = Sender Name';        TelemetryEmailAddedAsTaskLbl: Label 'Email added as agent task.', Locked = true;        TelemetryAgentTaskNotFoundLbl: Label 'Agent task not found.', Locked = true;        TelemetryAgentTaskMessageExistsLbl: Label 'Agent task message with external message id already exists.', Locked = true;        TelemetryEmailAddedToExistingTaskLbl: Label 'Email added to existing task.', Locked = true;        TelemetryEmailInboxNotFoundLbl: Label 'Email inbox not found.', Locked = true;        MessageTemplateLbl: Label '<b>Subject:</b> %1<br/><b>Body:</b> %2', Comment = '%1 = Subject, %2 = Body';        TelemetrySOAEmailNotModifiedLbl: Label 'SOA Email record not modified.', Locked = true;        TelemetryProcessingLimitReachedLbl: Label 'Processing limit of emails reached.', Locked = true;        PageCountExceededTelemetryTxt: label 'PDF Attachment ignored because it exceeds page count of %1.', Locked = true;        PageCountCallFailedTelemetryTxt: label 'Unable to calculate PDF Attachment''s page count.', Locked = true;        MaxAttachmentsExceededTelemetryTxt: label 'Number of attachments exceeds maximum allowed.', Locked = true;        AttachmentIgnoredTelemetryTxt: Label 'Attachment ignored: %1.', Comment = '%1 = Ignore reason', Locked = true;        IgnoredAttachmentPlaceholderTxt: Label 'The original attachment %1 was not stored. Reason: %2. Original size in bytes: %3.', Comment = '%1 = Original file name, %2 = Ignore reason, %3 = Size of the original attachment in bytes';        IgnoredAttachmentFileSuffixTok: Label '.ignored.txt', Locked = true;        ProcessedCategoryTok: Label 'Processed by Sales Order Agent', Locked = true;    local procedure RetrieveEmails(var SOASetup: Record "SOA Setup")    var        EmailInbox: Record "Email Inbox";        TempFilters: Record "Email Retrieval Filters" temporary;        SOAEmail: Record "SOA Email";        Email: Codeunit "Email";        Processed: Integer;        ProcessLimit: Integer;        TelemetryDimensions: Dictionary of [Text, Text];    begin        if not SOASetupCU.CheckSOASetupStillValid(SOASetup) then            exit;        ProcessLimit := SOAImpl.GetProcessLimitPerDay(SOASetup);        Processed := SOAMailSetup.GetEmailCountProcessedWithin24hrs(SOASetup."User Security ID");        if Processed >= ProcessLimit then begin            TelemetryDimensions.Set('Processed', Format(Processed));            TelemetryDimensions.Set('ProcessLimit', Format(ProcessLimit));            FeatureTelemetry.LogUsage('0000O9Y', SOASetupCU.GetFeatureName(), TelemetryProcessingLimitReachedLbl, TelemetryDimensions);            exit;        end;        TempFilters."Load Attachments" := true;        TempFilters."Max No. of Emails" := SOAMailSetup.GetMaxNoOfEmails();        TempFilters."Last Message Only" := true;        TempFilters."Folder Id" := SOASetup."Email Folder Id";        TempFilters."Earliest Email" := SOASetup."Earliest Sync At";        TempFilters."Category Filter Type" := TempFilters."Category Filter Type"::Exclude;        TempFilters.AddCategoryFilter(ProcessedCategoryTok);        TempFilters.Insert();        Email.RetrieveEmails(SOASetup."Email Account ID", SOASetup."Email Connector", EmailInbox, TempFilters);        if not EmailInbox.FindSet() then            FeatureTelemetry.LogUsage('0000NDN', SOASetupCU.GetFeatureName(), TelemetryNoEmailsFoundLbl, TelemetryDimensions)        else            FeatureTelemetry.LogUsage('0000NDO', SOASetupCU.GetFeatureName(), TelemetryEmailsFoundLbl, TelemetryDimensions);        if not SOASetupCU.CheckSOASetupStillValid(SOASetup) then            exit;        //Get latest instructions from KV        if SOASetupCU.InstructionsSyncRequired(SOASetup) then begin            SOASetupCU.UpdateInstructions(SOASetup);            SOASetupCU.UpdateSOASetupInstructionsLastSync(SOASetup);        end;        AddEmailInboxToSOAEmails(SOASetup, EmailInbox);        Commit();        SOAEmail.SetRange(Processed, false);        SOAEmail.SetRange("Agent User Security ID", SOASetup."User Security ID");        if not SOAEmail.FindSet() then            exit;        repeat            Commit();            OnAddEmailToAgentTask(SOASetup, SOAEmail);            Processed += 1;            if Processed >= ProcessLimit then begin                TelemetryDimensions.Set('Processed', Format(Processed));                TelemetryDimensions.Set('ProcessLimit', Format(ProcessLimit));                FeatureTelemetry.LogUsage('0000O9Z', SOASetupCU.GetFeatureName(), TelemetryProcessingLimitReachedLbl, TelemetryDimensions);                break;            end;        until SOAEmail.Next() = 0;        Commit();    end;    local procedure AddEmailToAgentTask(SOASetup: Record "SOA Setup"; var SOAEmail: Record "SOA Email")    var        EmailInbox: Record "Email Inbox";        AgentTaskBuilder: Codeunit "Agent Task Builder";        TelemetryDimensions: Dictionary of [Text, Text];    begin        if not EmailInbox.Get(SOAEmail."Email Inbox ID") then begin            FeatureTelemetry.LogUsage('0000NJT', SOASetupCU.GetFeatureName(), TelemetryEmailInboxNotFoundLbl, TelemetryDimensions);            SOAEmail.Delete(true);            exit;        end;        if AgentTaskBuilder.TaskExists(SOASetup."User Security ID", EmailInbox."Conversation Id") then            AddEmailToExistingAgentTask(SOASetup, EmailInbox, SOAEmail)        else            AddEmailToNewAgentTask(SOASetup, EmailInbox, SOAEmail);        OnAfterProcessEmail(SOAEmail."Email Inbox ID");    end;    procedure AddEmailInboxToSOAEmails(SOASetup: Record "SOA Setup"; var EmailInbox: Record "Email Inbox")    var        SOAEmail: Record "SOA Email";        Email: Codeunit "Email";    begin        if not EmailInbox.FindSet() then            exit;        repeat            SOAEmail."Email Inbox ID" := EmailInbox.Id;            SOAEmail."Sender Name" := EmailInbox."Sender Name";            SOAEmail."Sender Address" := EmailInbox."Sender Address";            SOAEmail."Sent DateTime" := EmailInbox."Sent DateTime";            SOAEmail."Received DateTime" := EmailInbox."Received DateTime";            SOAEmail."Agent User Security ID" := SOASetup."User Security ID";            if SOAEmail.Insert() then begin                if SOASetup."Mark Email As Read" then                    Email.MarkAsRead(SOASetup."Email Account ID", SOASetup."Email Connector", EmailInbox."External Message Id");                Email.ApplyEmailCategory(SOASetup."Email Account ID", SOASetup."Email Connector", EmailInbox."External Message Id", ProcessedCategoryTok);            end;        until EmailInbox.Next() = 0;    end;    procedure AddEmailToNewAgentTask(var SOASetup: Record "SOA Setup"; var EmailInbox: Record "Email Inbox"; var SOAEmail: Record "SOA Email")    var        AgentTaskMessage: Record "Agent Task Message";        AgentTaskBuilder: Codeunit "Agent Task Builder";        AgentMessageBuilder: Codeunit "Agent Task Message Builder";        EmailMessage: Codeunit "Email Message";        SOABilling: Codeunit "SOA Billing";        MessageText: Text;        AgentTaskTitle: Text[150];        TelemetryDimensions: Dictionary of [Text, Text];    begin        EmailMessage.Get(EmailInbox."Message Id");        MessageText := StrSubstNo(MessageTemplateLbl, EmailMessage.GetSubject(), EmailMessage.GetBody());        AgentTaskTitle := GetAgentTaskTitle(EmailInbox."Sender Name");        AgentMessageBuilder.Initialize(EmailInbox."Sender Address", MessageText)            .SetMessageExternalID(EmailInbox."External Message Id")            .SetRequiresReview(SOATaskMessage.MessageRequiresReview(SOASetup, EmailInbox."Sender Address", true))            .SetIgnoreAttachment(not SOASetup."Analyze Attachments");        AgentTaskBuilder.Initialize(SOASetup."User Security ID", AgentTaskTitle)            .SetExternalId(EmailInbox."Conversation Id")            .AddTaskMessage(AgentMessageBuilder);        AddEmailAttachmentToTaskMessage(AgentMessageBuilder, EmailMessage, SOASetup);        AgentTaskBuilder.Create();        AgentTaskMessage := AgentTaskBuilder.GetAgentTaskMessageCreated();        SOAEmail.SetAgentMessageFields(AgentTaskMessage);        SetAttachmentsTransferred(SOAEmail, EmailMessage);        SOAEmail.Modify();        SOABilling.LogEmailRead(AgentTaskMessage.ID, AgentTaskMessage."Task ID");        FeatureTelemetry.LogUsage('0000NDP', SOASetupCU.GetFeatureName(), TelemetryEmailAddedAsTaskLbl, TelemetryDimensions);    end;    local procedure AddEmailAttachmentToTaskMessage(AgentTaskMessageBuilder: Codeunit "Agent Task Message Builder"; var EmailMessage: Codeunit "Email Message"; var SOASetupRec: Record "SOA Setup")    var        TempAgentTaskFile: Record "Agent Task File" temporary;        SOASetup: Codeunit "SOA Setup";        InStream: InStream;        FileMIMEType: Text[100];        IsFileMimeTypeSupported: Boolean;        ExceedsPageCountThreshold: Boolean;        PdfContent: Boolean;        Ignore: Boolean;        IgnoredReason: Text[250];        NoOfAttachments: Integer;        AttachmentSizeInBytes: Integer;        SupportedAttachmentLbl: Label 'Email has supported attachment: %1', Locked = true, Comment = '%1 = MIME type of the attachment';        UnsupportedAttachmentLbl: Label 'Email has unsupported attachment: %1', Locked = true, Comment = '%1 = MIME type of the attachment';    begin        if not EmailMessage.Attachments_First() then            exit;        NoOfAttachments := 0;        repeat            if not EmailMessage.Attachments_IsInline() then begin                AttachmentSizeInBytes := EmailMessage.Attachments_GetLength();                FileMIMEType := CopyStr(EmailMessage.Attachments_GetContentType(), 1, 100);                if AttachmentSizeInBytes > SOASetup.GetMaxAttachmentSizeInBytes() then begin                    Ignore := true;                    IgnoredReason := CopyStr(Format(Enum::"SOA Email Attachment Status"::ExceedsFileSize), 1, MaxStrLen(IgnoredReason));                    CreateIgnoredAttachmentPlaceholder(TempAgentTaskFile, InStream, EmailMessage.Attachments_GetName(), IgnoredReason, AttachmentSizeInBytes);                    AgentTaskMessageBuilder.AddAttachment(TempAgentTaskFile."File Name", TempAgentTaskFile."File MIME Type", InStream, Ignore, IgnoredReason);                    LogIgnoredAttachmentTelemetry(SOASetup, IgnoredReason, FileMIMEType, AttachmentSizeInBytes);                end else begin                    EmailMessage.Attachments_GetContent(InStream);                    if not SOASetupRec."Analyze Attachments" then begin                        Ignore := true;                        IgnoredReason := CopyStr(Format(Enum::"SOA Email Attachment Status"::AnalyzeAttachmentsNotEnabled), 1, MaxStrLen(IgnoredReason));                        AgentTaskMessageBuilder.AddAttachment(EmailMessage.Attachments_GetName(), FileMIMEType, InStream, Ignore, IgnoredReason);                    end else begin                        ExceedsPageCountThreshold := false;                        IsFileMimeTypeSupported := SOASetup.SupportedAttachmentContentType(FileMIMEType);                        if IsFileMimeTypeSupported then begin                            PdfContent := SOASetup.IsPdfAttachmentContentType(FileMIMEType);                            if PdfContent then begin                                if not SOASetup.DocumentExceedsPageCountThreshold(InStream, ExceedsPageCountThreshold) then                                    FeatureTelemetry.LogError('0000QHK', SOASetup.GetFeatureName(), 'Document exceeds page count threshold', PageCountCallFailedTelemetryTxt);                                if ExceedsPageCountThreshold then                                    FeatureTelemetry.LogUsage('0000QHL', SOASetup.GetFeatureName(), StrSubstNo(PageCountExceededTelemetryTxt, Format(SOASetup.PageCountThreshold())));                                // The page count check consumes the stream, so a fresh stream is requested afterwards.                                EmailMessage.Attachments_GetContent(InStream);                            end;                        end;                        Ignore := IgnoreAttachment(IsFileMimeTypeSupported, ExceedsPageCountThreshold, NoOfAttachments, SOASetupRec, IgnoredReason);                        AgentTaskMessageBuilder.AddAttachment(EmailMessage.Attachments_GetName(), FileMIMEType, InStream, Ignore, IgnoredReason);                        if Ignore then                            LogIgnoredAttachmentTelemetry(SOASetup, IgnoredReason, FileMIMEType, AttachmentSizeInBytes);                        // Log telemetry for SOA session                        if IsFileMimeTypeSupported then                            FeatureTelemetry.LogUsage('0000QBM', SOASetup.GetFeatureName(), StrSubstNo(SupportedAttachmentLbl, FileMIMEType))                        else                            FeatureTelemetry.LogUsage('0000QBN', SOASetup.GetFeatureName(), StrSubstNo(UnsupportedAttachmentLbl, FileMIMEType));                    end;                end;                if not Ignore then                    NoOfAttachments += 1;            end;        until EmailMessage.Attachments_Next() = 0;        if NoOfAttachments > SOASetupCU.GetMaxNoOfAttachmentsPerEmail() then            FeatureTelemetry.LogUsage('0000QK9', SOASetup.GetFeatureName(), MaxAttachmentsExceededTelemetryTxt);    end;    local procedure IgnoreAttachment(IsFileMimeTypeSupported: Boolean; ExceedsPageCountThreshold: Boolean; NoOfAttachments: Integer; var SOASetupRec: Record "SOA Setup"; var IgnoredReason: Text[250]): Boolean    begin        IgnoredReason := '';        if not SOASetupRec."Analyze Attachments" then begin            IgnoredReason := CopyStr(Format(Enum::"SOA Email Attachment Status"::AnalyzeAttachmentsNotEnabled), 1, MaxStrLen(IgnoredReason));            exit(true);        end;        if not IsFileMimeTypeSupported then begin            IgnoredReason := CopyStr(Format(Enum::"SOA Email Attachment Status"::UnsupportedFormat), 1, MaxStrLen(IgnoredReason));            exit(true);        end;        if ExceedsPageCountThreshold then begin            IgnoredReason := CopyStr(Format(Enum::"SOA Email Attachment Status"::ExceedsPageCount), 1, MaxStrLen(IgnoredReason));            exit(true);        end;        if NoOfAttachments >= SOASetupCU.GetMaxNoOfAttachmentsPerEmail() then begin            IgnoredReason := CopyStr(Format(Enum::"SOA Email Attachment Status"::ExceedsNumberOfAttachments), 1, MaxStrLen(IgnoredReason));            exit(true);        end;        exit(false);    end;    local procedure CreateIgnoredAttachmentPlaceholder(var TempAgentTaskFile: Record "Agent Task File" temporary; var PlaceholderInStream: InStream; FileName: Text; IgnoredReason: Text[250]; AttachmentSizeInBytes: Integer)    var        PlaceholderOutStream: OutStream;    begin        // The attachment content is not stored, but the platform rejects an agent task file without content,        // so a short note is stored instead of the original file.        Clear(TempAgentTaskFile);        TempAgentTaskFile."File Name" := CopyStr(FileName, 1, MaxStrLen(TempAgentTaskFile."File Name") - StrLen(IgnoredAttachmentFileSuffixTok)) + IgnoredAttachmentFileSuffixTok;        TempAgentTaskFile."File MIME Type" := 'text/plain';        TempAgentTaskFile.Content.CreateOutStream(PlaceholderOutStream, TextEncoding::UTF8);        PlaceholderOutStream.WriteText(StrSubstNo(IgnoredAttachmentPlaceholderTxt, FileName, IgnoredReason, AttachmentSizeInBytes));        TempAgentTaskFile.Content.CreateInStream(PlaceholderInStream, TextEncoding::UTF8);    end;    local procedure LogIgnoredAttachmentTelemetry(SOASetup: Codeunit "SOA Setup"; IgnoredReason: Text[250]; FileMIMEType: Text[100]; AttachmentSizeInBytes: Integer)    var        TelemetryDimensions: Dictionary of [Text, Text];    begin        TelemetryDimensions.Set('IgnoredReason', IgnoredReason);        TelemetryDimensions.Set('FileMIMEType', FileMIMEType);        TelemetryDimensions.Set('AttachmentSizeInBytes', Format(AttachmentSizeInBytes));        FeatureTelemetry.LogUsage('0000UMB', SOASetup.GetFeatureName(), StrSubstNo(AttachmentIgnoredTelemetryTxt, IgnoredReason), TelemetryDimensions);    end;    local procedure SetAttachmentsTransferred(var SOAEmail: Record "SOA Email"; EmailMessage: Codeunit "Email Message")    begin        SOAEmail."Attachment Transferred" := not EmailMessage.Attachments_First();    end;    procedure AddEmailToExistingAgentTask(SOASetup: Record "SOA Setup"; EmailInbox: Record "Email Inbox"; var SOAEmail: Record "SOA Email")    var        AgentTaskRecord: Record "Agent Task";        AgentTaskMessage: Record "Agent Task Message";        AgentTaskMessageBuilder: Codeunit "Agent Task Message Builder";        EmailMessage: Codeunit "Email Message";        SOABilling: Codeunit "SOA Billing";        MessageText: Text;        TelemetryDimensions: Dictionary of [Text, Text];    begin        AgentTaskRecord.ReadIsolation(IsolationLevel::ReadCommitted);        AgentTaskRecord.SetRange("External ID", EmailInbox."Conversation Id");        if not AgentTaskRecord.FindFirst() then begin            FeatureTelemetry.LogError('0000NDX', SOASetupCU.GetFeatureName(), 'Find Agent Task', TelemetryAgentTaskNotFoundLbl, GetLastErrorCallStack(), TelemetryDimensions);            exit;        end;        AgentTaskMessage.ReadIsolation(IsolationLevel::ReadCommitted);        AgentTaskMessage.SetRange("Task ID", AgentTaskRecord.ID);        AgentTaskMessage.SetRange("External ID", EmailInbox."External Message Id");        if AgentTaskMessage.Count() >= 1 then begin            FeatureTelemetry.LogUsage('0000OFS', SOASetupCU.GetFeatureName(), TelemetryAgentTaskMessageExistsLbl, TelemetryDimensions);            exit;        end;        EmailMessage.Get(EmailInbox."Message Id");        MessageText := StrSubstNo(MessageTemplateLbl, EmailMessage.GetSubject(), EmailMessage.GetBody());        AgentTaskMessageBuilder.Initialize(EmailInbox."Sender Address", MessageText)            .SetMessageExternalID(EmailInbox."External Message Id")            .SetRequiresReview(SOATaskMessage.MessageRequiresReview(SOASetup, EmailInbox."Sender Address", false))            .SetIgnoreAttachment(not SOASetup."Analyze Attachments")            .SetAgentTask(AgentTaskRecord);        AddEmailAttachmentToTaskMessage(AgentTaskMessageBuilder, EmailMessage, SOASetup);        AgentTaskMessage := AgentTaskMessageBuilder.Create();        SOAEmail.SetAgentMessageFields(AgentTaskMessage);        SetAttachmentsTransferred(SOAEmail, EmailMessage);#pragma warning disable AA0214        SOAEmail.Modify();#pragma warning restore AA0214        SOABilling.LogEmailRead(AgentTaskMessage.ID, AgentTaskMessage."Task ID");        FeatureTelemetry.LogUsage('0000NGP', SOASetupCU.GetFeatureName(), TelemetryEmailAddedToExistingTaskLbl, TelemetryDimensions);    end;    internal procedure GetAgentTaskTitle(SenderName: Text[250]): Text[150]    var        BlankAgentTaskRecord: Record "Agent Task";    begin        exit(CopyStr(StrSubstNo(AgentTaskTitleLbl, SenderName), 1, MaxStrLen(BlankAgentTaskRecord.Title)));    end;    [InternalEvent(false, true)]    local procedure OnAddEmailToAgentTask(SOASetup: Record "SOA Setup"; var SOAEmail: Record "SOA Email")    begin    end;    [InternalEvent(false, true)]    local procedure OnAfterProcessEmail(EmailInboxId: BigInteger)    begin    end;    [EventSubscriber(ObjectType::Codeunit, Codeunit::"SOA Retrieve Emails", 'OnAddEmailToAgentTask', '', false, false)]    local procedure OnAddEmailToAgentTaskSubscriber(SOASetup: Record "SOA Setup"; var SOAEmail: Record "SOA Email")    begin        AddEmailToAgentTask(SOASetup, SOAEmail);    end;    [EventSubscriber(ObjectType::Codeunit, Codeunit::"SOA Retrieve Emails", 'OnAfterProcessEmail', '', false, false)]    local procedure OnAfterEmailProcessed(EmailInboxId: BigInteger)    var        SOAEmail: Record "SOA Email";        TelemetryDimensions: Dictionary of [Text, Text];    begin        SOAEmail.Get(EmailInboxId);        SOAEmail.Processed := true;        if not SOAEmail.Modify() then begin            TelemetryDimensions.Set('EmailInboxID', Format(EmailInboxId));            FeatureTelemetry.LogUsage('0000OA0', SOASetupCU.GetFeatureName(), TelemetrySOAEmailNotModifiedLbl, TelemetryDimensions);        end;    end;    [EventSubscriber(ObjectType::Table, Database::"SOA Email", 'OnBeforeDeleteEvent', '', false, false)]    local procedure OnBeforeDeleteSOAEmailEvent(var Rec: Record "SOA Email"; RunTrigger: Boolean)    var        EmailInbox: Record "Email Inbox";    begin        EmailInbox.Id := Rec."Email Inbox ID";        if EmailInbox.Delete(true) then;    end;}