We analyze the conditions of formation of the Oort cloud from icy planetesimals scattered by the accreting outer planets. The combined effect of planetary and external perturbations is considered to be the mechanism of transfer from the comet's birthplace in the planetary region to the Oort cloud reservoir. If the main external perturbers from the primitive galactic environment were similar to the current ones (namely, passing stars and the tidal force of the galactic disk), the resulting Oort cloud would have probably been too loosely bound to have withstood the disrupting effect of penetrating encounters with giant molecular clouds. An additional problem is that most of the objects formed in the outer planetary region are found to be finally ejected by Saturn or Jupiter, and not by Neptune or Uranus, thus making the whole process of transfer of bodies from the planetary region to the Oort cloud very inefficient. Jupiter and Saturn perturbations are so strong that most bodies scattered by these planets are very likely to overshoot the narrow energy range of the present Oort cloud to interstellar space.It is shown that the combined action of planetary and external forces would have produced a more tightly bound comet reservoir if the Solar System formed within a much denser galactic environment, perhaps a molecular cloud and/or an open cluster. This seems to be the way in which most stars form. Moreover, the time scales of formation of Uranus and Neptune could well have been very short (a few times 107years or even less), as their non-negligible contents of hydrogen and helium suggest, which would give stronger support to the idea that the massive scattering of planetesimals in the outer planetary region was produced while the Solar System was still within its natal environment. It is found that a much stronger external field, caused either by other members of an open cluster or by the tidal force of the molecular cloud itself, could have produced a much more strongly bound Oort cloud at distances of a few thousand AU. Furthermore, a widened energy range for the Oort cloud reservoir would have increased the probability of trapping bodies scattered by Jupiter and Saturn there, thus making the transfer process much more efficient. The strong external perturbations that drove comets to a much more tightly bound Oort cloud ceased to act shortly afterward, as the molecular cloud (or the open cluster) dissipated, thus preventing the formed comet cloud from being disrupted. Such a tightly bound comet cloud could have been the source from which the external Oort cloud has been replenished through the age of the Solar System. An interesting by-product of our scenario of a much denser galactic environment is that not only bodies from the accretion zones of Uranus and Neptune could find their way to the Oort cloud, but also a significant number of residual planetesimals from the Jupiter and Saturn accretion zones could have been incorporated into the Oort reservoir. The physical-chemical nature of new comets may present different signatures according to their different birthplaces, thus constituting relevant pieces of information to learn about the galactic environment in which the Solar System formed.