Dynamics 365 Business Central: How to bulk import marketing text to items – Customization

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to import marketing text to items in Business Central. This is an interesting question I saw on the Dynamics 365 Community last week. More details: Import marketingtext.

For any item registered in Business Central, you can write marketing text about the item. Although marketing text is a kind of description, it’s different than an item’s Description field. The Description field is typically used as a concise display name to quickly identify the product. The marketing text, on the other hand, is a more rich and descriptive text. Its purpose is to add marketing and promotional content, also known as copy.

This text can then be published with the item if it’s published on a web shop, like Shopify, or pasted into emails or other communications with your customers. For example,

More details: Drive sales with AI-generated product descriptions in Business Central

Drive sales with AI-generated product descriptions in Business Central

And there are two ways to create the marketing text. The easiest way to get started is to use Copilot, which suggests AI-generated text for you. The other way is to start from scratch. More details: Add marketing text to items

But no matter which way is used, they must be created one by one. Is there a way to import them? Let’s take a look at how this data is stored in BC.
table 2000000132 “Entity Text”:

You can see the data when you open the table directly in the URL. The data is stored in the Text; Blob field, and the preview (the first 1024 characters) is displayed in “Preview Text”; Text[1024].

As of now, there is only one scenario: Marketing Text.

Table 2000000132 is a system table, so it cannot be imported through the Configuration Package.

You cannot use system table 2000000132 in the package.

So we can only try to customize it. And this time, because we need to import a big/large text text value whose length may exceed 2048, so I will use the method I mentioned in my previous blog, More details: Dynamics 365 Business Central: How to bulk import big/large text (length exceeds 250 and 2048) from external files via AL

First, the file type is CSV. And the template is relatively simple, containing only Item No. and Marketing Text. The values ​​of other fields, Company, Source Table Id, and Source System Id, are directly taken from the code.

Item No.Marketing Text
1896-SA………
1900-SB………
1906-SC………

Test file:

In item 1896-S, I will import a base64 text. This is very long.

Another point to note is that if it already exists, edit the existing data, if not, insert the new data.

PS: I initially wanted to import via Excel (using Excel Buffer), but gave up due to field length limitations. More details: Dynamics 365 Business Central: How to use Excel Buffer to Import data

For the specific code, please refer to the Github. Let’s do a simple test.

Done.

Test video:

Source code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)

pageextension 50119 ItemListExt extends "Item List"
{
    actions
    {
        addafter(CopyItem)
        {
            action(ImportMarketingText)
            {
                ApplicationArea = All;
                Caption = 'Import Marketing Text From csv';
                Promoted = true;
                PromotedCategory = Process;
                Image = Import;
                ToolTip = 'Import marketing text from a csv file.';
                trigger OnAction()
                begin
                    Xmlport.Run(Xmlport::"Import Marketing Text", false, true);
                end;
            }
        }
    }
}

xmlport 50100 "Import Marketing Text"
{
    Caption = 'Import Marketing Text';
    Format = VariableText;
    Direction = Import;
    TextEncoding = UTF8;
    UseRequestPage = false;
    FileName = 'MarketingText.csv';
    TableSeparator = '<NewLine>';

    schema
    {
        textelement(Root)
        {
            tableelement(Integer; Integer)
            {
                XmlName = 'EntityText';

                textelement(ItemNo)
                { }
                textelement(MarketingText)
                {
                    TextType = BigText;
                }

                trigger OnAfterInitRecord()
                begin
                    if IsFirstline then begin
                        IsFirstline := false;
                        currXMLport.Skip();
                    end;
                end;

                trigger OnBeforeInsertRecord()
                var
                    Item: Record Item;
                    InStream: InStream;
                    EntityText: Record "Entity Text";
                    OutStream: OutStream;
                begin
                    if Item.Get(ItemNo) then
                        if EntityText.Get(CompanyName, Item.RecordId.TableNo, Item.SystemId, EntityText.Scenario::"Marketing Text") then begin
                            Clear(EntityText.Text);
                            EntityText.Text.CreateOutStream(OutStream, TEXTENCODING::UTF8);
                            MarketingText.Write(OutStream);
                            EntityText.Text.CreateInStream(InStream, TextEncoding::UTF8);
                            InStream.Read(EntityText."Preview Text");
                            EntityText.Modify();
                            i += 1;
                        end else begin
                            EntityText.Init();
                            EntityText.Company := CompanyName;
                            EntityText."Source Table Id" := Item.RecordId.TableNo;
                            EntityText."Source System Id" := Item.SystemId;
                            EntityText.Scenario := EntityText.Scenario::"Marketing Text";
                            EntityText.Text.CreateOutStream(OutStream, TEXTENCODING::UTF8);
                            MarketingText.Write(OutStream);
                            EntityText.Text.CreateInStream(InStream, TextEncoding::UTF8);
                            InStream.Read(EntityText."Preview Text");
                            EntityText.Insert();
                            i += 1;
                        end;
                    currXMLport.Skip();
                end;
            }
        }
    }

    var
        IsFirstline: Boolean;
        i: Integer;
        Msg: Label 'Imported Marketing text for %1 item(s).';

    trigger OnPreXmlPort()
    begin
        i := 0;
        IsFirstline := true;
    end;

    trigger OnPostXmlPort()
    begin
        Message(Msg, i);
    end;
}

PS: What’s new: Marketing text suggestions (2023 release wave 2)

What's new: Marketing text suggestions (2023 release wave 2)

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL