Function - Record won't initialize

RikarddoRikarddo Member Posts: 80
Hello,
I have been programming in C/AL for some time but I have a doubt in this particular case.

I'm trying to get a cost of a particular product line from an order, in which the dividend is the result of another function, and the divisor will be a new variable as the code shows below:

GetPVmin(VAR SalesLine : Record "Sales Line";VAR SalesLine2 : 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);

VVasilhame:=0;
IF SalesLine.FINDSET THEN
REPEAT
SalesLine2.RESET;
SalesLine2.SETRANGE("Document Type", SalesLine."Document Type");
SalesLine2.SETRANGE(SalesLine2."Document No.", SalesLine."Document No.");
SalesLine2.SETRANGE("Attached to Line No.", SalesLine."Line No.");
QProduto:=0;
QVasilhame:=0;
IF SalesLine2.FINDFIRST THEN
IF (SalesLine2."Attached to Line No." <>0) THEN
BEGIN
QProduto+=SalesLine.Quantity;
QVasilhame+=SalesLine2.Quantity;
VVasilhame:=QProduto/QVasilhame;

END;
SalesLine."Custo Operação":=GetCustoOperacao(SalesLine."No.",SalesLine."Variant Code")/(SalesLine.Quantity); <- replace by VVasilhame.

//VVasilhame will be always 0

The SalesLine2 won't initialize or won't get any value

Any sugestions of what am i doing wrong?

