I am not sure what the OP is asking.
Is he asking if the foam can be used after cutting a hole in the masonry and installing a flush mount box?
If so, I prefer to use concrete. It can be purchased in tubes and applied with a caulk gun. Grey 'fire stop' is also a good choice and is easy to install.
I sure hope the OP was not asking if it was OK to use for surface mount boxes.