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

Opportunity code coverage problem
Hello,
I am getting 71% code coverage. I have to be missing something really simple.
I have posted my test plan and trigger below.
Please let me know if you have any questions.
I would appreciate any help.
== Test Plan ==
@isTest(SeeAllData=false)
private class TestCreateOppTeam{
@isTest(SeeAllData=false)
private static void TestCreateOppTeam() {
Opportunity opp = new Opportunity(Name='Test opp', StageName='stage', Probability = 10, CloseDate=system.today(), Campaign_Medium__c = 'Campaign_Medium__c',Campaign_Name__c = 'Campaign_Name__c',Product__c = '01t90000004q3PH',Call_Centre__c = 'Call_Centre__c');
insert opp;
Opportunity [] OL = [select Id from Opportunity where Id = :opp.id];
for (Opportunity o: OL ) {
UserRole rn=[Select Name from UserRole where Id= :UserInfo.getUserRoleId()];
String Role = rn.name;
OpportunityTeamMember newTeamMember = new OpportunityTeamMember();
newTeamMember.OpportunityId = o.id;
newTeamMember.UserId = UserInfo.getUserId();
if (role != 'Admin') {
if (role.containsIgnoreCase('TEST WORD')) {
newTeamMember.TeamMemberRole = 'Role 1';
} else {
newTeamMember.TeamMemberRole = 'Role 2';
}
insert newTeamMember;
}
}
}
}
== Trigger ==
trigger CreateOppTeam on Opportunity (after insert) {
for (Opportunity o: trigger.new) {
UserRole rn=[Select Name from UserRole where Id= :UserInfo.getUserRoleId()];
String Role = rn.name;
OpportunityTeamMember newTeamMember = new OpportunityTeamMember();
newTeamMember.OpportunityId = o.id;
newTeamMember.UserId = UserInfo.getUserId();
if (role != 'Admin') {
if (role.containsIgnoreCase('TEST WORD')) {
newTeamMember.TeamMemberRole = 'Role 1';
} else {
newTeamMember.TeamMemberRole = 'Role 2';
}
insert newTeamMember;
}
}
}
I am getting 71% code coverage. I have to be missing something really simple.
I have posted my test plan and trigger below.
Please let me know if you have any questions.
I would appreciate any help.
== Test Plan ==
@isTest(SeeAllData=false)
private class TestCreateOppTeam{
@isTest(SeeAllData=false)
private static void TestCreateOppTeam() {
Opportunity opp = new Opportunity(Name='Test opp', StageName='stage', Probability = 10, CloseDate=system.today(), Campaign_Medium__c = 'Campaign_Medium__c',Campaign_Name__c = 'Campaign_Name__c',Product__c = '01t90000004q3PH',Call_Centre__c = 'Call_Centre__c');
insert opp;
Opportunity [] OL = [select Id from Opportunity where Id = :opp.id];
for (Opportunity o: OL ) {
UserRole rn=[Select Name from UserRole where Id= :UserInfo.getUserRoleId()];
String Role = rn.name;
OpportunityTeamMember newTeamMember = new OpportunityTeamMember();
newTeamMember.OpportunityId = o.id;
newTeamMember.UserId = UserInfo.getUserId();
if (role != 'Admin') {
if (role.containsIgnoreCase('TEST WORD')) {
newTeamMember.TeamMemberRole = 'Role 1';
} else {
newTeamMember.TeamMemberRole = 'Role 2';
}
insert newTeamMember;
}
}
}
}
== Trigger ==
trigger CreateOppTeam on Opportunity (after insert) {
for (Opportunity o: trigger.new) {
UserRole rn=[Select Name from UserRole where Id= :UserInfo.getUserRoleId()];
String Role = rn.name;
OpportunityTeamMember newTeamMember = new OpportunityTeamMember();
newTeamMember.OpportunityId = o.id;
newTeamMember.UserId = UserInfo.getUserId();
if (role != 'Admin') {
if (role.containsIgnoreCase('TEST WORD')) {
newTeamMember.TeamMemberRole = 'Role 1';
} else {
newTeamMember.TeamMemberRole = 'Role 2';
}
insert newTeamMember;
}
}
}
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_tools_runas.htm (https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_tools_runas.htm" target="_blank)
I created a second testmethod that runs under the new context. You should specify a user profile name in the SOQL statement for the user object. Ensure you choose a profile that has appropriate CRUD and FLS.
Also, here is the revised trigger.
PLEASE KEEP IN MIND THAT I FREEHANDED ALL OF THIS IN NOTEPAD
SO THERE MAY BE SOME SYNTAX ERRORS.
Hope that helps.
All Answers
After taking a quick look at your code, you have more pressing issues than coverage. Please review this article on how to properly bulkify your code. https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code (https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code" target="_blank)
Specifically, in your trigger you should move:
Those lines outside of your for loop(before).
You should also move: Outside of your for loop(after).
To do this, you will need to create a collection of opportunityTeamMembers and then insert them all at one time after your loop.
Any thoughts on code coverage? It is not possible to deploy without increasing it and I am sure it is something very trivial that i am missing.
I will write somehting up real fast and explain my reasoning.
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_tools_runas.htm (https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_tools_runas.htm" target="_blank)
I created a second testmethod that runs under the new context. You should specify a user profile name in the SOQL statement for the user object. Ensure you choose a profile that has appropriate CRUD and FLS.
Also, here is the revised trigger.
PLEASE KEEP IN MIND THAT I FREEHANDED ALL OF THIS IN NOTEPAD
SO THERE MAY BE SOME SYNTAX ERRORS.
Hope that helps.