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
jrosser1.39050288155405E12jrosser1.39050288155405E12 

DML Error upserting to AccountShare

I have a trigger and a test class to upsert account team and account team share when a new records is added/edited on a custom object.

The trigger works, the test class failes on the accountshare piece, if I comment that out it works fine for the account team.  But the user gets added with read only.

Without changing system wide sharing rules is there a way to bypass this with a trigger?

Thank you,
John WestenhaverJohn Westenhaver
I've done it many times, so the answer is yes, you can do this. Please post your code so we can figure out where the error is ...
jrosser1.39050288155405E12jrosser1.39050288155405E12
John, 

That would be amazing if you can figure this out.

---Trigger--
trigger AccountTeamADD on TSG__c (before update,before insert) {

//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>();


//Hold SalesRep
Set <String> ss = New Set<String>();
//Hold Customer ID
Set <String> aa = New Set<String>();
// Hold User Info
List<User> TheUser=new LIST<User>();
//Hold Account Info
List<Account> TheAcct=new LIST<Account>();

//Loop Through all records in the Trigger.new Collection

For(TSG__c T: Trigger.new)
{
    ss.add(T.email_address__c);
    aa.add(T.P21_CompanyCustomer_ID__c);
}


//Get User Info
TheUser = [Select ID,email from User where email IN:ss];
//Get Account Info
TheAcct = [Select ID,P21_CompanyCustomer_ID__c from Account where P21_CompanyCustomer_ID__c IN:aa];

    System.debug('@@@@--'+TheUser.size());
   
//Check to Make sure a record was retuned  
if (!TheUser.isEmpty()) {

     //Loop Through the Trigger (New Records)

     For (TSG__c T: Trigger.new) {
    
             For (Account acct: TheAcct) {
            
             For (User email: TheUser) {
        
             if (t.email_address__c == email.email && T.P21_CompanyCustomer_ID__c ==acct.P21_CompanyCustomer_ID__c) { //Make sure we match on email_address__c
            
                 //Insert into Account Team Here;
                
                    AccountTeamMember ca = new AccountTeamMember();
                    ca.AccountId = acct.Id;
                    ca.TeamMemberRole = 'Technical Sales';
                    ca.UserId = email.Id;
                    acctMembers.add(ca);
                   
                //Insert into Account Share Here;
               
                AccountShare caSharingRule = new AccountShare();
                    caSharingRule.AccountId = acct.Id;
                    caSharingRule.OpportunityAccessLevel = 'Edit';
                    caSharingRule.CaseAccessLevel = 'Edit';
                    caSharingRule.AccountAccessLevel = 'Edit';
                    caSharingRule.UserOrGroupId = email.Id;
                    acctSharingRules.add(caSharingRule);
                

                
                             }

                        }

                    }

                }

  
                     upsert acctMembers;
                    upsert acctSharingRules;    //This is what is causing the error.         
    
                
             
            }
        }


--Class--
/**
This is a Class for the auto adding account team based on TSG Group
*/

@isTest

Private class add_team {

Private static testmethod void myTeamTest(){

List<AccountTeamMember> acctMembers = new List<AccountTeamMember>();

List<AccountShare> acctSharingRules = new List<AccountShare>();


//Hold SalesRep
Set <String> ss = New Set<String>();
//Hold Customer ID
Set <String> aa = New Set<String>();
List<User> TheUser=new LIST<User>();
//Hold Account Info
List<Account> TheAcct=new LIST<Account>();
List<TSG__c> TheTSG = new List<TSG__c>();

  
  
       // Create the Acounts
    //Links to TSG P21 Customer ID
        List<Account> accList = new List<Account>();
        for (Integer i = 0; i < 1; i++) {
            Account newAcc = new Account(Name='test' + i,P21_CompanyCustomer_ID__c = '1-10009' + i );
            accList.add(newAcc);
        }
        insert accList;
       
      // create the Users
        List<User> userList = new List<User>();
        for (Integer i = 0; i< 200; i++) {
            USer newUser = new User(lastname = 'Test',Alias = 'Test',TimeZoneSidKey = 'GMT',
            LocaleSidKey = 'eu_ES',EmailEncodingKey = 'ISO-8859-1',
            ProfileId = '00ei0000000TYak',LanguageLocaleKey = 'en_US',
            userName='test' + i + '@tri-ed.com',email='test' + i + '@tri-ed.com');
            userList.add(newUser);
        }
        insert userList;
       
    //Create the TSG Records
    List<TSG__c> tsgList = new List<TSG__c>();
    For (Integer i = 0; i < 1; i++) {
        TSG__c newTSG = new TSG__c(Name = 'Test' + i,P21_CompanyCustomer_ID__c = accList[i].P21_CompanyCustomer_ID__c,email_address__c = 'jrosser@tri-ed.com');
       
        tsgList.add(newTSG);
      }
      insert tsgList;
   
   //     Try{
   //     update tsgList;
       
        
For(TSG__c tsg: tsgList) {
     ss.add(tsg.email_address__c);
     aa.add(tsg.P21_companycustomer_id__c);
     }
    
 
  //Get User Info
TheUser = [Select ID,email from User where email IN:ss];
//Get Account Info
TheAcct = [Select ID,P21_CompanyCustomer_ID__c from Account where P21_CompanyCustomer_ID__c IN:aa];
//
TheTSG = [Select ID,P21_companyCustomer_ID__c,email_address__c from TSG__C where P21_companyCustomer_ID__c IN:aa];

       
        IF(!TheAcct.isEmpty()) {
       
           
            //Loop Through the Trigger (New Records)
           
            For(TSG__c tsg2: TheTSG) {
           
             For (Account acct: TheAcct) { For (User email: TheUser) {
                
                
                
               if (tsg2.email_address__c == email.email && tsg2.P21_CompanyCustomer_ID__c == acct.P21_CompanyCustomer_ID__c) { //Make sure we match on email_address__c
                
                    AccountTeamMember ca = new AccountTeamMember();
                    ca.AccountId = acct.ID;
                    ca.TeamMemberRole = 'Technical Sales';
                    ca.UserId = email.ID;
                    acctMembers.add(ca);
                   
           //Insert into Account Share Here;
               
                AccountShare caSharingRule = new AccountShare();
                    caSharingRule.AccountId = acct.Id;
                    caSharingRule.OpportunityAccessLevel = 'Edit';
                    caSharingRule.CaseAccessLevel = 'Edit';
                    caSharingRule.AccountAccessLevel = 'Edit';
                    caSharingRule.UserOrGroupId = email.Id;
                    acctSharingRules.add(caSharingRule);
                
                   
                   
                            }
                         }
                       }
                     }
      
           
         }
        
        

                upsert acctMembers;
               upsert acctSharingRules; // This is what is causing the error
               
        
                
            
            }   

            }
