  In the interstellar cold gas, the chemistry of formaldehyde (H$_2$CO) can be
essential to explain the formation of complex organic molecules. On this
matter, the massive and energetic protostellar object G331 is still unexplored
and, hence, we carried out a comprehensive study of the isotopologues of
H$_2$CO and formyl cation (HCO$^+$), and of protonated formaldehyde
(H$_2$COH$^+$) through the APEX observations in the spectral window
$\sim$159-356~GHz. We employed observational and theoretical methods to derive
the physical properties of the molecular gas combining LTE and non-LTE
analyses. Formaldehyde was characterized via 35 lines of H$_2$CO, H$_2^{13}$CO,
HDCO and H$_2$C$^{18}$O. The formyl cation was detected via 8 lines of HCO$^+$,
H$^{13}$CO$^+$, HC$^{18}$O$^+$ and HC$^{17}$O$^+$. Deuterium was clearly
detected via HDCO, whereas DCO$^+$ remained undetected. H$_2$COH$^+$ was
detected through 3 clean lines. According to the radiative analysis,
formaldehyde appears to be embedded in a bulk gas with a wide range of
temperatures ($T\sim$20-90 K), while HCO$^+$ and H$_2$COH$^+$ are primarily
associated with a colder gas ($T\lesssim$ 30 K). The reaction H$_2$CO+HCO$^+
\rightarrow$ H$_2$COH$^+$ + CO is crucial for the balance of the three species.
We used Nautilus gas-grain code to predict the evolution of their molecular
abundances relative to H$_2$ which values at time scales $\sim$10$^3$ yr
matched with the observations in G331: [H$_2$CO] = (0.2-2) $\times$10$^{-8}$,
[HCO$^+$] = (0.5-4) $\times$10$^{-9}$ and [H$_2$COH$^+$] = (0.2-2)
$\times$10$^{-10}$. Based on the molecular evolution of H$_2$CO, HCO$^+$ and
H$_2$COH$^+$, we hypothesized about the young lifetime of G331, which is
consistent with the active gas-grain chemistry of massive protostellar objects.
