You need to sign in to do that
Don't have an account?
Kathryn Bullock
Help - writing a test class for a trigger to prevent duplicate Opportunities
I new to Apex, and failing miserably. Time and time again and I cannot come up with anything beyond 0% code coverage. I have the following trigger:
If anyone is out there who can help it would be greatly appreciated!
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.'); } } }
The Trigger_Help__c is a formula field that combines Oppty_Type__c+Account Id. These 9 lines are all I have to figure out and I cannot get this for the life of me. My current test class is as follows:
@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', AccountId=acct.Id); try { insert opp; } catch(Exception duplicate) { System.assertEquals('An Opportunity of this type already exists on this Account. Please contact a system administrator with questions.', duplicate.getMessage()); } //this should probably be in a separate test method Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; User u2 = new User(Alias = 'newUser', Email='newuser@testorg.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='newuser@testorg.com'); System.runAs(u2) { insert opp; } Test.stopTest();}}But I have also been trying and failing with the following test class:
@isTest public class TestleadDuplicatePreventer { static testMethod Void TestleadDuplicatePreventerwithOneOpp() { User u1 = [SELECT Id FROM User WHERE Alias='jmilt']; System.runAs(u1){ Account acct = new Account(Name='Test Account'); insert acct; Opportunity opp1 = 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); Opportunity opp2 = new Opportunity(Name=acct.Name + ' Opportunity1', StageName='Open Opportunity', CloseDate=System.today().addMonths(1), Facility__c='Tacoma WA', Oppty_Type__c='UCO Service & Indoor Equipment', AccountID=acct.Id); Opportunity opp3 = new Opportunity(Name=acct.Name + ' Opportunity2', StageName='Open Opportunity', CloseDate=System.today().addMonths(1), Facility__c='Tacoma WA', Oppty_Type__c='Trap Service', AccountID=acct.Id); Opportunity[] opps = new Opportunity [] {opp1, opp2, opp3}; insert opps; Opportunity dup1 = new Opportunity(Name='Check', StageName='Open Opportunity', CloseDate=System.today().addMonths(1), Facility__c='Tacoma WA', Oppty_Type__c='UCO Service', AccountID=acct.Id); Test.startTest(); try { insert dup1; } catch(Exception duplicate) { System.assertEquals('An Opportunity of this type already exists on this Account. Please contact a system administrator with questions.', duplicate.getMessage()); }} Test.stopTest(); } }
If anyone is out there who can help it would be greatly appreciated!
All Answers
Mark as best answer if it helps you.
Thanks,
Ghanshyam Choudhari
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_USERNAME, Duplicate Username.<br>The username already exists in this or another Salesforce organization. Usernames must be unique across all Salesforce organizations. To resolve, use a different username (it doesn't need to match the user's email address). : [Username]