Skip to content

Commit 4b7a926

Browse files
authored
[27.2] Agent Task Message - Allow downloading multiple attachments (#5696)
#### Summary When trying to download multiple attachments, only the last one was downloaded. This is a limitation in BC. Solution: Make a zip out of them if there's more than one, like the Email module does. #### Work Item(s) Fixes [AB#615056](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/615056)
1 parent 31cd789 commit 4b7a926

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/System Application/App/Agent/Interaction/Internal/AgentMessageImpl.Codeunit.al

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
namespace System.Agents;
77

8+
using System.IO;
9+
using System.Utilities;
10+
811
codeunit 4308 "Agent Message Impl."
912
{
1013
Access = Internal;
@@ -13,6 +16,7 @@ codeunit 4308 "Agent Message Impl."
1316

1417
var
1518
GlobalIgnoreAttachment: Boolean;
19+
AttachmentsFilenameLbl: Label 'attachments_task%1_msg%2.zip', Comment = 'Filename format for downloading multiple attachments as a zip file. %1 = Task ID, %2 = Message ID';
1620

1721
procedure GetText(var AgentTaskMessage: Record "Agent Task Message"): Text
1822
var
@@ -94,15 +98,47 @@ codeunit 4308 "Agent Message Impl."
9498
procedure DownloadAttachments(var AgentTaskMessage: Record "Agent Task Message")
9599
var
96100
AgentTaskMessageAttachment: Record "Agent Task Message Attachment";
101+
AgentTaskFile: Record "Agent Task File";
102+
DataCompression: Codeunit "Data Compression";
103+
TempBlob: Codeunit "Temp Blob";
104+
AgentTaskImpl: Codeunit "Agent Task Impl.";
105+
FileInStream: InStream;
106+
ZipOutStream: OutStream;
107+
ZipInStream: InStream;
108+
FileName: Text;
109+
AttachmentCount: Integer;
110+
DownloadDialogTitleLbl: Label 'Download Email Attachment';
97111
begin
98112
AgentTaskMessageAttachment.SetRange("Task ID", AgentTaskMessage."Task ID");
99113
AgentTaskMessageAttachment.SetRange("Message ID", AgentTaskMessage.ID);
100114
if not AgentTaskMessageAttachment.FindSet() then
101115
exit;
102116

103-
repeat
117+
// Count attachments
118+
AttachmentCount := AgentTaskMessageAttachment.Count();
119+
120+
// If single file, download directly
121+
if AttachmentCount = 1 then begin
104122
ShowOrDownloadAttachment(AgentTaskMessageAttachment."Task ID", AgentTaskMessageAttachment."File ID", true);
123+
exit;
124+
end;
125+
126+
// If multiple files, create a zip
127+
DataCompression.CreateZipArchive();
128+
repeat
129+
if AgentTaskFile.Get(AgentTaskMessageAttachment."Task ID", AgentTaskMessageAttachment."File ID") then begin
130+
AgentTaskFile.CalcFields(Content);
131+
AgentTaskFile.Content.CreateInStream(FileInStream, AgentTaskImpl.GetDefaultEncoding());
132+
DataCompression.AddEntry(FileInStream, AgentTaskFile."File Name");
133+
end;
105134
until AgentTaskMessageAttachment.Next() = 0;
135+
136+
TempBlob.CreateOutStream(ZipOutStream);
137+
DataCompression.SaveZipArchive(ZipOutStream);
138+
DataCompression.CloseZipArchive();
139+
TempBlob.CreateInStream(ZipInStream);
140+
FileName := StrSubstNo(AttachmentsFilenameLbl, Format(AgentTaskMessage."Task ID"), Format(AgentTaskMessage.ID));
141+
File.DownloadFromStream(ZipInStream, DownloadDialogTitleLbl, '', '', FileName);
106142
end;
107143

108144
procedure ShowOrDownloadAttachment(TaskID: BigInteger; FileID: BigInteger; ForceDownloadAttachment: Boolean)

src/System Application/App/Agent/app.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@
5656
"name": "DotNet Aliases",
5757
"publisher": "Microsoft",
5858
"version": "27.2.0.0"
59+
},
60+
{
61+
"id": "e31ad830-3d46-472e-afeb-1d3d35247943",
62+
"name": "BLOB Storage",
63+
"publisher": "Microsoft",
64+
"version": "27.2.0.0"
65+
},
66+
{
67+
"id": "008c9419-0c3b-4d08-b03e-84e3adca689f",
68+
"name": "Data Compression",
69+
"publisher": "Microsoft",
70+
"version": "27.2.0.0"
5971
}
6072
],
6173
"propagateDependencies": true,

0 commit comments

Comments
 (0)