This type of foam has no business inside of a box. Seriously. It's bad news.
You can use it on the outside and around the back, but keep it from getting in the box. If you have an electrical fire in one of the boxes that contains this foam, your insurance company has the option of not honoring your coverage.
Even if the fire starts elsewhere, and in the subsequent investigation either the fire dept inspector or the insurance company's adjuster/inspector discovers foam in the boxes, they can reduce your claim based upon poor practices in the home.