We describe a novel current accumulation algorithm for the three-dimensional self magnetic field calculation in a charged-particle beam optics code. The current source is a charged-particle beam represented by a collection of numerically-integrated current-carrying rays. We compute the magnetic vector potential using edge basis functions and the curl–curl formulation of the finite-element method. The current accumulation algorithm takes advantage of a novel particle tracker that happens to be ideal for this application. We show that our source vector is compatible with the singular finite-element matrix, even with modest numerical integration errors. Thus, a conjugate gradient matrix solver works well, with no need for additional gauge conditions. We confirmed this behavior in a series of numerical tests on a small problem with incomplete linear and quadratic basis functions on a variety of element shapes.