+ Start a Discussion
imishraimishra 

Delete Trigger(Urgent)

Hi,
I have a custom Object called Opp Team(replicate of standard Opportunity Team).
When i add a user in Opp Team, it adds the same user in the standard Opportunity Team.(through trigger)
I want to delete the user from standard Oppotunity Team when in delete it from Opp Team and also vice versa.

Can anyone please help me to write a trigger for this. Please provide me some sample triggers if possible.

Thanks,
Ipsita

Best Answer chosen by imishra
hitesh90hitesh90
Hello Ipsita,

see following sample trigger code on after delete for your requirement.

Apex Trigger:
trigger trgRemoveOpportunityTeam on Opportunity_Team_Custom__c (after Delete) {
    Map<Id,Id> mapOppUserId = new Map<Id,Id>();
    for(Opportunity_Team_Custom__c opp: trigger.old){
        mapOppUserId.Put(opp.Opportunity__c, opp.User__c);
    }
    List<OpportunityTeamMember> lstOppTeamDel = new List<OpportunityTeamMember>();
    List<OpportunityTeamMember> lstOppTeam = [select id, UserId, OpportunityId from OpportunityTeamMember where OpportunityId IN: mapOppUserId.keyset()];
    for(OpportunityTeamMember oppMem: lstOppTeam){
        if(oppMem.UserId == mapOppUserId.get(oppMem.OpportunityId)){
            lstOppTeamDel.add(oppMem);
        }
    }
    if(lstOppTeamDel.size() > 0){
        delete lstOppTeamDel;
    }
}

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
Email :- hiteshpatel.aspl@gmail.com
My Blog:- http://mrjavascript.blogspot.in/

All Answers

hitesh90hitesh90
Hello Ipsita,

see following sample trigger code on after delete for your requirement.

Apex Trigger:
trigger trgRemoveOpportunityTeam on Opportunity_Team_Custom__c (after Delete) {
    Map<Id,Id> mapOppUserId = new Map<Id,Id>();
    for(Opportunity_Team_Custom__c opp: trigger.old){
        mapOppUserId.Put(opp.Opportunity__c, opp.User__c);
    }
    List<OpportunityTeamMember> lstOppTeamDel = new List<OpportunityTeamMember>();
    List<OpportunityTeamMember> lstOppTeam = [select id, UserId, OpportunityId from OpportunityTeamMember where OpportunityId IN: mapOppUserId.keyset()];
    for(OpportunityTeamMember oppMem: lstOppTeam){
        if(oppMem.UserId == mapOppUserId.get(oppMem.OpportunityId)){
            lstOppTeamDel.add(oppMem);
        }
    }
    if(lstOppTeamDel.size() > 0){
        delete lstOppTeamDel;
    }
}

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
Email :- hiteshpatel.aspl@gmail.com
My Blog:- http://mrjavascript.blogspot.in/
This was selected as the best answer
imishraimishra
Thanks Hitesh.
It worked.