Calculation more Request Order

Hi Everybody.

On Purchase Line of my include more Request Order. I want calculation Amount on all Request. But wrong.

This procedure only calculates 1 line. It can not repeat for the remaining lines by conditional :

RecRequisition.SETRANGE(“Requisition No”,RecPurchLine.“Requisition No”);


RecRequisition.RESET;
RecRequisition.SETRANGE("Requisition No",RecPurchLine."Requisition No");
IF RecRequisition.FIND('+') THEN BEGIN
  REPEAT
    RecRequisition.SETRANGE("Requisition No",RecPurchLine."Requisition No");
    RecRequisition.CALCSUMS(Amount);
    PRAmount:=RecRequisition.Amount;
  UNTIL RecRequisition.NEXT=0   ;
END;
MESSAGE(Txt0001,PRAmount);

Please help me fix code. Thanks so much

Best Regards,

Dinh Son

Hi Dinh,

What table is RecRequisition? And exactly what is it, that you’re trying to calculate?

The second “RecRequisition.SETRANGE(“Requisition No”,RecPurchLine.“Requisition No”);” line certainly does not make anything for your code. It’s just repeating a line.

But assuming that your tables/code otherwise is ok, then try to change

PRAmount := RecRequisition.Amount;

to

PRAmount += RecRequisition.Amount;

Thanks you Erik

Hi Erik

Thanks you, But it’s not work.

Below is the structure of 2 tables. Please help me

Thanks so much

Purchase Line
No Location Code Quantity Requisition No
P00001 BLUE 10 PR-00001
P00002 WHITE 20 PR-00002
Requisition
Requisition No Amount
PR-00001 2000
PR-00002 1000

If you only have one purchase line per requisition no. then maybe that is why it is only returning one line…

Hi Erik.

Exactly it only returns one line. Please help me solution.

Thanks so much.

Yes, of course. You only have one purchase line per requisition no. and you filter on the requisition no. - so will only return one line.

You still have note written exactly what it is that you’re actually trying to do?

Hi Erik,

I want to calculate the Amount of purchase requests versus the PO before receiving the goods into the warehouse.

Then you don’t need all that code. This should be enough:

PROCEDURE GetRequisitionAmount(RequisitionNo: Integer): Decimal;
BEGIN
  IF NOT Requisition.GET(RequisitionNo) then EXIT(0); 
  EXIT(Requisition.Amount);
END;

You need to define the variable in the function also, but then you can just call it where you need it.

Thanks Erik

I write procedure : define RequisitionNo type : Code


GetRequisitionAmount(RequisitionNo : Code[20]) : Decimal
BEGIN
  IF NOT RecRequisition.GET(RecPurchLine.RequisitionNo) THEN EXIT(0);
  EXIT(RecRequisition.Amount);
END;

then i call it by :

PRAmount:=GetRequisitionAmount(RecPurchLine.RequisitionNo);

MESSAGE(Txt000002,PRAmount);

But it’s not work.

What is the primary key of RecRequisition?

Primary key of RecRequisition: RequisitionNo,Item.

Yes, then the code will not work. That would have been something you should have mentioned in the first place. Also if other filters where set.

Is the Amount field defined as a SumIndexField? CALCSUMS only work with fields defined as SumIndexFields.

Yes.Amount field defined as a SumIndexField

pastedimage1526722266777v1.png

As part of which key? Has to be the same key as you filter on.

Then we are back to:.

If that’s your whole dataset, then no code can change that. But I assume it’s not, even though you didn’t tell us…

So far we know that you have a table Requisition with a primary field of Requisition No. and Item No. It also contains a field Amount, which is declared as a sum index field (assuming part of the primary key).

In that case this could/should work:

PROCEDURE GetRequisitionAmount(RequisitionNo: Code[20]): Decimal;
VAR
  Requisition: RECORD Requisition;
BEGIN
  Requisition.SETRANGE("Requisition No",RequisitionNo);
  IF Requisition.ISEMPTY THEN EXIT(0);
  Requisition.CALCSUMS(Amount);
  EXIT(Requisition.Amount);
END;

Thanks you Erik.

On Page Purchase Order I new 1 button Name: check Amount Then I call the procedure:

PRAmount:=GetRequisitionAmount(RecPurchLine.“RequisitionNo”);

But return value PRAmount = 0

PROCEDURE GetRequisitionAmount("RequisitionNo" : Code[20]) : Decimal
VAR
  RecRequisition: RECORD Requisition;
  RecPurchLine  : RECORD "Purchase Line";
BEGIN
//RecPurchLine.SETFILTER("Document Type",'%1',RecPurchLine."Document Type"::"Blanket Order");
RecRequisition.SETRANGE("RequisitionNo",RecPurchLine."RequisitionNo");
IF RecRequisition.ISEMPTY THEN EXIT(0);
RecRequisition.CALCSUMS(Amount);
EXIT(RecRequisition.Amount);
END;

Sure it doesn’t, as you didn’t follow my advise. You are using RecPurchLine.“RequisitionNo” as your filter (a locally declared variable). You should be using RequisitionNo as declared in your parameters.

If you run it with your debugger, then you will see. RecPurchLine should not be declared or used in the function.

Also why do you add “Rec” in front of your variable names? That’s not good practice. Best practice is to call your variable’s the same as your tables.

Thanks Erik.

I have edit code, but procedure run one only line. Not repeat


GetRequisitionAmount(RequisitionNo : Code[20]) : Decimal
BEGIN
  Requisition.SETRANGE("Requisition No",RequisitionNo);
  IF Requisition.ISEMPTY THEN EXIT(0);
  Requisition.CALCSUMS(Amount);
  EXIT(Requisition.Amount);
END;

Call Procedure :

  PRAmount+=GetRequisitionAmount(PurchLine."Requisition No");
  MESSAGE(Txt000002,PRAmount);