CALCFIELDS does not work with CHANGECOMPANY

Hi Experts!!
NAV2018, shared customer table for all companies

In a report I need to put the client balance per company,for which I have a temporary table, but the value only calculates it to me correctly for the first company, for the rest it leaves the same value and does not recalculate it, this is my code:

pCompany.RESET;
IF pCompany.FINDFIRST THEN REPEAT
pCustomer.RESET;
pCustomer.CHANGECOMPANY(pEmpresa.Name);
IF pCustomer.FINDFIRST THEN REPEAT
pCustomer.CALCFIELDS("Balance (LCY)");
IF pCustomer."Balance (LCY)"<>0 THEN BEGIN
pSaldos.INIT;
pSaldos.Empresa:=pCompany."Display Name";
pSaldos.NIF:=pCustomer."VAT Registration No.";
pSaldos.Nombre:=pCustomer.Name;
pSaldos."Saldo Deudor":=pCustomer."Balance (LCY)";
pSaldos.INSERT(TRUE);
END;
UNTIL pCustomer.NEXT=0;
UNTIL pCompany.NEXT=0;

Any ideas? thanks in advance

Best Answer

Answers

  • kanika wrote: »
    Hi Experts!!
    NAV2018, shared customer table for all companies

    In a report I need to put the client balance per company,for which I have a temporary table, but the value only calculates it to me correctly for the first company, for the rest it leaves the same value and does not recalculate it, this is my code:

    pCompany.RESET;
    IF pCompany.FINDFIRST THEN REPEAT
    pCustomer.RESET;
    pCustomer.CHANGECOMPANY(pEmpresa.Name);
    IF pCustomer.FINDFIRST THEN REPEAT
    pCustomer.CALCFIELDS("Balance (LCY)");
    IF pCustomer."Balance (LCY)"<>0 THEN BEGIN
    pSaldos.INIT;
    pSaldos.Empresa:=pCompany."Display Name";
    pSaldos.NIF:=pCustomer."VAT Registration No.";
    pSaldos.Nombre:=pCustomer.Name;
    pSaldos."Saldo Deudor":=pCustomer."Balance (LCY)";
    pSaldos.INSERT(TRUE);
    END;
    UNTIL pCustomer.NEXT=0;
    UNTIL pCompany.NEXT=0;

    Any ideas? thanks in advance

    it should be pCompany.Name

  • i second vijay_g's answer.

    side note: "IF xyz.FINDFIRST THEN REPEAT" is bad practise. Use "IF xyz.FINDSET THEN REPEAT" for better performance.
    Reason: If you want to loop through several records, "findset" is made to get a few of them into the NSTs buffer for faster processing. With Findfirst, your NST doesn't expect you to need more than one record so it will only ask the SQL server for the "top 1" and i guess it will repeat that practise with every "next".
    If you don't belive me, please read the "remarks"-Section of those two functions in the help.
  • yes vijay_g you're right, I translated it wrong

    but in the code is well placed

  • StLi , I belive you!!
  • You cannot use a flowfield after a changecompany instruction. It will calculate everything with the data contained in your "CURRENTCOMPANY".

    Same behaviour with validates.
    Giving thanks is always wellcome
  • Thanks everyone for your help!

    lubost, that's what I did and it works perfectly
  • it happened to me a calcsums for ledger entries did not work, no clue why, it was retrieving records from other companies and at the end I had to do sum+=value sadly.