AL coding with ChatGPT.

AL coding with ChatGPT.

I've always had an interest in the dev side of Business Central, but my job doesn't allow me the time to fully invest. I did a dev course a few years back and passed by the skin of my teeth but if I'm not doing it day-in, day-out then I forget most of it. Which I have. I still try and have a go myself but if I'm not sure then I'll pass it on to our partner to do for me.

I've been having a play with ChatGPT over the last year or so and with the latest release of GPT-4o I thought I'd have a go at an exercise I wanted to do in a BC sandbox environment. So, to set the scene, we use a third-party app for delivery tracking, PODs etc. and this app will consolidate deliveries into one single delivery on the device providing that the Ship-to address is set. A lot of our users use Custom Address which is fine if it's a one-time thing but it's being used most of the time for reoccurring deliveries, so I need to put a stop to this.

So, I started up ChatGPT and asked it to do a basic error (that I specified) that if the Custom Address field is selected on the Sales Order, then to return an error. Now even I can do this so expected to see if it could throw back the code:

layout
    {
        modify(ShippingOptions)
        {
            trigger OnAfterValidate();
            Begin
                if ShipToOptions = ShipToOptions::"Custom Address" then
                    Error('Custom Address is no longer available. Please select or create a Ship-to Address or use the default address.');
            End;
        }        

And what did I get? Well, exactly that! Easy. But now I wanted to go a bit further with this an select a default ShipToOption. Currently, Custom Address is the default on new orders and defaults to Custom Address after I've fetched a customer record on the sales order. So how do I want to control this? Sales & Receivables Setup was the route for me so again, something I can do. Simple table extension that fetches the Enum values of the ShipToOptions and then a Page Extension to show that. So, I asked ChatGPT to write it for me and again, spot on as per below response.

Now I needed it to modify the Sales Order page to create a trigger which again, I could have done but with a bit of thinking back and/or googling to see what I was doing was right. So, I asked GPT to do it for me again. It created me a couple of triggers for OnOpenPage and an OnAfterGetCurrRecord and voila. New extension built within about 5mins that works perfectly.

New Shipping Option selection in Sales & Receivables Setup
Default from the Sales & Receivables Setup applied to the Sales Order
Error when Custom Address is selected.

This was a pretty basic mod to the system, but I have also had it create a processing report for me in our Sandbox. This report I wanted to go and modify the costs of a certain supplier for a % increase. This % increase would be a user input and would allow our purchasing team to update a whole supplier's item ledger within seconds. We have bespoke costings on our system to show rebates etc so forgive me if some of the fields below don't make sense to you, but it created me the report no problems at all. I even told GPT what the naming conventions were of our bespoke fields as part of another extension. It created the report, first time, no errors.

report 99003 "Increase Item Unit Cost"
{
    ApplicationArea = All;
    Caption = 'Increase Suppliers List Price';
    UsageCategory = ReportsAndAnalysis;
    ProcessingOnly = true;
    Permissions = tabledata Item = RIMD;

    dataset
    {
        dataitem(Item; Item)
        {
            RequestFilterFields = "No.", "Vendor No.";

            trigger OnAfterGetRecord()
            begin
                if PercentageIncrease <> 0 then begin
                    Item.Validate("Suppliers List Price TMB68E", Item."Suppliers List Price TMB68E" * (1 + (PercentageIncrease / 100)));
                    Item.Modify(true);
                end;
            end;

            trigger OnPreDataItem()
            begin
                if (Item.GetFilter("No.") = '') and (Item.GetFilter("Vendor No.") = '') then
                    Error(NoFilterSelectedLbl);
            end;
        }
    }

    requestpage
    {
        layout
        {
            area(content)
            {
                group("Update Options")
                {
                    field("Percentage Increase"; PercentageIncrease)
                    {
                        ApplicationArea = All;
                        Caption = 'Percentage Increase';
                        ToolTip = 'Enter the percentage by which the Unit Cost should be increased for the filtered items.';
                    }
                }
            }
        }
    }

    var
        NoFilterSelectedLbl: Label 'You must select at least one filter for items.';
        PercentageIncrease: Decimal;
}        

This isn't me saying GPT can do it all, it's obviously not as black and white but for doing little mods like this that I wanted to have a go at first, I'm pretty impressed! People will say that AI will take over blah blah blah but for me it was a question of did I want to spend an hour or so faffing about or have a tool to help me do it in a tenth of the time? It's a decent companion that is getting better and better and I'm all for it.



要查看或添加评论,请登录

社区洞察

其他会员也浏览了