RE: Search values in other months if the current one does not have
The question sounded very familiar. It's like I answered once .
In the other question, account was involved, so I included the header in the answer.
If it's just the item, the process is easier. You can find the last record of this item with a simple select, instead of looking for the last record month by month. After you find the last record, what you need to do is to apply a one-month range with the date(month) information of that record and find the maximum price.
ItemId _itemId = "";
VendInvoiceTrans vendInvoiceTrans;
FromDate fromDate;
ToDate toDate;
;
select firstOnly vendInvoiceTrans
order by InvoiceDate Desc
where vendInvoiceTrans.ItemId == _itemId;
fromDate = mkDate(1,mthofyr(vendInvoiceTrans.InvoiceDate),year(vendInvoiceTrans.InvoiceDate));
toDate = endMth(vendInvoiceTrans.InvoiceDate);
select maxof(PurchPrice) from vendInvoiceTrans
where vendInvoiceTrans.InvoiceDate >= fromDate
&& vendInvoiceTrans.InvoiceDate <= toDate
&& vendInvoiceTrans.ItemId == _itemId;
info(strFmt("%1",vendInvoiceTrans.PurchPrice));