Error code 1806 on server

Product :4D - 4D Server
OS : Mac OS X
4D V17 R2

I’m getting error code 1806 when running my database as server. I do not get this code when running it under interpreted or compiled. Here is the code:

C_OBJECT(Torders;Tcust;Tsub)

Torders:=ds.orders.all()
Tsub:=ds.orders.query(“dealercode=:1”;“noonesbaby”)
For each (Torders;ds.orders.query(“customer#:1”;""))
Tcust:=ds.customers.query(“dealer_search_link=:1”;Torders.customer)
$count:=Tcust.count(“dealer_search_link”)
If ($count#1)
Tsub.add(Torders)
End if

End for each

Tcust:=ds.customers.query(“dealer_search_link=:1”;Torders.customer) seems to be the offending line.
Any thoughts on how to fix this? I’ve attached a screen shot of the error code.

assuming this is not a typo:

if you assign an entity selection to your variable Torders

you can’t then use the same variable to represent an entity in your For each block.

true, both entity and entity selection are objects, but still, you should use distinct variables for such code.