I have a trigger and a class that seem to be causing an insert fail in Dataloader. I am trying to insert 200 records to see if an error is generated before moving these to production. I'm just inserting the mandatory fields such as Name, Owner, Market, Customer_Success_Manager__c, and Business Unit.

Each time I get an error:

UpdateCSMTeam:execution of AfterInsertcaused by: System.DmlException: Insert failed. First exception on row 0 with id01MJ0000009gXthMAE; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]Class.UpdateCSM.updateTeamMem . I've listed the class and trigger below.


public class UpdateCSM {        
    public static void updateTeamMember(List<Account> accounts) {            
        List<AccountShare> ashareLIST = new List<AccountShare>();            
        List<AccountTeamMember> ATM = new List<AccountTeamMember>();           
        Set<String> rmMember = new Set<String>();            
        Map<ID, ID> acctToBRepMap = new Map<ID, ID>();            

        for(Account a:accounts){                

            AccountShare caSharingRule = new AccountShare();                   
            caSharingRule.AccountId = a.Id;                   
            caSharingRule.OpportunityAccessLevel = 'Edit';                   
            caSharingRule.CaseAccessLevel = 'Edit';                   
            caSharingRule.AccountAccessLevel = 'Edit';                   
            caSharingRule.UserOrGroupId = a.Customer_Success_Manager__c;                   

            if(a.Customer_Success_Manager__c!= null){                  
                AccountTeamMember Teammemberad=new AccountTeamMember();
                Teammemberad.TeamMemberRole = 'Customer Success Manager';                  

                    insert ATM;                         
                    insert ashareLIST;                                   

                        list<AccountTeamMember> ratm = [SELECT UserId                    
                                                        FROM AccountTeamMember                    
                                                        WHERE (TeamMemberRole = 'Customer Success Manager') 
                                                        AND AccountId =:a.id];                    
                            delete ratm;               


trigger UpdateCSMTeam on Account (After update, After insert){    

1 Answer 1


Its a bit hard to tell because of the lack of indenting, but your inserts look like they are inside the loop. So the first time round the loop the insert is done and an ID allocated to the first item in the list and then the second time round the loop the insert fails because the first item already has an ID (though the second doesn't).

The fix is to move the inserts beyond the end of the main loop and adjust your other logic accordingly.

You'll also need to remove the delete out of the loop too to avoid a governor limit exception.

See Apex Code Best Practices for techniques on how to get a trigger to work for the bulk case.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .