Hi, Readers.
The public preview for Dynamics 365 Business Central 2024 release wave 2 (BC25) is available. Learn more: Link.
We briefly discussed Business Central 2024 wave 2 (BC25): Use drag and drop on file upload dialog to attach multiple files (New Documents Factbox) a few days ago.
Test video:
So can this standard feature be used for our customization? In this post, I’ll make a simple attempt.
As you might know, there is a very good standard feature in Business Central, Import Item Pictures. You can find out more about Import Multiple Item Pictures from MS Docs.
You can import multiple item pictures in one go. Simply name your picture files with names corresponding to your item numbers, compress them to a zip file, and then use the Import Item Pictures page to manage which item pictures to import.
This feature can also be used on our customizations, but a zip file is required. More details: Dynamics 365 Business Central: How to import files from a zip file (Bulk import attachments and Excel files)
So the requirement this time is that there is no need for a zip file, just select multiple item pictures and import them directly.
First, when developing this requirement, we need to add a new action type, fileuploadaction. And we need to add a new required property, AllowMultipleFiles property.
fileuploadaction: The fileupload keyword defines an action that can accepts files uploaded in Business Central.
AllowMultipleFiles: Specifies if the action accepts multiple files.
PS:
1. The property ‘AllowMultipleFiles’ cannot be used in normal action.
2. The ‘Promoted’ related properties cannot be used in the fileuploadaction.
You need to use actionref. More details: Business Central 2022 wave 2 (BC21) new features: Promoted action groups, action references, and ShowAs Property (New Modern Action Bar)
Oaky, then we can add a new OnAction trigger, trigger OnAction(files: List of [FileUpload])
The new FileUpload data type will be used here.
This data type has two standard methods.
procedure CreateInStream(InStream: InStream, Encoding: TextEncoding): Creates an InStream object for a file. This enables you to import or read data from the file.
Refer to table 1173 “Document Attachment” and you can see that foreach is used to read each file.
procedure FileName(): Text: Gets the file name.
This gets the full file name including the extension.
PS: Dynamics 365 Business Central: How to quickly get the extension name, file name without extension, and Media (MIME) types from an uploaded file
Ok, that’s it for explaining all the new elements, let’s start writing some code.
Test files:
Test video:
It’s very simple. The example we discussed this time used pictures, but any other file format can be processed in this way. Give it a try!!!😁
Source code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)
pageextension 50112 ItemListExt extends "Item List"
{
actions
{
addafter(CopyItem)
{
fileuploadaction(ImportMultipleItemPictures)
{
ApplicationArea = All;
Caption = 'Import Multiple Item Pictures';
Image = Import;
AllowMultipleFiles = true;
trigger OnAction(files: List of [FileUpload])
var
CurrentFile: FileUpload;
InStr: InStream;
FileName: Text;
FileMgt: Codeunit "File Management";
Item: Record Item;
begin
FileName := '';
foreach CurrentFile in Files do begin
CurrentFile.CreateInStream(InStr, TextEncoding::MSDos);
FileName := FileMgt.GetFileNameWithoutExtension(CurrentFile.FileName);
if Item.Get(FileName) then begin
Clear(Item.Picture);
Item.Picture.ImportStream(InStr, 'Demo picture for item ' + Format(Item."No."));
Item.Modify(true);
end;
end;
end;
}
}
addafter(CopyItem_Promoted)
{
actionref(ImportMultipleItemPictures_Promoted; ImportMultipleItemPictures)
{
}
}
}
}
PS:
1. Dynamics 365 Business Central: Importing picture/image from URL
2. Dynamics 365 Business Central: How to import/create item picture from encoded text (Base64 String)
3. Another test video:
END
Hope this will help.
Thanks for reading.
ZHU
コメント