differential equations – Reconstruction of a function from its gradients

I have a list of gradient components, $ partial f / partial x_i $, of a function $ f (x_1, x_2, cdots) $. Is there a good way to rebuild the function $ f $?

One approach to doing this would be to treat this as an EDP system and to use DSolve. However, Mathematica is unable to resolve PDEs with more than 3 variables – see, for example, here.

Another approach is to integrate the gradients $ int ( partial f / partial x_i) dx_i $ then take the Union terms of all integrals. This is not a robust enough way to do things because it fails if expressions for integrals are not simple enough (ExpandAll do not help). A code for doing this is as follows:
Table(act(m)=ExpandAll(Integrate(gradient(m),Subscript(x, m)),{m,1,NN});
f=Fold(Union,act(1),Table(act(m),{m,2,NN}));

Better ideas?