Technically they can record it anytime they want, but if the entity has proper controls it shouldn't be recorded until the transaction takes place, meaning terms have been contractually agreed upon between the entity and vendor.
Usually, in larger companies and certain other industries like manufacturing or others with high volume transactions, an entity will first create a purchase order. This is usually done when the company has internally decided there is a need to purchase certain good(s).
Then that purchase order (prepared by a sales agent or purchasing agent) is sent for approval by a manager for example and then a copy of it is sent to the accounting department to record the transaction and prepare the bill (accounts payable) in the system.
Depending on the size and nature of the entity, a receiving report from the warehouse and an invoice from the vendor could first be required from the vendor, another step in the control process in order to get recorded, which still comes after the purchase order but before getting entered as accounts payable.
It might sound complicated but it's just wordy and in the real world nowadays with a computerized environment, all of this can happen virtually simultaneously.