function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sinka Tinko 1Sinka Tinko 1 

What am I doing wrong here

public static void OnAfterInsert(List < Opportunity > newOpps) {
List<OpportunityTeamMember> opptList = new List<OpportunityTeamMember>();
for(Opportunity opp : newopps) {
    
    List<Account> acclist = [Select id, name, owner.division from account where id = :opp.account.id ];
    
    for (account a : acclist) {
        if (a.owner.division == 'Payback') {

            opportunityteammember opptm = new opportunityteammember(
            TeamMemberRole = 'Account Manager',
            OpportunityId = opp.id,
            UserId = opp.Ownerid);
            opptList.add(opptm);
            }    
            insert opptList;
        }
    }    
}  
Best Answer chosen by Sinka Tinko 1
BALAJI CHBALAJI CH
Hi Sinka Tinkom
When you are querying acclist using Opportunity's AccountID, you can use opp.accountid.

Please find below modified code.
public static void OnAfterInsert(List < Opportunity > newOpps) {
List<OpportunityTeamMember> opptList = new List<OpportunityTeamMember>();
for(Opportunity opp : newopps) {
    
    List<Account> acclist = [Select id, name, owner.division from account where id = :opp.accountid ];
    
    for (account a : acclist) {
        if (a.owner.division == 'Payback') {

            opportunityteammember opptm = new opportunityteammember(
            TeamMemberRole = 'Account Manager',
            OpportunityId = opp.id,
            UserId = opp.Ownerid);
            opptList.add(opptm);
            }    
            insert opptList;
        }
    }    
}

Let us know if that helps you.

Best Regards,
BALAJI

All Answers

Anil kumar GorantalaAnil kumar Gorantala
public class communityCodeCheck {
    public static void OnAfterInsert(List<Opportunity> newOpps) {
        List<OpportunityTeamMember> opptList = new List<OpportunityTeamMember>();
        for(Opportunity opp : newopps) {
            
            List<Account> acclist = [Select id, name, owner.division from account where id = :opp.account.id ];
            
            for (account a : acclist) {
                if (a.owner.division == 'Payback') {
                    
                    opportunityteammember opptm = new opportunityteammember(
                        TeamMemberRole = 'Account Manager',
                        OpportunityId = opp.id,
                        UserId = opp.Ownerid);
                    opptList.add(opptm);
                }      
            }
            insert opptList; 
        }    
    }  
}

You wrote dml inside for loop which is wrong way of coding, Make sure you write dml after for loop.
BALAJI CHBALAJI CH
Hi Sinka Tinkom
When you are querying acclist using Opportunity's AccountID, you can use opp.accountid.

Please find below modified code.
public static void OnAfterInsert(List < Opportunity > newOpps) {
List<OpportunityTeamMember> opptList = new List<OpportunityTeamMember>();
for(Opportunity opp : newopps) {
    
    List<Account> acclist = [Select id, name, owner.division from account where id = :opp.accountid ];
    
    for (account a : acclist) {
        if (a.owner.division == 'Payback') {

            opportunityteammember opptm = new opportunityteammember(
            TeamMemberRole = 'Account Manager',
            OpportunityId = opp.id,
            UserId = opp.Ownerid);
            opptList.add(opptm);
            }    
            insert opptList;
        }
    }    
}

Let us know if that helps you.

Best Regards,
BALAJI
This was selected as the best answer
Anil kumar GorantalaAnil kumar Gorantala
yes @sinka balaji is also right
Sinka Tinko 1Sinka Tinko 1
Hi Guys, 

Thanks so much for your help. It worked.