You need to sign in to do that
Don't have an account?
Kathryn Bullock
Test Class to catch a duplicate
Hello,
I am trying to create a test class for this trigger:
I think my problem is with assigning the second opp to be put on the Test Account. How should I do this so that it does so correctly?
My current test class:
I am trying to create a test class for this trigger:
trigger leadDuplicatePreventer on opportunity(before insert) { set<string> settgs = new set<string>(); list<opportunity> opps = [select id,Trigger_Help__c from opportunity WHERE CreatedDate = LAST_N_DAYS:90]; Profile p=[SELECT ID, Name FROM Profile WHERE Id=:userinfo.getProfileId() Limit 1]; for(opportunity opp : opps){ if(opp.Trigger_Help__c != null && p.Name <> 'System Administrator'){ settgs.add(opp.Trigger_Help__c); } } for(opportunity op : trigger.new){ if(settgs.contains(op.Trigger_Help__c)){ op.adderror('An Opportunity of this type already exists on this Account. Please contact a system administrator with questions.'); } } }
I think my problem is with assigning the second opp to be put on the Test Account. How should I do this so that it does so correctly?
My current test class:
@isTest private class TestleadDuplicatePreventer { @isTest static void TestleadDuplicatePreventerwithOneOpp() { Account acct = new Account(Name='Test Account'); insert acct; Opportunity opp = new Opportunity(Name=acct.Name + ' Opportunity', StageName='Open Opportunity', CloseDate=System.today().addMonths(1), Facility__c='Tacoma WA', Oppty_Type__c='UCO Service', AccountID=acct.Id); insert opp; Test.startTest(); opp= new Opportunity(Name='Opportunity Test', StageName='Open Opportunity', CloseDate=System.today().addMonths(2), Facility__c='Tacoma WA', Oppty_Type__c='UCO Service', Account='Test Account'); try { insert opp; } catch(Exception duplicate) { } Test.stopTest(); } }
From the code above I can see the criteria for duplicate opp is the Trigger_Help__c. Additionally you are checking if this field is not null then add it to set.
While in test class you are not adding any value for Trigger_Help__c & the logic for not null doesn't allow it to fit into set.
So the logic for duplicate will not fire i.e. the error message wont be hit.
So you need to add the Trigger_Help__c field value in test class in order to get it distinguished.
Regards,
Mayuri Patil.
The Trigger_Help__c is a formula created by adding the Oppty Type and the Id associated with the account. So is there a different way I should be tackling this test class?