+ Start a Discussion


I keep getting this error when running the test case on the trigger and I have no idea why.


Can anybody shed any light on the situation?


The Trigger and Test Case are Below.



trigger UpdateTeamMembers on Account (before update, before insert) { AccountTeamMember[] newmembers = new AccountTeamMember[]{}; //list of new team members to add AccountShare[] newShare = new AccountShare[]{}; //list of new shares to add Map<Id,Id> ForDel = new Map<Id,Id>(); //map of team members and shares to delete for(Account a:trigger.new){ //for all changing accounts if (trigger.old[0].X2ndOwnerTM__c != a.X2ndOwnerTM__c ){ //if the owner has changed ForDel.put(a.Id,trigger.old[0].X2ndOwnerTM__c); //add account id and old user id to map AccountTeamMember Teammemberad=new AccountTeamMember(); Teammemberad.AccountId=a.id; Teammemberad.UserId=a.X2ndOwnerTM__c; Teammemberad.TeamMemberRole = a.X2nd_Owner_Role__c; newmembers.add(Teammemberad); //add new team member to list } } //del all old account shares List<AccountShare> delASQ = [Select Id, AccountId, UserOrGroupId FROM AccountShare WHERE AccountId IN : ForDel.keySet()]; List<AccountShare> delAS = new List<AccountShare>(); for(AccountShare ASfor: delASQ){ if(ASfor.UserOrGroupId==ForDel.get(ASfor.AccountId)){ delAS.add(ASfor); } } delete delAS; // delete All Account Share //del all old team members List<AccountTeamMember> delATMQ = [Select Id, AccountId, UserId FROM AccountTeamMember WHERE AccountId IN : ForDel.keySet()]; List<AccountTeamMember> delATM = new List<AccountTeamMember>(); for(AccountTeamMember ATMfor: delATMQ){ if(ATMFor.UserId==ForDel.get(ATMfor.AccountId)){ delATM.add(ATMfor); } } delete delATM; Database.SaveResult[] lsr = Database.insert(newmembers,false);//insert any valid members then add their share entry if they were successfully added Integer newcnt=0; for(Database.SaveResult sr:lsr){ if(!sr.isSuccess()){ //if not success throw error message Database.Error emsg =sr.getErrors()[0]; system.debug('\n\nERROR ADDING TEAM MEMBER:'+emsg); }else{ newShare.add(new AccountShare (UserOrGroupId=newmembers[newcnt].UserId, AccountId=newmembers[newcnt].Accountid, AccountAccessLevel='Edit',OpportunityAccessLevel='Edit',ContactAccessLevel='Edit', CaseAccessLevel='Edit' )); } newcnt++; } Database.SaveResult[] lsr0 =Database.insert(newShare,false); //insert the new shares Integer newcnt0=0; for(Database.SaveResult sr0:lsr0){ if(!sr0.isSuccess()){ Database.Error emsg0=sr0.getErrors()[0]; system.debug('\n\nERROR ADDING SHARING:'+newShare[newcnt0]+'::'+emsg0); } newcnt0++; } }



@isTest private class UpdateTeamMembersTest { static testMethod void myUnitTest() { // TO DO: implement unit test Account account = new Account( Name = 'Apex Test Account', Type = 'Customer', OwnerId='00520000000rd6FAAQ', IF_City__c='Singapore', IF_PostalCode__c='629065', IF_Street__c='41 Joo Koon Circle', IF_Region__c='NULL', IF_Country__c='Singapore', Phone='+65 6862 3811', IF_Fax__c='NULL', Industry='Others', IF_Classification__c='D (<25.000 EUR)', RecordTypeId='012200000004g6HAAQ', IF_Name2__c='NULL', IF_Name4__c='NULL', Website='www.echthaar.nl', UsePOBoxAddress__c='NULL', IF_POBoxNumber__c='NULL', IF_POBoxPostalCode__c='NULL',Type_Detail__c='NULL', NA_Key_Account__c='NULL'); insert account; //select statement to get the id of account created account AccID = [select id from account where IF_Street__c = '41 Joo Koon Circle' limit 1]; AccountSetupForm__c accsetup = new AccountSetupForm__c( AccountName__c= AccID.Id, ACH_WIRE_CHECK__c = 'CHECK', Attachment_Media__c ='No Attachment', Attachment_Reports__c = 'No Attachment', CurrencyIsoCode='USD', H100MinimumOrder__c=TRUE, Invoice_Currency__c='USD (US Dollars)', Invoice_Frequency__c='Weekly', Invoice_Media_Type__c='Email', Layout_Type__c='Summary by Date and Product', Name='41 Joo Koon Circle', No_Costomer_References__c=FALSE, Primary_Email__c='stan@temp.com', Rebate__c=FALSE, RecordTypeId='012200000004o6TAAQ', Required_Customer_References__c=FALSE, Same_as_Bill_To_Address__c=FALSE, Same_as_Billing_Contact__c = FALSE, Status__c='Draft', Template_Type__c='Standard', Terms__c='Net 15', Validated_Customer_References__c=FALSE); insert accsetup; account.X2ndOwnerTM__c = '005200000015nd1AAA'; account.X2nd_Owner_Role__c = 'Temp'; update account; } }






I removed "before insert" because its not possible to use trigger.old on inserts, only updates.
Did this solve the issue?  Let me know if I should look deeper.