jrosser1.39050288155405E12jrosser1.39050288155405E12
John, belive it or not I was able to resolve the error, I just changed my class, see updated class.
Basically I saw this post online about getting this error with a duplicate user being added to the account share, and since I had the test class hard coded to me, so I just changed that and good to go.

Thank you,

--Class

/**
This is a Class for the auto adding account team based on TSG Group
*/

@isTest

Private class add_team {

Private static testmethod void myTeamTest(){

List<AccountTeamMember> acctMembers = new List<AccountTeamMember>();

List<AccountShare> acctSharingRules = new List<AccountShare>();


//Hold SalesRep
Set <String> ss = New Set<String>();
//Hold Customer ID
Set <String> aa = New Set<String>();
List<User> TheUser=new LIST<User>();
//Hold Account Info
List<Account> TheAcct=new LIST<Account>();
List<TSG__c> TheTSG = new List<TSG__c>();

  
  
       // Create the Acounts
    //Links to TSG P21 Customer ID
        List<Account> accList = new List<Account>();
        for (Integer i = 0; i < 1; i++) {
            Account newAcc = new Account(Name='test' + i,P21_CompanyCustomer_ID__c = '1-10009' + i );
            accList.add(newAcc);
        }
        insert accList;
       
      // create the Users
        List<User> userList = new List<User>();
        for (Integer i = 0; i< 200; i++) {
            USer newUser = new User(lastname = 'Test',Alias = 'Test',TimeZoneSidKey = 'GMT',
            LocaleSidKey = 'eu_ES',EmailEncodingKey = 'ISO-8859-1',
            ProfileId = '00ei0000000TYak',LanguageLocaleKey = 'en_US',
            userName='test' + i + '@tri-ed.com',email='test' + i + '@tri-ed.com');
            userList.add(newUser);
        }
        insert userList;
       
    //Create the TSG Records
    List<TSG__c> tsgList = new List<TSG__c>();
    For (Integer i = 0; i < 1; i++) {
        TSG__c newTSG = new TSG__c(Name = 'Test' + i,P21_CompanyCustomer_ID__c = accList[i].P21_CompanyCustomer_ID__c,email_address__c = 'test' + i + '@tri-ed.com');
       
        tsgList.add(newTSG);
      }
      insert tsgList;
   
   //     Try{
   //     update tsgList;
       
        
For(TSG__c tsg: tsgList) {
     ss.add(tsg.email_address__c);
     aa.add(tsg.P21_companycustomer_id__c);
     }
    
 
  //Get User Info
TheUser = [Select ID,email from User where email IN:ss];
//Get Account Info
TheAcct = [Select ID,P21_CompanyCustomer_ID__c from Account where P21_CompanyCustomer_ID__c IN:aa];
//
TheTSG = [Select ID,P21_companyCustomer_ID__c,email_address__c from TSG__C where P21_companyCustomer_ID__c IN:aa];

       
        IF(!TheAcct.isEmpty()) {
       
           
            //Loop Through the Trigger (New Records)
           
            For(TSG__c tsg2: TheTSG) {
           
             For (Account acct: TheAcct) { For (User email: TheUser) {
                
                
                
               if (tsg2.email_address__c == email.email && tsg2.P21_CompanyCustomer_ID__c == acct.P21_CompanyCustomer_ID__c) { //Make sure we match on email_address__c
                
                    AccountTeamMember ca = new AccountTeamMember();
                    ca.AccountId = acct.ID;
                    ca.TeamMemberRole = 'Technical Sales';
                    ca.UserId = email.ID;
                    acctMembers.add(ca);
                   
           //Insert into Account Share Here;
               
                AccountShare caSharingRule = new AccountShare();
                    caSharingRule.AccountId = acct.Id;
                    caSharingRule.OpportunityAccessLevel = 'Edit';
                    caSharingRule.CaseAccessLevel = 'Edit';
                    caSharingRule.AccountAccessLevel = 'Edit';
                    caSharingRule.UserOrGroupId = email.Id;
                    acctSharingRules.add(caSharingRule);
                
                   
                   
                            }
                         }
                       }
                     }
      
           
         }
        
        

                upsert acctMembers;
               upsert acctSharingRules; // This is what is causing the error
               
        
                
            
            }   

            }