I think I'd just promote the invoice type to the library. Add a data connection to the library - don't run the query on form open. Add rules to your submit rules - to get the invoice numbers just for your type, set the query field in the data connection to the selected type, then run the query. If there are no rows returned, you know this is the first one of its type - otherwise you can use your max(id) logic - since you'll only have rows for the selected type, you won't need any XPath filters. Remember, there is a slight chance if two people submit the same type at the same time that you could get a duplicate number - slim, but possible (and the work arounds for that can get kind of complicated, so it may be an OK risk for you to take, just wanted to make sure you were aware).
Hilary Stoupa
