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
JN22JN22 

Error Trying to Upload with Dataloader

Hello,

 

I have 2 triggers on the Account Object.  One is a before trigger that populates a field on the Account called Sales_Owner__c with the Account Owner ID.  The other is an After trigger that looks at 3 fields on the Account and then either populates or updates values in the Account Team section.  The triggers work as expected in both my sandbox and production environments for single updates, but when I try to dataload in multiple revisions I get the following error:

 

AccountTeamChanges: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0 with id 01M7000000e5rGpEAI; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] Trigger.AccountTeamChanges: line 195, column 1

 

Has anyone else experienced this or know whay I am getting this?  Thanks for any assistance!  My triggers are below:

 

 

 

trigger ownerCopy on Account (before Insert, before Update) {

    // Update Sales Owner field with Account Owner
    for(Account x : Trigger.New){

            x.Sales_Owner__c = x.OwnerId;
}
}

 

=====================================================

 

trigger AccountTeamChanges on Account(after insert, after update)
{
    //list to hold new account team members
     List<AccountTeamMember> acctMembers = new List<AccountTeamMember>();
     
     //list to hold new account sharing rules
     List<AccountShare> acctSharingRules = new List<AccountShare>();
     
     //misc
     Set<String> rmMemberAccts = new Set<String>();
     Map<ID, ID> AccountTeamMap = new Map<ID, ID>();
    
     //iterate through records to find update processor values
     for(Account a : Trigger.new)
     {

        //new Account - Client Advisor
        if(Trigger.isInsert && a.Client_Advisor__c != null)
        {
            AccountTeamMember ca = new AccountTeamMember();
            ca.AccountId = a.Id;
            ca.TeamMemberRole = 'Client Advisor';
            ca.UserId = a.Client_Advisor__c;
            acctMembers.add(ca);
            
            AccountShare caSharingRule = new AccountShare();
            caSharingRule.AccountId = a.Id;
            caSharingRule.OpportunityAccessLevel = 'Read';
            caSharingRule.CaseAccessLevel = 'Read';
            caSharingRule.AccountAccessLevel = 'Edit';
            caSharingRule.UserOrGroupId = a.Client_Advisor__c;
            acctSharingRules.add(caSharingRule);
        }

        //new Account - Market Director
        if(Trigger.isInsert && a.Market_Director__c != null)
        {
            AccountTeamMember md = new AccountTeamMember();
            md.AccountId = a.Id;
            md.TeamMemberRole = 'Market Director';
            md.UserId = a.Market_Director__c;
            acctMembers.add(md);
            
            AccountShare mdSharingRule = new AccountShare();
            mdSharingRule.AccountId = a.Id;
            mdSharingRule.OpportunityAccessLevel = 'Read';
            mdSharingRule.CaseAccessLevel = 'Read';
            mdSharingRule.AccountAccessLevel = 'Edit';
            mdSharingRule.UserOrGroupId = a.Market_Director__c;
            acctSharingRules.add(mdSharingRule);
        }

        //new Account - Industry Manager
        if(Trigger.isInsert && a.Industry_Manager__c != null)
        {
            AccountTeamMember im = new AccountTeamMember();
            im.AccountId = a.Id;
            im.TeamMemberRole = 'Industry Manager';
            im.UserId = a.Industry_Manager__c;
            acctMembers.add(im);
            
            AccountShare imSharingRule = new AccountShare();
            imSharingRule.AccountId = a.Id;
            imSharingRule.OpportunityAccessLevel = 'Read';
            imSharingRule.CaseAccessLevel = 'Read';
            imSharingRule.AccountAccessLevel = 'Edit';
            imSharingRule.UserOrGroupId = a.Industry_Manager__c;
            acctSharingRules.add(imSharingRule);
        }

        //updated Account
        else if(Trigger.isUpdate)
        {
            //old Accoount record
            Account oldAcct = Trigger.oldMap.get(a.Id);
            
            //check to see if the team values have changed and verifies the
            //new values are not null
            if(oldAcct.Client_Advisor__c != a.Client_Advisor__c && a.Client_Advisor__c != null)
            {
                //add old Client Advisor to remove list if one exists
                if(oldAcct.Client_Advisor__c != null)
                {
                    rmMemberAccts.add(oldAcct.Id);
                    AccountTeamMap.put(oldAcct.Id, oldAcct.Client_Advisor__c);
                }
                
                //add new processor to account team and update sharing rules
                AccountTeamMember ca = new AccountTeamMember();
                ca.AccountId = a.Id;
                ca.TeamMemberRole = 'Client Advisor';
                ca.UserId = a.Client_Advisor__c;
                acctMembers.add(ca);
                
                AccountShare caSharingRule = new AccountShare();
                caSharingRule.AccountId = a.Id;
                caSharingRule.OpportunityAccessLevel = 'Read';
                caSharingRule.CaseAccessLevel = 'Read';
                caSharingRule.AccountAccessLevel = 'Edit';
                caSharingRule.UserOrGroupId = a.Client_Advisor__c;
                acctSharingRules.add(caSharingRule);
            }
            else if(oldAcct.Client_Advisor__c != a.Client_Advisor__c && a.Client_Advisor__c == null)
            {
                rmMemberAccts.add(a.Id);
                AccountTeamMap.put(oldAcct.Id, oldAcct.Client_Advisor__c);
            }

            
            //check to see if the team values have changed and verifies the
            //new values are not null
            if(oldAcct.Market_Director__c != a.Market_Director__c && a.Market_Director__c != null)
            {
                //add old Client Advisor to remove list if one exists
                if(oldAcct.Market_Director__c != null)
                {
                    rmMemberAccts.add(oldAcct.Id);
                    AccountTeamMap.put(oldAcct.Id, oldAcct.Market_Director__c);
                }
                
                //add new processor to account team and update sharing rules
                AccountTeamMember md = new AccountTeamMember();
                md.AccountId = a.Id;
                md.TeamMemberRole = 'Market Director';
                md.UserId = a.Market_Director__c;
                acctMembers.add(md);
                
                AccountShare mdSharingRule = new AccountShare();
                mdSharingRule.AccountId = a.Id;
                mdSharingRule.OpportunityAccessLevel = 'Read';
                mdSharingRule.CaseAccessLevel = 'Read';
                mdSharingRule.AccountAccessLevel = 'Edit';
                mdSharingRule.UserOrGroupId = a.Market_Director__c;
                acctSharingRules.add(mdSharingRule);
            }
            else if(oldAcct.Market_Director__c != a.Market_Director__c && a.Market_Director__c == null)
            {
                rmMemberAccts.add(a.Id);
                AccountTeamMap.put(oldAcct.Id, oldAcct.Market_Director__c);
            }

            
            //check to see if the team values have changed and verifies the
            //new values are not null
            if(oldAcct.Industry_Manager__c != a.Industry_Manager__c && a.Industry_Manager__c != null)
            {
                //add old Client Advisor to remove list if one exists
                if(oldAcct.Industry_Manager__c != null)
                {
                    rmMemberAccts.add(oldAcct.Id);
                    AccountTeamMap.put(oldAcct.Id, oldAcct.Industry_Manager__c);
                }
                
                //add new processor to account team and update sharing rules
                AccountTeamMember im = new AccountTeamMember();
                im.AccountId = a.Id;
                im.TeamMemberRole = 'Industry Manager';
                im.UserId = a.Industry_Manager__c;
                acctMembers.add(im);
                
                AccountShare imSharingRule = new AccountShare();
                imSharingRule.AccountId = a.Id;
                imSharingRule.OpportunityAccessLevel = 'Read';
                imSharingRule.CaseAccessLevel = 'Read';
                imSharingRule.AccountAccessLevel = 'Edit';
                imSharingRule.UserOrGroupId = a.Industry_Manager__c;
                acctSharingRules.add(imSharingRule);
            }
            else if(oldAcct.Industry_Manager__c != a.Industry_Manager__c && a.Industry_Manager__c == null)
            {
                rmMemberAccts.add(a.Id);
                AccountTeamMap.put(oldAcct.Id, oldAcct.Industry_Manager__c);
            }

        }
        
        //DML OPERATIONS
        //remove team members from account team if any exist
        if(rmMemberAccts.size() > 0)
        {
            List<AccountTeamMember> removeTeam = new List<AccountTeamMember>();
            for(AccountTeamMember atm : [SELECT Id, UserId, AccountId
            FROM AccountTeamMember
            WHERE (TeamMemberRole='Client Advisor' OR TeamMemberRole='Market Director' OR TeamMemberRole='Industry Manager') AND AccountId IN :rmMemberAccts])
            {
                if(atm.UserId == AccountTeamMap.get(atm.AccountId))
                    removeTeam.add(atm);
            }
            
            delete removeTeam;
        }
        
        system.debug('-->ACCOUNT MEMBERS: ' + acctMembers);
        //insert the new account team members if any exist
        if(acctMembers.size() > 0)
            insert acctMembers; //LINE 100
        
        //insert account sharing rules if any exist
        if(acctSharingRules.size() > 0)
            insert acctSharingRules;
     }    
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

As the error states, you cannot set an id on an sObject which you are trying to insert.

What is on line 195 ?

 

If the problem is with the line you've marked as Line 100, try to replace the insert with an upsert.

 

   if(acctMembers.size() > 0)
            upsert acctMembers; //LINE 100

 

All Answers

Ritesh AswaneyRitesh Aswaney

As the error states, you cannot set an id on an sObject which you are trying to insert.

What is on line 195 ?

 

If the problem is with the line you've marked as Line 100, try to replace the insert with an upsert.

 

   if(acctMembers.size() > 0)
            upsert acctMembers; //LINE 100

 

This was selected as the best answer
JN22JN22

Thanks, that got me past the error and I was able to upload to my fields.  However, the trigger was was supposed to update the Account Team (AccountTeamChanges) did not fire.  Any idea why?  Thanks