Spray foam insulation comes in pressurized tanks and aerosol cans. Made from a polyurethane liquid, the foam squirts out as a thick wet liquid and rapidly expands.
To fill behind outlet boxes and gaps beside windows and doors, the aerosol cans are sufficient, and they come with an extension tube that fits on the cap for precision foam distribution.
If your exterior walls are constructed from standard 2-inch by 4-inch stud framing, you may use spray foam behind the wall outlets. In standard wall construction, there will be only a small gap behind the outlet box, less than 1/2-inch, so regular batt insulation will not fit.
In 6-inch wide exterior walls, regular batt insulation is a better choice since the gap behind the outlet box is approximately 2.5 inches. In these larger gaps, the foam can run down the wall or not expand evenly to fill the wider space between the back of the outlet box and the wall.
In wider walls, you can cut the top face off insulation batts around the outlet box, and tuck the back half of the batt behind the box.
Attach the extension tube to the nozzle of the spray foam can and shake the contents of the can as directed on the label. Position the end of the extension tube at the back of the cavity behind the wall outlet. Depress the nozzle to disperse the foam/liquid behind the outlet box while moving the end of the tube up and down, working your way toward the outer edge of the box. The spray foam may come out quickly, so be prepared to move the tube as soon as you start spraying. If you get too much spray behind the box, the pressure can push the box outward, creating a problem for the drywall installer.
There is no reason to use spray foam insulation on the sides of outlet boxes. The foam is not adhesive, and the open nature of the outlet sides doesn't provide the opposing barriers required to hold the foam in place.
Keep a utility knife handy. If you over-spray, you can cut away the excess with the knife.