Stealth Rock does not "attack" the opponent, it lays a trap of stones around them so that any Pokemon get hurt when switching in.
Using a Custap Berry makes the holder go first in the same "priority bracket". So Stealth Rock would go before the opponent used a regular attack. However, switching out is +6 priority and will always happen before you use Stealth Rock.
The opponent will switch, then you lay the rocks. It will not injure the opponent then because they already switched in. But when they next switch out, it will hurt them.