Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

Function - Record won't initialize

Posted on by 265

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?

*This post is locked for comments

  • Rikarddo Profile Picture
    Rikarddo 265 on at
    RE: Function - Record won't initialize

    Finnaly i managed to put it working...just needed to apply SalesLine2 Filters has Suresh Kulla sugested.

    //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.");


  • Rikarddo Profile Picture
    Rikarddo 265 on at
    RE: Function - Record won't initialize

    Basicly I want to divide SalesLine.Quantity/SalesLine2.Quantity to get the volume of container

    Where SalesLine is the product and SalesLine2 is the container

  • Rikarddo Profile Picture
    Rikarddo 265 on at
    RE: Function - Record won't initialize
    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');
    
    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);// replace by QVasilhame or SalesLine2.Quantity
         
    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.");

    This is the full function. Do i have to apply the SalesLine2 Filters also at the end?

    the goal is to loop through it again for the second time, so i can dvide the quantity of item container false, by the one is true

  • Verified answer
    Suresh Kulla Profile Picture
    Suresh Kulla 43,745 on at
    RE: Function - Record won't initialize

    In your second SalesLine.FINDSET which saleslines are you planning to retrieve, since you already looped through the sales lines above to insert into ItValueEntry Table it carries the same filters again and then applies your other filter of Item Container on top of those.  I have not seen second UNTIL Statement for the REPEAT are you planning to get one salesline or want to loop through it again for the second time.

    Best to clear all your sales line filters using SalesLine.RESET after the first loop and apply your filters again then loop through it.

  • Zaid Tariq Profile Picture
    Zaid Tariq 2,266 on at
    RE: Function - Record won't initialize

    Can you let me know what exactly you want to achieve? Like do you want exact copy of SalesLine 2 asof SalesLine?

  • Rikarddo Profile Picture
    Rikarddo 265 on at
    RE: Function - Record won't initialize

    0407.Image1.jpg

    This is how it goes when i run the Code Unit

  • Rikarddo Profile Picture
    Rikarddo 265 on at
    RE: Function - Record won't initialize

    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.

    I will take your sugestion and figure how it goes.

  • Zaid Tariq Profile Picture
    Zaid Tariq 2,266 on at
    RE: Function - Record won't initialize

    Hi,

    I don't know whether your logic is correct or not but i did some chnages. Try this

    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;

    CLEAR(SalesLine); //Seems like something is wrong here because all the filters of the sales line will be removed.

    SalesLine.SETRANGE("Item Container",FALSE); //Seems like something is wrong here

    VVasilhame:=0;

    IF SalesLine.FINDSET THEN

    REPEAT

    CLEAR(SalesLine2);

    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.");

    QProduto:=0; // Please Check that eight you want to initalize this 0 here or above

    QVasilhame:=0; // Please Check that eight you want to initalize this 0 here or above

    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);
    SalesLine.Modify; 

  • Rikarddo Profile Picture
    Rikarddo 265 on at
    RE: Function - Record won't initialize

    It does not find a salesline2.

  • Rikarddo Profile Picture
    Rikarddo 265 on at
    RE: Function - Record won't initialize

    Didn't work.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans