If Statement acting strange

nvermanverma Member Posts: 396
I am dealing with a strange problem.

Refer to the picture for better understanding-
'Remaining Balance Formula' field has two options ( Annual Allocation + Opening Balance - Receipts) & (Opening Balance + Cheques - Receipts). If the user selects - Annual Allocation + Opening Balance - Receipts. Then the system will Calculate the 'Remaining Balance' field by taking the Annual Allocation from the Header and adding the Opening Balance (Balance Forward) and subtracting all the Receipts. The system should throw an error if the Remaing Balance field is less than 0. To do that I wrote the following code in the table
IF (lrecJob."Remaining Balance Formula" = lrecJob."Remaining Balance Formula"::"Annual Allocation + Opening Balance – Receipts")
THEN BEGIN

  lrecJob.GET(Rec."Job No.");
  lrecConsumerDisbursement.SETCURRENTKEY("Transaction Type");
  lrecConsumerDisbursement.SETRANGE("Job No.", Rec."Job No."); 
  lrecConsumerDisbursement.SETFILTER("Transaction Type", '%1|%2', "Transaction Type"::Receipt,"Transaction Type"::" " );
  lrecConsumerDisbursement.SETFILTER("Line No.", '<>%1', Rec."Line No.");
  lrecConsumerDisbursement.CALCSUMS(Amount);
  TotalRemaining := lrecJob."Annual Allocation" + lrecConsumerDisbursement.Amount + Rec.Amount;
  IF (TotalRemaining < 0) THEN  ********************************
  BEGIN
    ERROR('Remaining Balance cannot be less than 0');
  END;
END

This part works perfectly!

My issue is, even if the user selects the other option for 'Remaining Balance Formula' (Opening Balance + Cheques - Receipts), the system still throws that error message (Remaining Balance cannot be less than 0). Which makes no sense. I tried debugging the code, and even though the remaining balance formula is (Opening Balance + Cheques - Receipt), system still enters the above code.

Any idea what I am doing wrong?

Answers

  • Purvesh_MaisuriaPurvesh_Maisuria Member Posts: 71
    Hello nverma,

    I think you forgot to write GET statement / Setrange filters before your code.
    EX.

    IF GET(Primarykey Field1,Primarykey Field2...Primarykey FieldN) THEN
    BEGIN
    UR Code.
    END
    ELSE
    BEGIN
    ELSE Part.
    END;

    Ur code is OK, except this statement.

    lrecJob.GET(Rec."Job No.");

    Write this before your If condition.


    Hope this will solve your problem.

    Thanks & Regards,
    Purvesh Maisuria.
  • nvermanverma Member Posts: 396
    Thanks Purvesh

    OMG!!! It worked!

    I spend few hours trying to figure out why it was not working. I guess what you suggested made sense since I have to get that record before I can do the if statement. It doesnt make sense to do the if statement before fetching the record.
Sign In or Register to comment.