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 :
Small and medium business | Business Central, N...
Answered

Gross Profit Margin Calc

(0) ShareShare
ReportReport
Posted on by 55

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

I have the same question (0)
  • Verified answer
    Francisco Bedolla Profile Picture
    1,123 on at

    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

  • Rajiv Sewsarran Profile Picture
    55 on at

    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

  • Francisco Bedolla Profile Picture
    1,123 on at

    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
    55 on at

    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 %';
                }
            }
        }
    }

  • Rajiv Sewsarran Profile Picture
    55 on at

    HI all,

    Happy to report that i've got this complete.

    Thanks for all your help

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,226

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 2,047 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,257 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans