+ Start a Discussion

Deleting Related Record using Apex Controller

Hi Everyone,

This may be my most pathetic post yet, but I can not seem to figure this out. I have used VS pages and Apex Controllers to create new records and update existing--- HOWEVER, deleting just has me stumped! 

What i would like to do is... 

Create a button called "Delete myself from Account Team" and put it on the Account Object. Upon clicking the button, i would like the system to loop thru all Account Team Members and delete the one that matches the current users's User Id. Does anyone have the code for this? 
Best Answer chosen by LaurenP6777
Ravi Dutt SharmaRavi Dutt Sharma
Hey Lauren,

Below code should work for you :
delete [SELECT Id FROM AccountTeamMember WHERE UserId =:Userinfo.getUserId()]

Put this in a method and call that method on your button click.