I have an apex class that I have been having trouble with when I try to upload in bulk accounts. It is tied to triggers that do certain things depending on whether the Customer_Success_Manager__c field is filled in. I've edited this class several times to get as much out of the loop as I can get but I am still getting this error. I am new to development and I need help to get this to a point where it the code can handle bulk requests. I also would like to know how do you know when too much is in the loop. If you answer with code it would be great if you could leave an explanation of what the code is saying so that I can apply that logic in the future. The apex class is 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;
ashareLIST.add(caSharingRule);
if(a.Customer_Success_Manager__c!= null){
AccountTeamMember Teammemberad=new AccountTeamMember();
Teammemberad.AccountId=a.id;
Teammemberad.UserId=a.Customer_Success_Manager__c;
Teammemberad.TeamMemberRole = 'Customer Success Manager';
ATM.add(Teammemberad);
if(!ATM.isEmpty()){
insert ATM;
insert ashareLIST;
}
}
}
}
}
See APEX Dataloader fail INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call for the trigger this is called from