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);}
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.