Comments

  • guidorobbenguidorobben Member Posts: 157
    this is not the complete function. you are missing a repeat. And what does this do?

    SalesLine2.SETRANGE("Attached to Line No.", SalesLine."Line No.");
  • RikarddoRikarddo Member Posts: 80
    You are right guidorobben, rhere are a few more calculations but it ends like this:
    ...
    UNTIL SalesLine.NEXT=0;
    END;
    SalesLine.RESET;
    SalesLine.SETRANGE("Document Type",SalesLine."Document Type");
    SalesLine.SETRANGE("Document No.",SalesLine."Document No.");

    SalesLine2.SETRANGE("Attached to Line No.", SalesLine."Line No."); this code tries to differentiate products in which "Attached to line no." <> 0 is a container and "line no." = 0 is a normal product.

    The objective is to obtain the volume of the container dividing the number of products by the number of containers in the order and thus obtaining the cost.
  • RikarddoRikarddo Member Posts: 80
    Really need some help here..

    SalesLine2.SETRANGE("Document Type", SalesLine."Document Type"); -> Here it gets the filter
    SalesLine2.SETRANGE(SalesLine2."Document No.", SalesLine."Document No."); -> Not getting the filter or 0
    SalesLine2.SETRANGE("Attached to Line No.", SalesLine."Line No."); Not getting the filter or 0

    Any ideas?
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    If you could start from pasting the complete function code and formatting it as a code 39p6399pk7if.png
    It would make helping you so much easier...
    Slawek
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • RikarddoRikarddo Member Posts: 80
    GetPVmin(VAR SalesLine : Record "Sales Line";SalesLine2 : Record "Sales Line";ShipAgent : Code[20];Contacto : Code[20];ShipPostCode : Code[20];Country : Code[10]) : Decimal
    SalesLine.SETFILTER(Type,'<>0');
    SalesLine2.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.RESET;
         SalesLine2.SETRANGE("Document Type", SalesLine."Document Type");
         SalesLine2.SETRANGE(SalesLine2."Document No.",  SalesLine."Document No.");
         SalesLine2.SETRANGE("Attached to Line No.", SalesLine."Line No.");
         QProduto:=0;
         QVasilhame:=0;
         IF SalesLine2.FINDFIRST THEN
         IF (SalesLine2."Attached to Line No." <>0) THEN
         BEGIN
             QProduto+=SalesLine.Quantity;
             QVasilhame+=SalesLine2.Quantity;
             VVasilhame:=QProduto/QVasilhame;
    
         
         END;
    
    
    //roliveira end
          SalesLine."Custo Operação":=GetCustoOperacao(SalesLine."No.",SalesLine."Variant Code")/(SalesLine.Quantity);//sugestão : não dividir pela qtd mas sim pelo volume unitário  (Qtd/nºvas)
             
          IF SalesLine."Codigo Vasilhame"<>'' THEN
          BEGIN
           SalesLine."Custo Vasilhame":=GetCustoVasilhame(SalesLine."Codigo Vasilhame",0,SalesLine."Vasilhame Devolver")/(SalesLine.Quantity);//(VVasilhame); //roliveira 2017-09-25 //dividir pelo volume do vasilhame
          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.");
    
  • RikarddoRikarddo Member Posts: 80
    Ok Slawek_Guzek there it its.
  • guidorobbenguidorobben Member Posts: 157
    does it find a salesline2?
  • RikarddoRikarddo Member Posts: 80
    guidorobben No it does not
  • AKAK Member Posts: 226
    Do you see records when you run the salesline table and apply the filter manually?
  • RikarddoRikarddo Member Posts: 80
    Yes
  • RikarddoRikarddo Member Posts: 80
    I'm doing this in a code unit with others functions. If i do it in a Report using dataitem Sales Line (37) it works perfectly. I just can't understand why.
  • RikarddoRikarddo Member Posts: 80
    Well I guess if i'm trying to get a new variable by dividing quantity of item no containers by quantity item that are containers maybe this filter makes no sense now...
    ...
    SalesLine.SETRANGE("Item Container",FALSE); //Remove or re write code
    
    ...
    But i have to see the consequences of the calculations if i remove this filter.
    If i remove all, products (Item or Item containers) will be read.
    Going to see hhow it goes...
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    Why in your first unformatted listing SalesLine2 is passed by VAR, and in the more recent post where the code is formatted VAR is gone?

    Which version is the 'live' version?

    Is it possible that you have copied the entire function from one place to another and the two listings are in fact two different versions of the same function taken from different objects?

    Missing VAR next to SalesLine2 may explain why nothing is passed back

    Slawek
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • RikarddoRikarddo Member Posts: 80
    edited 2017-10-17
    I think i may have unthicked SalesLine2 as Var by mistake, but the 'Live' version is the one with sales line 2 VAR. I have to check that
  • RikarddoRikarddo Member Posts: 80
    409499snyfvo.jpg

    This is how it goes when i Run the Code Unit and use the debugger.
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    At what point this screenshot was taken? My understanding is that SalesLine is your input parameter (as an input it should not use VAR IMHO but maybe you have some reasons for it), and the SalesLine2 is the output, so I'd expect to have it unitialised at the function entry point
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • RikarddoRikarddo Member Posts: 80
    Finnaly i managed to put it working...just needed to apply SalesLine2 Filters
    //roliveira 2017-10-04 begin  
    
    
    SalesLine.SETFILTER(Type,'<>0');
    SalesLine2.RESET;
    SalesLine2.SETCURRENTKEY("Document Type","Document No.","Attached to Line No.");
    SalesLine2.SETRANGE("Document Type", SalesLine."Document Type");
    SalesLine2.SETFILTER("Document No.", SalesLine."Document No.");
    SalesLine2.SETRANGE("Attached to Line No.", SalesLine."Line No.");
    IF SalesLine2.FINDSET THEN;
    //roliveira
    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);
       // pteixeira
        {
       IF NOT SalesLine.FINDSET THEN
       SalesLine.RESET;
       SalesLine.SETRANGE("Document Type",SalesLine."Document Type");
       SalesLine.SETRANGE("Document No.",SalesLine."Document No.");
       }
        
      //pteixeira END
    //roliveira 2017-10-04 begin  
    VVasilhame:=0; 
       IF SalesLine.FINDSET THEN
       REPEAT
         SalesLine2.RESET;
         SalesLine2.SETRANGE("Document Type", SalesLine."Document Type");
         SalesLine2.SETRANGE("Document No.",  SalesLine."Document No.");
         SalesLine2.SETRANGE("Attached to Line No.", SalesLine."Line No.");
         QProduto:=0;
         QVasilhame:=0;
         IF SalesLine2.FINDFIRST THEN
         IF (SalesLine2."Attached to Line No." <>0) THEN
         BEGIN
             QProduto+=SalesLine.Quantity;
             QVasilhame+=SalesLine2.Quantity;
             VVasilhame:=QProduto/QVasilhame;
             
         
         END;
        
    //roliveira end
          SalesLine."Custo Operação":=GetCustoOperacao(SalesLine."No.",SalesLine."Variant Code")/(SalesLine.Quantity/SalesLine2.Quantity);//sugestão : não dividir pela qtd mas sim pelo volume unitário  (Qtd/nºvas)
          
          {
          IF ltValueEntry.GET(SalesLine."Line No.") THEN
            SalesLine."Custo Vasilhame":=GetCustoVasilhame(ltValueEntry."Item No.",ltValueEntry."Valued Quantity", SalesLine."Tara a devolver")
          ELSE
            SalesLine."Custo Vasilhame":=0;
          }
          IF SalesLine."Codigo Vasilhame"<>'' THEN
          BEGIN
           SalesLine."Custo Vasilhame":=GetCustoVasilhame(SalesLine."Codigo Vasilhame",0,SalesLine."Vasilhame Devolver")/(SalesLine.Quantity/SalesLine2.Quantity);//(VVasilhame); //roliveira 2017-09-25 //dividir pelo volume do vasilhame
          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.");
    
Sign In or Register to comment.