Does every piece you’re trying to count have this ProdLoc marker? If so, I’m afraid I have no idea why that isn’t working. I think Brent is the expert on Beanshell Expressions, perhaps he can chime in?
I think I can explain why it works: $ProdLoc$ gets replaced by the contents of ProdLoc on the executing piece before the Count function is run, while ProdLoc doesn’t get replaced until the Count function is running, so it gets replaced by the contents of ProdLoc on the piece being counted, instead.