You need to sign in to do that
Don't have an account?
Abhishek Singh 88
Stucked in test class coverage .
Hi All,
I have written a small trigger on opportunity.Which is updating field 'opportunity sales Leader' with the user in opportunity team member.Here is trigger.
And Here Is Test clas.;
But its covering only 53 %
this if condition and also error statment is not been covered.any suggestion on this would be really help full
I have written a small trigger on opportunity.Which is updating field 'opportunity sales Leader' with the user in opportunity team member.Here is trigger.
trigger UpdateOpportunity on Opportunity (after insert) { Set<Id> setOfOppty = new Set<Id>(); List<Opportunity> listOfOpptyToUpdate = new List<Opportunity>(); for(Opportunity opp : Trigger.New){ if(opp != null && (opp.RecordTypeId == Label.Oppty_Record_Type_ISV || opp.RecordTypeId == Label.Oppty_Record_Type_OEM || opp.RecordTypeId == Label.Oppty_Record_Type_Wholesale || opp.RecordTypeId == Label.Oppty_Record_Type_xSP)){ setOfOppty.add(opp.Id); } } List<OpportunityTeamMember> listOTM = new List<OpportunityTeamMember>([SELECT Id, UserId, OpportunityId, User.Name FROM OpportunityTeamMember WHERE OpportunityId in :setOfOppty AND TeamMemberRole = :Label.Opportunity_Sales_Leader]); for(OpportunityTeamMember otmObj: listOTM){ if(otmObj != null && otmObj.UserId!=null && otmObj.OpportunityId != null){ Opportunity opp = new Opportunity(Id = otmObj.OpportunityId, Opportunity_Team_Member_Sales_Leader__c = otmObj.UserId); listOfOpptyToUpdate.add(opp); } } if(!listOfOpptyToUpdate.IsEmpty()){ Database.SaveResult[] saveResultList = Database.update(listOfOpptyToUpdate,false); for(Database.SaveResult svResult : saveResultList){ if(!svResult.isSuccess()){ system.debug('Error::'+svResult); break; } } } }
And Here Is Test clas.;
@Istest(SeeAllData=false) Private class Test_opportunity { public static testmethod void testOpportunity() { //ProId = [SELECT Id FROM Profile WHERE Name = 'Consumer Sales Ops'].Id; User user = new User(); user.FirstName = 'Test'; user.LastName = 'Name'; user.CompanyName = 'IT Test Company'; user.MobilePhone = '123-456-7890'; user.Username = 'testUser-' + '@test.com'; user.Email = 'testUser-' +'@test.com'; user.Alias = 'test'; user.CommunityNickname = 'test1'; user.TimeZoneSidKey = 'America/New_York'; user.LocaleSidKey = 'en_US'; user.EmailEncodingKey = 'UTF-8'; user.ProfileId = '00e1I000000Veel'; user.LanguageLocaleKey = 'en_US'; user.Street = '123 Test St'; user.City = 'Testcity'; user.State = 'va'; user.PostalCode = '23223'; user.Country = 'USA'; insert user; Date myDate = Date.today(); Opportunity opp=new opportunity(); opp.RecordTypeId='0122F0000004ot9'; opp.name='test opp'; opp.AccountId='0012F00000EXcyQ'; opp.CurrencyIsoCode='AED'; opp.CloseDate= myDate; opp.StageName='Identification'; opp.Opportunity_Team_Member_Sales_Leader__c=user.Id; insert opp; OpportunityTeamMember opptyteam=new OpportunityTeamMember(); opptyteam.opportunityId=opp.Id; opptyteam.TeamMemberRole='Sales Leader'; opptyteam.UserId=user.Id; insert opptyteam; opp.Opportunity_Team_Member_Sales_Leader__c = opptyteam.UserId; update opp; } }
But its covering only 53 %
if(otmObj != null && otmObj.UserId!=null && otmObj.OpportunityId != null){ Opportunity opp = new Opportunity(Id = otmObj.OpportunityId, Opportunity_Team_Member_Sales_Leader__c = otmObj.UserId); listOfOpptyToUpdate.add(opp); }
this if condition and also error statment is not been covered.any suggestion on this would be really help full
trigger UpdateOpportunity on Opportunity (after insert ,after update )
As on insert you will not get OpportunityTeamMember.
All Answers
trigger UpdateOpportunity on Opportunity (after insert ,after update )
As on insert you will not get OpportunityTeamMember.
I think you must run opportunity and opportunity team member as RUNAS from user. i have tried to solve your problem in attested code below :)
Have a Look and complete the details of Profile name and opportunity Record type Name in code below and run it !!! It will work as expected :)
Please let us know if this will help you.
Thanks,
Parag Bhatt