Working with Mediaset References in Microsoft Dynamics NAV 2017
Mediaset is a new data type introduced with Dynamics NAV 2017. Read the documentation here: https://msdn.microsoft.com/en-us/dynamics-nav/mediaset-data-type.
When working with the Mediaset data type you might want to copy its content from one table to another table but not the same reference.
If the same reference is copied through an assignment statement, any action is and will be performed on the same Mediaset. If a record that holds the Mediaset is deleted, then all other records in different tables that stick with the same Mediaset reference are affected. This results in empty content and no picture is displayed.
Let’s make a simple example.
- Create a custom table like e.g. “Price Quotation Picture” with the following structure:
Item No. Code 20
Main Picture MediaSet
- Create a simple Codeunit where “Item” is a record variable and GET is done on a record that stores a valid MediaSet (e.g. an Item picture) and PriceQuotationPicture is the destination table of the Mediaset Assignment.
…
Item.GET(ItemNo);
PriceQuotationPicture.”Main Picture” := Item.Picture; //Assign Mediaset from source
PriceQuotationPicture.MODIFY(TRUE); //or INSERT, depending on your development
…
If you instruct the application to run the code written above, in the end you will have the MediaSet copied from the source table “Item” to the destination table “Price Quotation Picture”, but also with the same reference.
If you DELETE the source record or the destination record through AL Code or the UI, since both source and destination table hold the same MediaSet reference, the deletion command will move forward and delete contents. This will happen for both tables and both records will display empty Mediaset content (no picture shown).
If your development scope is to copy the content with a different reference, then it is warmly suggested to code or refactor your code based on the following example:
…
Item.GET(ItemNo);
PriceQuotationPicture.INIT;
FOR index := 1 TO Item.Picture.COUNT DO
PriceQuotationPicture.”Main Picture”.INSERT(Item.Picture.ITEM(index));
PriceQuotationPicture.MODIFY(TRUE);
…
This will create a brand new MediaSet that contains so-called shared MediaSet references.
If/when you DELETE the MediaSet (for example, by deleting the target or destination table or associated record) the application runtime will detect that the MediaSet Item is used (shared) in multiple MediaSets and will not delete the MediaSet content.
The Mediaset content will be removed only when the application runtime cannot find other references.
The application runtime will not look in all tables all over the database to see if MediaSet content is used elsewhere. This last action might potentially cause very expensive SQL Server table scan and this is the main technical reason that lies beneath this design.
To resume, then, it is typically recommended to use the direct assignment only if both source and destination tables are the same as well as the Field number. If your development implies that your MediaSet content has to be shared, it should be shared through a common reference table, or by cloning the MediaSet field content as shown above.
These postings are provided “AS IS” with no warranties and confer no rights. You assume all risk for your use.
Duilio Tacconi (dtacconi)
Microsoft Dynamics Italy
Microsoft Customer Service and Support (CSS) EMEA
Thanks to Niels-Henrik Sejthen (Server Backend team MDCC Lyngby)