Magnesium works by antagonizing calcium, which causes muscles to contract. But calcium associated cramping is not always due to a deficiency of balancing magnesium. High serum calcium, which can occur from problems such as excess vitamin D, hyperparathyroidism, etc. as well. Therefore, it would be wise to get a full blood workup just to make sure there is not a contributing factor.
If supplementing with magnesium I recommend using an acidified form of magnesium such as magnesium malate, or magnesium citrate (my second choice). The acidified forms of magnesium are absorbed much better than other forms and provide an additional benefit by further increasing cellular ATP levels.