Imagine in a hypothetical situation that a commercial airliner was left on autopilot at some cruising altitude. Next imagine that all pilots left the flight deck and the door slammed shut behind them (presumably locked). Would they all be completely trapped outside the flight deck or would there be a way to enter should there be a situation like this?
The flight deck door lock system is normally turned on while on the ground and remains on for the entire flight. Once the system is turned on, the door DOES lock automatically when closed and can only be opened by the pilots in the flight deck.
If both pilots left the flight deck, the door would lock automatically behind them unless the door lock system was first turned off. There are procedures in place which can allow the door to be unlocked from the cabin in case no one in the flight deck can open it due to incapacitation.
The pilots can also over-ride this unlocking procedure in case they suspect criminal interference or un-authorized entry attempt. This over-ride feature prevented the Captain of Germanwings 9525 from re-entering the flight deck as the suicidal First Officer deliberately crashed the aircraft killing all on board.