Like you switch in with magic guard Alakazam when there are Rocks/Spikes on will Alakazam take damage?
Prevented! Wikisaur
no because the ability means that you only take damage from attacks. this is the same with burn poison etcetera but Im sure you already know this
Bulbapedia link