-3

I have been trying to do a bulk load of accounts in salesforce and it keeps triggering the following error

UpdateCSMTeam:execution of AfterInsertcaused by: System.DmlException: Insert failed. First exception on row 0 with id00rJ000003iFSruIAG; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]Class.UpdateCSM.updateTeam Mem

Below I'm including the class and the trigger referenced.

APEX CLASS (Update CSM)

public class UpdateCSM {       
    public static void updateTeamMember(List<Account> accounts) {        
        SObject[] sobs = new SObject[] {};        
        for (Account a : accounts) {          
            if (a.Customer_Success_Manager__c != null) {            
                sobs.add(new AccountShare(                    
                AccountId = a.Id,                   
                OpportunityAccessLevel = 'Edit',                    
                CaseAccessLevel = 'Edit',                    
                AccountAccessLevel = 'Edit',                    
                UserOrGroupId = a.Customer_Success_Manager__c));        

            }       
            for (Account acc : accounts) {             
                if (acc.Customer_Success_Manager__c != null) {                
                    sobs.add(new AccountTeamMember(                        
                    AccountId = acc.id,                        
                    UserId = acc.Customer_Success_Manager__c,                        
                    TeamMemberRole = 'Customer Success Manager'));           
                }                        
             }                     
             insert sobs;                                                                        
        }             
    }
}

APEX TRIGGER (UpdateCSMTeam)

trigger UpdateCSMTeam on Account (After update, After insert){    
UpdateCSM.updateTeamMember(trigger.new);}
6
  • Did Rewrite apex class to be out of the loop not solve your issue on this?
    – BarCotter
    Nov 11 '14 at 16:46
  • @BarCotter it did not get rid of the error. I thought the issue was just the way it was written but I still ran into the problem. I'm not sure if it is the class or the trigger that is causing this error and why. Nov 11 '14 at 16:51
  • 2
    The code you have in this question and the code that was posted in the other answer are not the same. You still have your insert inside a loop. You also have a nested for loop on accounts which is wrong. I suggest you review @KeithCs answer again in the other question.
    – BarCotter
    Nov 11 '14 at 16:52
  • @BarCotter I used his second answer and I added the if (a.Customer_Success_Manager__c != null) after the first FOR, because without it the system wasn't allowing a blank value in the Customer_Success_Manager__c field. I guess I'm confused about what it means to be in or outside the loop. I thought it was just making sure that the insert was after closing curly brackets. Am I wrong? Nov 11 '14 at 16:57
  • There's two curly brackets after the insert in KeithCs answer. You have 3 curly brackets after it. Your insert is in a different place.
    – BarCotter
    Nov 11 '14 at 16:59
0

A slightly modified version of KeithC's answer in Rewrite apex class to be out of the loop is shown below.

I have made sure you only create both AccountShare and AccountTeamMember objects when if (a.Customer_Success_Manager__c != null) is true.

The reason for having the two for loops is so that the sobs list will have the first half AccountShare objects and the second half AccountTeamMember objects. A list can only have a maximum of 10 chunks of different records. The pattern below will make sure that there is only ever 2 chunks.

public class UpdateCSM {   
    public static void updateTeamMember(List<Account> accounts) {
        SObject[] sobs = new SObject[] {};
        for (Account a : accounts) {  
             if (a.Customer_Success_Manager__c != null) { 
                   sobs.add(new AccountShare(
                        AccountId = a.Id,
                        OpportunityAccessLevel = 'Edit',
                        CaseAccessLevel = 'Edit',
                        AccountAccessLevel = 'Edit',
                        UserOrGroupId = a.Customer_Success_Manager__c
                        ));
             }
        }
        for (Account a : accounts) {  
            if (a.Customer_Success_Manager__c != null) {
                sobs.add(new AccountTeamMember(
                        AccountId = a.id,
                        UserId = a.Customer_Success_Manager__c,
                        TeamMemberRole = 'Customer Success Manager'
                        ));
            }                
        }              
        insert sobs;                                                                      
    }              
}
5
  • now it is giving me a chunking error. The exact error is UpdateCSMTeam:execution of AfterInsertcaused by: System.TypeException: Cannot have more than 10 chunks in a single operation. Please rearrange the data to reduce chunking.Class.UpdateCSM.updateTeamMember: line 21 column 1 Trigger. UpdateCSMTeam line 2 . I'm not going between more than ten objects though. Nov 11 '14 at 17:29
  • Make sure you are using the latest code from my answer.
    – BarCotter
    Nov 11 '14 at 17:37
  • that seemed to work great but when I tried to push in a list everything went in except 200 out of 1000. I thought I may have pushed to much but when I tried again it wouldn't work. Could it be because the User in the record that is being granted sharing permissions in the class already has the access to the information. I heard that could potentially cause the problem. Is there a way around that? Would it be something that says if users have permission don't grant sharing permissions or something? Am I completely off? Nov 11 '14 at 21:29
  • The error is System.DmlException: Insert failed First Exception row 8; first error INSUFFICIENT+ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id:{} Class.UpdateCSM.updateTeamMember: line 24, column 1 Trigger.UpdateCSMTeam: line 2, column 1 Nov 11 '14 at 21:31
  • That is a different issue to the one you originally posted. If you create a new Question with those details then it will be a lot easier for people to help.
    – BarCotter
    Nov 12 '14 at 11:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.