I'm using objects Opportunity, OpportunityTeamMember, User, Opportunity_Team_Member, and Employee__c.
For me to create a member, i will choose an employee, checks if the email has match with user and if true, will create the standard OpportunityTeamMember but if false, will create the custom object.
Both the parent of course would be the Opportunity. I'm using a custom profile with read,create,edit permission on Opportunity, Opportunity_Team__c, and read only on Employee.
Question is why the error does the error appear when the profile is not given a modifyAll on Opportunity? Read/Create/Edit is not enough.
Other objects (employees, opportunity_team__c) doesn't affect it so even if read-only. It allows creation since it's done through apex. Also, even if I switch the field type (Master/Lookup) to parent Oppy, it doesn't seem to fix it. Sample test code is below:
User user = sampleUser(OpportunityTeamUtils.PROFILE_TECH_ADMIN, 'tstz');
User user1;
User user2;
User user3;
User user4;
Employee__c emp1;
Employee__c emp2;
Employee__c emp3;
Employee__c emp4;
Employee__c emp5;
Product2 prod;
Product2 prod2;
Account acct;
Opportunity opp;
OpportunityTeamMember otm;
Opportunity_Team__c otmC;
List<OpportunityTeamMember> otmList;
List<OpportunityTeamMember> otmList2;
List<Opportunity_Team__c> otmClist;
Test.startTest();
System.runAs(user) {
user1 = sampleUser(OpportunityTeamUtils.PROFILE_SR_MANAGEMENT, 'tsta');
user2 = sampleUser(OpportunityTeamUtils.PROFILE_SR_MANAGEMENT, 'tstb');
user3 = sampleUser(OpportunityTeamUtils.PROFILE_SR_MANAGEMENT, 'tstc');
user4 = sampleUser(OpportunityTeamUtils.PROFILE_SR_MANAGEMENT, 'tstd');
emp1 = sampleEmployee('tsta');
emp2 = sampleEmployee('tstb');
emp3 = sampleEmployee('tstc');
emp4 = sampleEmployee('tstd');
emp5 = sampleEmployee('tste');
prod = sampleProductFamily('prodFam');
prod2 = sampleProduct('prod', prod.Id);
}
System.runAs(user1) {
acct = sampleAccount('test');
insert acct;
}
System.runAs(user2) {
opp = sampleOpportunity('testOpp', acct.Id, prod2.Id);
insert opp;
OpportunityTeamController.createTeamMember(opp.Id, emp3.Id, OpportunityTeamUtils.ROLE_FOREIGNEXCHANGEMARKETER);
}
System.runAs(user3) {
//Create/update/delete an opportunity team member standard object
OpportunityTeamController.createTeamMember(opp.Id, emp4.Id, OpportunityTeamUtils.ROLE_PRODUCT_PARTNER);
otmList = [SELECT
Id, TeamMemberRole FROM OpportunityTeamMember WHERE OpportunityId=:opp.Id AND TeamMemberRole=:OpportunityTeamUtils.ROLE_PRODUCT_PARTNER AND UserId=:user4.Id];
System.assert(otmList.size() == 1, 'createTeamMember failed');
OpportunityTeamController.updateTeamMember(otmList[0].Id, user4.Id, opp.Id, OpportunityTeamUtils.ROLE_OTHERBUSINESSPARTNER, true, false, OpportunityTeamUtils.ROLE_PRODUCT_PARTNER, user4.FirstName+' '+user4.LastName);
otmList = [SELECT Id, TeamMemberRole FROM OpportunityTeamMember WHERE OpportunityId=:opp.Id AND TeamMemberRole=:OpportunityTeamUtils.ROLE_OTHERBUSINESSPARTNER];
System.assert(otmList.size() == 1, 'updateTeamMember failed');
OpportunityTeamController.deleteTeamMember(otmList[0].Id, OpportunityTeamUtils.OBJECTAPINAME_STANDARD, opp.Id, OpportunityTeamUtils.ROLE_OTHERBUSINESSPARTNER, false, user4.FirstName+' '+user4.LastName);
otmList = [SELECT Id, TeamMemberRole FROM OpportunityTeamMember WHERE OpportunityId=:opp.Id AND TeamMemberRole=:OpportunityTeamUtils.ROLE_OTHERBUSINESSPARTNER];
System.assert(otmList.size() == 0, 'deleteTeamMember failed');
//Create/update/delete an opportunity team member custom object
OpportunityTeamController.createTeamMember(opp.Id, emp5.Id, OpportunityTeamUtils.ROLE_PRODUCT_PARTNER);
otmClist = [SELECT Id, Team_Role__c FROM Opportunity_Team__c WHERE Opportunity__c=:opp.Id AND Team_Role__c=:OpportunityTeamUtils.ROLE_PRODUCT_PARTNER];
System.assert(otmClist.size() == 1, 'createTeamMember failed');
OpportunityTeamController.updateTeamMember(otmClist[0].Id, emp5.Id, opp.Id, OpportunityTeamUtils.ROLE_OTHERBUSINESSPARTNER, false, false, OpportunityTeamUtils.ROLE_PRODUCT_PARTNER, emp5.name);
otmClist = [SELECT Id, Team_Role__c FROM Opportunity_Team__c WHERE Opportunity__c=:opp.Id AND Team_Role__c=:OpportunityTeamUtils.ROLE_OTHERBUSINESSPARTNER];
System.assert(otmClist.size() == 1, 'updateTeamMember failed');
OpportunityTeamController.deleteTeamMember(otmClist[0].Id, OpportunityTeamUtils.OBJECTAPINAME_CUSTOM, opp.Id, OpportunityTeamUtils.ROLE_OTHERBUSINESSPARTNER, false, emp5.name);
otmClist = [SELECT Id, Team_Role__c FROM Opportunity_Team__c WHERE Opportunity__c=:opp.Id AND Team_Role__c=:OpportunityTeamUtils.ROLE_OTHERBUSINESSPARTNER];
System.assert(otmClist.size() == 0, 'deleteTeamMember failed');
}
Test.stopTest();