web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :

Business Central master data templates selection with Power Automate ︎

JAngle Profile Picture JAngle 133

Ever needed to specify the master data template/configuration template for creating new master data? As ever a few possibilities exist. I’ll run through a couple I came up with, and what pros and cons they have.

Example I’ll use to illustrate the functionality: two types of vendors are created in a company. To distinguish between them a different number series is used. Simple requirement and there is a way to achieve this with zero modifications and a way with a tiny modification. Up to you which one makes the most sense based on your needs.

Let’s review the standard way of achieving the requirement. You may have noticed the below page in BC:

In principal this table helps with creation of data through an API page. Within this page I’ve stipulated that if data for the vendor table (23) is inserted then a different “Template Code” should be used based on a filter condition. You can setup a series of filtering conditions if you need. I found that I had to alter the data in this page to be as shown above for my example to work. Note that I have no “Page ID” set – out of the box setup does have a page ID set. Another key thing about this page is that it says “Template Code” – which sounds like exactly what we need. However, it actually has a table relationship with “Configuration Templates” – which are the legacy solution for templates used with Configuration Packages. So what? I hear you ask. Well you can’t get “Configuration Templates” to use a specific number series. “No. Series” can be added as a field, but it does not get used.

Is that the standard approach done? Not quiet no. Small caveat of the standard approach is that you will only have 1 number series updated system side. The other one will have to work in a different way. What do I mean? BC keeps tabs on the “Last No. Used”. This stops users/integrations from tripping over one another:

Whats the way around this? The below image gallery shows images of an example Power Automate Flow. This is working on the assumption the trigger is another data source and that knows what type of vendor needs to be created. You can then assess that value and act accordingly. If multiple types are needed then “Switch” could be used instead of a “Condition”, which the example uses. The basic outline of the flow is that you check what was the last number used for that type of vendor. You then take that value and perform a data type conversion whilst adding a 1 to it. A new vendor record is then created in BC using the created number:

Granted if we created an API page for the number series we could go ahead and update the record to ensure we keep tabs on things. This does make this method limiting but no modifications.

How about a way with modifications? When a vendor is created in the regular UI you can be asked which vendor template (providing you have some setup). In this process we need to know up front which vendor template we want to use and get Power Automate to do whatever else we need. Thankfully there isn’t much coding required here as standard BC has most bases covered for this situation. The below code is available in codeunit 1385. It’s not 100% perfect for usage with Power Automate, but we can use it as a “template” – get it!?

codeunit 1385 "Vendor Templ. Mgt."

procedure CreateVendorFromTemplate(var Vendor: Record Vendor; var IsHandled: Boolean) Result: Boolean
    var
        VendorTempl: Record "Vendor Templ.";
    begin
        IsHandled := false;
        OnBeforeCreateVendorFromTemplate(Vendor, Result, IsHandled);
        if IsHandled then
            exit(Result);

        IsHandled := true;

        OnCreateVendorFromTemplateOnBeforeSelectVendorTemplate(Vendor, VendorTempl);
        if not SelectVendorTemplate(VendorTempl) then
            exit(false);

        Vendor.SetInsertFromTemplate(true);
        Vendor.Init();
        InitVendorNo(Vendor, VendorTempl);
        Vendor.Insert(true);
        Vendor.SetInsertFromTemplate(false);

        ApplyVendorTemplate(Vendor, VendorTempl);

        OnAfterCreateVendorFromTemplate(Vendor, VendorTempl);
        exit(true);
    end;

We will need an action which we can call in Power Automate. This is a good guide if the concept is new to you: https://yzhums.com/20111/. This is the code for the action I’ve used, which is on an API page which uses the “Vendor Templ.” as the source table. I’ve included on that page the Code field from the mentioned table. In addition to this API page you will need one for the Vendor table. Only field required is SystemCreatedAt:

[ServiceEnabled]
    procedure SetVendorNoFromTemplate(var actionContext: WebServiceActionContext)
    var
        VendTemplateMgt: Codeunit "Vendor Templ. Mgt.";
        IsHandled: Boolean;
        VendorTemplate: Record "Vendor Templ.";
        Vendor: Record Vendor;
    begin
        VendorTemplate.SetRange(Code, Rec.Code);
        VendorTemplate.FindFirst();
        Vendor.SetInsertFromTemplate(true);
        Vendor.Init();
        VendTemplateMgt.InitVendorNo(Vendor, VendorTemplate);
        Vendor.Insert(true);
        Vendor.SetInsertFromTemplate(false);
        VendTemplateMgt.ApplyVendorTemplate(Vendor, VendorTemplate);
        actionContext.SetResultCode(WebServiceActionResultCode::Created);
    end;

Similar to the 1st Power Automate flow this one picks up after the trigger. The above API page action is called and we pass in the vendor template code. This will create a vendor using the template. We then grab that newly created vendor ID by doing a find on the other API page where we have SystemCreatedAt to query. Once complete we can modify the vendor record created in the 1st action with all the other details we have, such as Name, Address, Contact details etc.

The 2nd option does involve development, but it ensures the number series are maintained correctly. The template mgt. codeunit is the glue that holds this altogether. Thankfully one exists for the Customer, Item and Employee tables too.


This was originally posted here.

Comments

*This post is locked for comments