I'm not sure you'll be able to do this like you are planning. When you are first creating a record - you haven't actually saved it yet and have not generated the ID for the record. I could see when you would get an error because in your code - entityId should be null if you are launching prior to saving the record.
You won't be able to send a value from JavaScript to the Dialog - there isn't any supported mechanism to handle this.