Sleep is believed to play an important role in memory consolidation. We induced sleep on demand by expressing the temperature-gated nonspecific cation channel Transient receptor potential cation channel (UAS-TrpA1) in neurons, including those with projections to the dorsal fan-shaped body (FB). When the temperature was raised to 31°C, flies entered a quiescent state that meets the criteria for identifying sleep. When sleep was induced for 4 hours after a massed-training protocol for courtship conditioning that is not capable of inducing long-term memory (LTM) by itself, flies develop an LTM. Activating the dorsal FB in the absence of sleep did not result in the formation of LTM after massed training.