Another option might be to use the ph_slider. When the ball comes close to the magnet, you attach the constraint. You can then adjust the motor 1 parameter to simulate attraction and rejection and release the constraint when it goes outta range. The manual says that other movement is blocked so this might not work for the kind of magnetic motion you want.