Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

Gross Profit Margin Calc

(0) ShareShare
ReportReport
Posted on by 41

Hi all,

I'm continuing my testing of coding on BC and have come across a requirement that involves adding a line to the Sales Order form for Gross Profit Margin.

In my testing i found that I couldn't get the Unit Cost of the item on the sales Line as it was in the Item table.

So my thinking was, i should try to get it on the Item lookup table just as a starting position.

This is the code i used

 

tableextension 50112 GPM extends Item
{
    fields
    {
        field(50113; UC2; Decimal)
        {
            TableRelation = item."Unit Cost" where("No." = field("No."));
        }
        field(50112; GPM; Decimal)
        {
            DataClassification = ToBeClassified;
            Caption = 'GPM';
        }
    }




    procedure GetGPM(): Decimal
    begin
        calcfields("Unit Price", "Unit Cost");
        exit(Rec."Unit Price" - Rec."Unit Cost");
    end;


}



pageextension 50112 GPM extends "Item Lookup"
{
    layout
    {
        addafter("Unit Cost")
        {
            field(GPM; Rec.GPM)
            {
                ApplicationArea = all;
                caption = 'GPM';
            }
        }
    }

it returns 0.00 for everything can someone guide me ? 

Thanks! 

coding noob

  • Rajiv Sewsarran Profile Picture
    41 on at
    RE: Gross Profit Margin Calc

    HI all,

    Happy to report that i've got this complete.

    Thanks for all your help

  • Rajiv Sewsarran Profile Picture
    41 on at
    RE: Gross Profit Margin Calc

    This is what i came up with, but it isn't giving me the correct figure. its for the Gorss Profit Margin for the entire document i need.

    tableextension 50132 GPM extends "Sales Line"
    {
        fields
        {
            field(50132; GPM; Decimal)
            {
                FieldClass = FlowField;
                CalcFormula = sum(Item."Unit Cost" where("No." = field("No.")));
            }
        }
    
        procedure GP(): Decimal
    
        begin
            CalcFields("Outstanding Amount (LCY)", GPM);
            exit(Rec."Outstanding Amount (LCY)" / rec.GPM)
        end;
    
    
    }
    
    
    
    
    
    
    
    pageextension 50132 GrossProfit extends "Sales Quote Subform"
    {
        layout
        {
            addafter("Subtotal Excl. VAT")
            {
                field(GPM; Rec.GPM)
                {
                    ApplicationArea = all;
                    Caption = 'Gross Margin %';
                }
            }
        }
    }

  • Francisco Bedolla Profile Picture
    1,117 on at
    RE: Gross Profit Margin Calc

    If you do what I told you, you have the calculated field in the sales lines

    if you want the total gross margin , you can put a new field in the sales header, and the new field need to be a calcfield which sums the sales line filtered by "document no.", you can check the T36 field 60 or 61 to get an idea. and putting this in the sales header page is easy as make an extension and put the field in the area that you want (general, invoicing, etc).

    you need to check my instructions about the field calculatiosn in item because is different if you want a sum or a difference, but calling the fild is the same as I wrote in the previous answer.

  • Rajiv Sewsarran Profile Picture
    41 on at
    RE: Gross Profit Margin Calc

    HiFrancisco,

    thanks for your response.

    If i wanted to go straight ahead and create a page extension for the gross profit margin, how would i go about getting the calformula to look at the item lookup?

    It would need to take all items on the sales order into consideration.

    eg

    4 different items on the sales Order , i would need to get the gross profit margin for the 4 items

  • Verified answer
    Francisco Bedolla Profile Picture
    1,117 on at
    RE: Gross Profit Margin Calc

    Well, you are extending the table item with a new field in the same table, maybe you want to create a minimum cost with the item.unit cost  plus 1+gpm, is that right?

    first, you dont need to add a table relation, use a direct call to the field, you are in the same rec.

    second, you need to add a condition, if gpm <> 0 then uc2:= item.unit cost * 1+(gpm/100) else uc2:= item unit cost; in the unit cost and the gpm validate.

    third, in the sales line you can add the field an then add the call in the field value subscribing to the OnAfterCopyFromItem(Rec, Item, CurrFieldNo); proc in table 37 validating the gpm field first and the copying the result to th esales line field

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,886 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,766 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans