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 

Apex Class, For Inserting Account Teams

I created a trigger on Custom Object TSG__c to insert a record into the account team when the record is updated.  The trigger works fine, now I am just trying to write the class.

I am inserting records for 200 Accounts, 200 Users and 200 AccountTeamMemebers and referencing the ID's from Accounts,Users as the Account Team is being created, but I do not know how to tie it all back to gether.

Any Ideas?

@isTest
private class addAccountTeam {

    @isTest
    static void test_checkTSGAccounTeamInsert() {
   

   
    // Create the Acounts
        List<Account> accList = new List<Account>();
        for (Integer i = 0; i < 200; i++) {
            Account newAcc = new Account(Name='Test' + i,P21_CompanyCustomer_ID__c = '1-10009' + i );
            accList.add(newAcc);
        }
        insert accList;
       
   // create the TSGs
        List<TSG__c> tsgList = new List<TSG__c>();
        for (Integer i = 0; i < 200; i++) {
            TSG__c newTSG = new TSG__c(Name='Test' + i,email_address__c ='Test' + i + '@Tri-ed.com',
                                P21_CompanyCustomer_ID__c = accList[i].P21_CompanyCustomer_ID__c);
            tsgList.add(newTSG);
        }
        insert tsgList; 
       
        // create the Users
        List<User> userList = new List<User>();
        for (Integer i = 0; i< 200; i++) {
            // observe the same name for Opp and TSG
            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 AccountTeam
        List<AccountTeamMember> teamList = new List<AccountTeamMember>();
        For (Integer i = 0; i< 200; i++) {
        AccountTeamMember newTeam = new AccountTeamMember(AccountId = accList[i].ID,TeamMemberRole = 'Technical Sales',UserId = userList[i].ID);
        teamList.add(newTeam);
        }
        insert teamList;
       
        //Create the AccountTeamShare
        List<AccountShare> shareList = new List<AccountShare>();
        For (Integer i = 0; i< 200; i++) {
        AccountShare newShare = new AccountShare(AccountId = accList[i].ID,UserOrGroupId = userList[i].ID,CaseAccessLevel = 'Edit',AccountAccessLevel = 'Edit',OpportunityAccessLevel = 'Edit');
        shareList.add(newShare);
        }
        insert shareList;

    
         //Check to Make Sure records Match Up.
     teamList = [Select AccountID,UserId,TeamMemberRole from AccountTeamMember order by ID];
     accList = [Select ID,P21_CompanyCustomer_ID__c from Account order by ID];
    
     For (AccountTeamMember team: teamList) {
       
                 For (Account acct: accList) {
       
            System.assertEquals(team.AccountID,acct.ID);
       
        }
         }
}
}
Best Answer chosen by jrosser1.39050288155405E12
James LoghryJames Loghry

Your assert is "asserting" that all AccountTeamMembers are members of All Accounts.

Try changing it around to the following:

for (Integer i=0; i < accList.size(); i++) {
     System.assertEquals(accList.get(i).Id,teamList.get(i).AccountId);
}
Also, you will need to add additional asserts to ensure the logic in your trigger is running properly

All Answers

jrosser1.39050288155405E12jrosser1.39050288155405E12
This is the error: System.AssertException: Assertion Failed: Expected: 001f000000htsFWAAY, Actual: 001f000000htsFVAAY

I cannot seem to get them to sync up.
James LoghryJames Loghry

Your assert is "asserting" that all AccountTeamMembers are members of All Accounts.

Try changing it around to the following:

for (Integer i=0; i < accList.size(); i++) {
     System.assertEquals(accList.get(i).Id,teamList.get(i).AccountId);
}
Also, you will need to add additional asserts to ensure the logic in your trigger is running properly
This was selected as the best answer
jrosser1.39050288155405E12jrosser1.39050288155405E12
James, thank you,  That did help, now I just need to get the % coverage a little higher.

Thanks again.