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 :
Microsoft Dynamics NAV (Archived)

ReRun CodeUnit

(0) ShareShare
ReportReport
Posted on by 273

I have a codeunit with several functions that calculates a minimum price according to certain costs, and then inserts in the table Sales Line.
I corrected some functions who calculated some costs, is there any way to rerun this codeunit for the current year of 2017 to correct possible errors previously calculated and inserted in the table sales line.
I thought I'd create a report to run the code unit but I do not know how.

Here is the main function with in the codeunit:

GetPVmin(VAR SalesLine : Record "Sales Line";ShipAgent : Code[20];Contacto : Code[20];ShipPostCode : Code[20];Country : Code[10]) : Decimal

SalesLine.SETFILTER(Type,'<>0');

IF SalesLine.FINDSET THEN
BEGIN

IF SalesLine."Document Type"<>SalesLine."Document Type"::Order THEN
  EXIT;

   ltcontact.GET(Contacto);
   IF NOT ltTabDiv.GET('RUBCUSTO',0,'TAXAJURO') THEN
   EXIT;
   ltLocation.GET(SalesLine."Location Code");
   IF ltLocation."Country/Region Code"='' THEN
   ltLocation."Country/Region Code":='PT';
   IF Country='' THEN
    Country:='PT';
   KgTotais:=0;
   distancia:=GetDist(ltLocation."Post Code",ShipPostCode,ltLocation."Country/Region Code",Country);

   REPEAT
     
    IF NOT SalesLine."Item Container" THEN
      KgTotais+=SalesLine.Quantity
    ELSE
    BEGIN
      ltValueEntry."Entry No.":=SalesLine."Attached to Line No.";
      ltValueEntry."Item No.":=SalesLine."No.";
      ltValueEntry."Valued Quantity":=SalesLine.Quantity;
      ltValueEntry.INSERT;
    END;
   UNTIL SalesLine.NEXT=0;

   SalesLine.SETRANGE("Item Container",FALSE);

    

  //roliveira 2017-10-04 begin  
   VVasilhame:=0; 
   IF SalesLine.FINDSET THEN
   REPEAT

    
     SalesLine2:=SalesLine;
     SalesLine.SETRANGE("Item Container");
     SalesLine.SETRANGE("Document Type", SalesLine."Document Type");
     SalesLine.SETFILTER("Document No.",  SalesLine."Document No.");
     SalesLine.SETRANGE("Attached to Line No.", SalesLine."Line No.");
     QProduto:=0;
     QVasilhame:=0;
     IF SalesLine.FINDFIRST THEN
       BEGIN
         QProduto:=SalesLine2.Quantity;
         QVasilhame:=SalesLine.Quantity;

       END;
         IF QVasilhame<>0 THEN
          VVasilhame:=QProduto/QVasilhame
        ELSE
          VVasilhame:=1;   
     SalesLine.SETRANGE("Attached to Line No.");
     SalesLine.SETRANGE("Item Container",FALSE); 
     SalesLine.GET(SalesLine2."Document Type",SalesLine2."Document No.",SalesLine2."Line No."); 
 
    

      SalesLine."Custo Operação":=GetCustoOperacao(SalesLine."No.",SalesLine."Variant Code")/VVasilhame;   
      IF SalesLine."Codigo Vasilhame"<>'' THEN
      BEGIN
       SalesLine."Custo Vasilhame":=GetCustoVasilhame(SalesLine."Codigo Vasilhame",0,SalesLine."Vasilhame Devolver")/VVasilhame;
      END;
      SalesLine."Custo Stock":=GetCustoStock(SalesLine."No.");
      SalesLine."Custo Fornecedor":=GetCustoFornecedor(SalesLine."No.");
      SalesLine."Custo Transporte":=GetCustoTransporte(ShipAgent,ltLocation.Code,KgTotais,distancia,COPYSTR(ShipPostCode,1,2));
      SalesLine."Custo Transferencia":=GetCustoTransferencia(ltLocation.Code);
      ltitem.GET(SalesLine."No.");
      SalesLine."Unit Cost":=ltitem."Unit Cost";
      SalesLine."PV Minimo":=(ltitem."Unit Cost"+SalesLine."Custo Operação"+SalesLine."Custo Vasilhame"+SalesLine."Custo Stock"-SalesLine."Custo Fornecedor"+SalesLine."Custo Transporte"+SalesLine."Custo Transferencia")
      /(1-ltTabDiv.Custo*GETdiasCliente(Contacto)/365-GetMargemMin(SalesLine."No.",ltcontact.Segmentacao,SalesLine.Quantity));
      SalesLine."PV Aconselhado":=(ltitem."Unit Cost"+SalesLine."Custo Operação"+SalesLine."Custo Vasilhame"+SalesLine."Custo Stock"-SalesLine."Custo Fornecedor"+SalesLine."Custo Transporte"+SalesLine."Custo Transferencia")
      /(1-ltTabDiv.Custo*GETdiasCliente(Contacto)/365-GetMargemDef(SalesLine."No.",ltcontact.Segmentacao,SalesLine.Quantity));
      SalesLine."Custo Cliente":=SalesLine."PV Minimo"*ltTabDiv.Custo*GETdiasCliente(Contacto)/365;
      SalesLine.Margem:=GetMargemMin(SalesLine."No.",ltcontact.Segmentacao,SalesLine.Quantity)*SalesLine."PV Minimo";
      GetUltimaVenda(SalesLine);
      GetUltimaProposta(SalesLine);
      SalesLine.MODIFY;
      
   
   UNTIL SalesLine.NEXT=0;
END;
SalesLine.RESET;
SalesLine.SETRANGE("Document Type",SalesLine."Document Type");
SalesLine.SETRANGE("Document No.",SalesLine."Document No.");


*This post is locked for comments

I have the same question (0)
  • Mohana Yadav Profile Picture
    61,005 Super User 2025 Season 2 on at

    if it gets calculate while validation quantity etc

    then you can run process only report by validating qty

    SalesLine.Validate(Quantity);

  • Rikarddo Profile Picture
    273 on at

    Can you please explain better?

  • Verified answer
    Mohana Yadav Profile Picture
    61,005 Super User 2025 Season 2 on at

    Are you getting correct values when you enter quantity in new line of sales line?

    if yes then you can create a process only report to validate quantity in existing sales lines which will get correct values

  • Verified answer
    Zaid Tariq Profile Picture
    2,274 on at

    Hi,

    Create a processing only report and in the dataset add the Sales Line as the dataset. On OnAfterGetRecord trigger write your logic.

    Let me know if you need more elaboration. Thanks

  • Rikarddo Profile Picture
    273 on at

    Thanks. That was what i did.

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 > 🔒一 Microsoft Dynamics NAV (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans