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
But assuming that your tables/code otherwise is ok, then try to change
PRAmount := RecRequisition.Amount;
to
PRAmount += RecRequisition.Amount;
Hi Erik
Thanks you, But it's not work.
Below is the structure of 2 tables. Please help me
Thanks so much
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
As part of which key? Has to be the same key as you filter on.
Then we are back to:.
Erik P. Ernst said: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.
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);
What do you mean by repeat?
When you set a filter on Requisition No. in the Requisition table, then calling CALCSUMS will calculate the total for all records in the Requisition table. That's the beauty in CALCSUMS. It's using the sum index field, so you don't need a REPEAT-UNTIL to get the sum of Amount's. The line "IF Requisition.ISEMPTY THEN EXIT(0);" doesn't really have to be there for the function to work, leaving it out would give you the same. It's just there for a tiny bit of performance. ISEMPTY is "cheap" whereas CALCSUMS not so much.
Right now, if the above code doesn't work, then it is either because your definition of the sum index field does not match the key used. Or your data are not as you think they are. As I have pointed out earlier in this thread, then if the table data examples where your full data set, then it would only return the value for one record.
Also please understand the difference between := and +=
VarNo := 1; Means set the variable to 1. The existing value is overwritten.
VarNo += 1; Means add 1 to the existing value of the variable. You use it when you want to add to the value.
So in your case, if something had previously been added to the PRAmount variable, then your message would not show the amount for the requisition no.
Thanks Erik. It's work after I change call procedure.
Happy to hear that Dinh, your last reply was not that clear about it. And just in time, I was almost about to give up!
I hope you learned something from it.