Search code examples
dynamics-business-centraldynamics-albusinesscentral

Get Marketing Text with Business Central Dynamics AL


I have created a custom extension for a client and one of the things I need to do is fetch the Marketing Text for products. This data is stored in a table called Entity Text and I can't figure out how to get it.

I know that I have to perform a .Get but I don't know how to get all of the primary key components or how to put them together (I've never queried a table with a composite key before). Getting the Source System Id for the record is easy, but it looks like I also need to provide Company, Source Table Id, and Scenario.

I hardcoded the Source Table Id only because I didn't see a way that I thought I could get it programmatically; I would greatly prefer to do that though. I know that the Scenario is supposed to be an enum, but I'm not sure how to find and provide that.

This code is where I try to get the data and it is inside the OnAfterGetCurrRecord

Clear(EntityText); //Do I need to clear before fetching?
if EntityText.Get(Rec.CurrentCompany(), 27, Rec.SystemId, 'Marketing Text') then
    MarketingText := EntityText."Preview Text";

Here are the [I think] pertinent variable declarations

var
    EntityText: Record "Entity Text";
    MarketingText: Text;

Solution

  • I think you are on track, however there are some tweaks to your code that you can do.

    Using Clear is normally considered an anti-pattern. It can be required if you use a variable in a loop or it is declared as global but in both cases one should attempt to refactor the code to e.g. enclose the code in a procedure with local variables thus removing the need to use Clear.

    For the Get on Entity Text you should use the system function CompanyName, Database for the Source Table Id and (as you suggest yourself) the Entity Text Scenario enum for Scenario:

    EntityText.Get(CompanyName, Database::Item, Rec.SystemId, "Entity Text Scenario"::"Marketing Text")
    

    A better solution however is to use codeunit Entity Text and then calling the GetText procedure because this codeunit is the interface for Entity Texts meaning you will have less maintenance on your code in the future.

    The variable should be declared like this:

    EntityText: Codeunit "Entity Text";
    

    And then you call GetText:

    MarketingText := EntityText.GetText(Database::Item, Rec.SystemId, "Entity Text Scenario"::"Marketing Text");