Business Central 2023 wave 1 (BC22): Discovers all orphaned media/media sets (New Media.FindOrphans/MediaSet.FindOrphans method)

Dynamics 365 Business Central

Hi, Readers.
Dynamics 365 Business Central 2023 wave 1 (BC22) is generally available last month. More details: Dynamics 365 Business Central 2023 release wave 1 (BC22)

I will continue to test and share some new features that I hope will be helpful.

This new feature is not mentioned in the 2023 wave1 release plan, but it is in Business Central Launch Event (2023 release wave 1) and AL Language extension changelog version 11.0.

Media.FindOrphans & MediaSet.FindOrphans: New methods for finding media and media sets that are no longer referenced, and can be deleted.

In fact, this information was first made public by Microsoft in October last year when Business Central 2022 release wave 2 was released, but it was postponed until now. The following image is from BCLE rw2 2022.

As you might know, there are two ways that you can upload media in Business Central:

  • Use a BLOB data typeYou add media to a BLOB data type field on the record. For more information, see BLOB Data Type.

For example, Picture in Company Information:

  • Use a Media or MediaSet data typeThis way enables you to store media in system tables of the database, and then reference the media from application records. For example, you can:
    • Display media with records in list type pages, when the page is viewed in the Tile layout. For more information, see Displaying Data as Tiles.
    • Display media on a card type page for a record.
    • Display media in a report.

Attachments:

Customer Picture:

Using the Media or MediaSet data type provides better performance than using a BLOB data type and is more flexible in its design. With a BLOB data type, each time the media is rendered in the client, it’s retrieved from the SQL database server, which requires extra bandwidth and affects performance. With the Media and MediaSet data types, the client uses media ID to cache the media data, which in turn improves the response time for rendering the media in the user interface. More details: Working With Media on Records

Table fields support two data types for adding media to records: Media and MediaSet. With these data types, you can import media directly from a file to a record, or media can be passed to the record in an InStream object.

Imported media is stored as an object in the system table 2000000184 Tenant Media of the tenant database. Each media object is assigned a unique identifier (ID).

If a media object is added to MediaSet data type field, the media object is assigned to a media set in the system table 2000000183 Tenant Media Set. The media set is assigned a unique identifier, which is then referenced from the field. The media set is created with the first file media object that you add on the record. Any other media objects for the record are then associated with the same media set.

But there has always been a problem with the use of Media type. When data is frequently written and deleted, some orphaned data that are not referenced by any other table will appear. How to clean them has always troubled developers. With this wave, we now have the following two new methods to use.

Media.FindOrphans() Method: Discovers all orphaned media. Orphaned media is media that is not referenced by any other table. (VersionAvailable or changed with runtime version 11.0.)

Syntax: Orphans := Media.FindOrphans()

Return Value: Orphans
 Type: List of [Guid]
A list of orphaned media.

MediaSet.FindOrphans() Method: Discovers all orphaned media sets. Orphaned media sets are media sets that are not referenced by any other table. (VersionAvailable or changed with runtime version 11.0.)

Syntax: Orphans := MediaSet.FindOrphans()

Return Value: Orphans
 Type: List of [Guid]
A list of orphaned media sets.

Their return values are both List Data Type. So we can use the following way to clean up all orphaned media/media sets simply.

Source Code:

pageextension 50116 MyExtension extends "Customer List"
{
    actions
    {
        addafter(Email)
        {
            action(DiscoversAllOrphanedMedia)
            {
                Caption = 'Discovers all orphaned Media and MediaSet';
                Promoted = true;
                PromotedCategory = Process;
                PromotedIsBig = true;
                ApplicationArea = All;

                trigger OnAction()
                var
                    OrphanedId: Guid;
                    OrphanedMediaIds: List of [Guid];
                    OrphanedMediaSetIds: List of [Guid];
                    MediaRec: Record "Tenant Media";
                    MediaSetRec: Record "Tenant Media Set";
                begin
                    Clear(Orphanedid);
                    OrphanedMediaIds := Media.FindOrphans();
                    foreach Orphanedid in OrphanedMediaIds do
                        if MediaRec.Get(OrphanedId) then
                            MediaRec.Delete();

                    Clear(Orphanedid);
                    OrphanedMediaSetIds := MediaSet.FindOrphans();
                    foreach Orphanedid in OrphanedMediaSetIds do
                        if MediaSetRec.Get(OrphanedId) then
                            MediaSetRec.Delete();
                end;
            }
        }
    }
}

Give it a try!!!😁

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL