Hello,
I had previously posted a question to see if it was possible to display the last date a specific order was ordered in a data display. I received some code that although it works, pulls in ALL the dates the specific code was ordered. Is there any one who could help me fix the code or have code to only bring in the last date this code was ordered???
This is the function I received and was using:
{ fn RetrieveOrder()
{
local arrayOrder
local i=0
local strSel="CPT-G0444"
local strOrder=""
local strRet=""
strOrder=Orders_All("delimited")
if strOrder"" then
arrayOrder=GetField(strOrder,"|","")
for i=1,i<=size(arrayOrder),i=i+1 do
arrayOrder[i]=GetField(arrayOrder[i],"^","")
if arrayOrder[i][3]==strSel then
strRet=strRet+(if strRet=="","",",")+arrayOrder[i][3]+" -- "+arrayOrder[i][4]
endif
endfor
endif
return strRet
}
}
Thank you very much for all your help in advanced! 🙂
-Alex
{ fn RetrieveOrder()
{
local arrayOrder
local i=0
local strSel="CPT-G0444″
local strOrder=""
local strRet=""
local Result=""
strOrder=Orders_All("delimited")
arrayOrder=GetField(strOrder,"|","")
for i=1,i<=size(arrayOrder),i=i+1 do
arrayOrder[i]=GetField(arrayOrder[i],"^","")
if arrayOrder[i][3]==strSel then
Result=arrayOrder[i][3]+" — "+arrayOrder[i][4]
endif
endfor
if Result <> "" then
strRet=Result
endif
return strRet
}
}
You will need to capture all occurrences of the order code you are looking for inside your loop and then pick the occurrence that has the most recent date. Try something like this inside of your loop (you will need to define a local variable to store the date):
local last_order_date = "" //initialize local variable to store order date
for i=1,i<=size(arrayOrder),i=i+1 do
arrayOrder[i]=GetField(arrayOrder[i],"^","")
if arrayOrder[i][3]==strSel then
if last_order_date == "" //first order encountered
last_order_date = arrayOrder[i][4]
else
if (DURATIONDAYS(arrayOrder[i][4],last_order_date) < 0)
last_order_date = arrayOrder[i][4]
else
//do nothing, last_order_date is more recent than this order date
endif
endif
endif
endfor
strRet= strSel +" — "+ last_order_date
return strRet
Just want to clarify. The above code is just the "for loop" that you need to substitute in your current code. The return string should occur outside the loop since all you want returned is the last (most recent) order date. I saw Cecil's response and believe that it will work as well, however if you want to be absolutely sure that you are getting the most recent order date, you should probably compare the order dates.
Thank you very much guys for all your help I got it working perfectly! 🙂
-Alex