You need to sign in to do that
Don't have an account?

Apex test class error
I am writing a Test class for a trigger and when I run it I get the following error, it is referencing a line in the trigger, but I am not sure how to address this.
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountTeamADD: execution of BeforeInsert
caused by: System.DmlException: Upsert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []
Trigger.AccountTeamADD: line 76, column 1: []
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountTeamADD: execution of BeforeInsert
caused by: System.DmlException: Upsert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []
Trigger.AccountTeamADD: line 76, column 1: []
Can you post your trigger and if possible test class so that we can take a look at where it went wrong?
Thank you.
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;
// break; //Match found we can break out of the loop and go to the next record
}
}
-------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------
/**
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 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;
}
// catch (System.DmlException e) {
// System.debug('we caught a dml exception: ' + e.getDmlMessage(0));
// }
// }
}
You can include "test.runAs(User)" where user would be the owner of the record you want to upsert.
So since you are using AccountShare object there might be an issue with security as sharing work little different and there might be various reasons for your test class to fail.
Quick questions though, did you try running the trigger live and check if its working?
I found this link about sharing that might have caused you the issue, can you go through it and see if you can sort it out?
http://www.forcetree.com/2011/12/insufficientaccessoncrossreferenceentit.html