You need to sign in to do that
Don't have an account?
Timothy Smith
APEX testing cant populate my Trigger.new
Trigger will close Opportunity if no valid phone numbers exist.
Opportunity has Primary_Contact__c and Secondary_Contact__c
On Contact 3 phone fields to check:
Phone__c
Work_phone__c
Mobile_phone__c
On Contact 3 Phone Status Fields:
Phone_Status__c
Work_Status__c
Mobile_Status__c.
After Insert/Update of a Contact, the Trigger will check all Opportunities associated with the Contact. If there are no valid (meaning having a number and corresponding status is not equal to "Do Not Call"), then close the Opportunity.
I have created the Trigger and Test Class but I don't understand why the lines in the test class are not executing.
Thank you.
Trigger:
Test Class:
Opportunity has Primary_Contact__c and Secondary_Contact__c
On Contact 3 phone fields to check:
Phone__c
Work_phone__c
Mobile_phone__c
On Contact 3 Phone Status Fields:
Phone_Status__c
Work_Status__c
Mobile_Status__c.
After Insert/Update of a Contact, the Trigger will check all Opportunities associated with the Contact. If there are no valid (meaning having a number and corresponding status is not equal to "Do Not Call"), then close the Opportunity.
I have created the Trigger and Test Class but I don't understand why the lines in the test class are not executing.
Thank you.
Trigger:
trigger Contact_DNC_Opportunity on Contact (after insert, after update){ List<Opportunity> OpportunitiesToClose = new List<Opportunity>(); List<Opportunity> WorkingOpportunities = [SELECT StageName, Primary_Contact__c, Secondary_Contact__c, Primary_Contact__r.Phone_Status__c, Primary_Contact__r.Mobile_Phone_Status__c, Primary_Contact__r.Work_Phone_Status__c, Primary_Contact__r.Phone, Primary_Contact__r.Normalized_Mobile_Phone__c, Primary_Contact__r.Normalized_Work_Phone__c, Secondary_Contact__r.Phone_Status__c, Secondary_Contact__r.Mobile_Phone_Status__c, Secondary_Contact__r.Work_Phone_Status__c, Secondary_Contact__r.Phone, Secondary_Contact__r.Normalized_Mobile_Phone__c, Secondary_Contact__r.Normalized_Work_Phone__c FROM Opportunity WHERE Primary_Contact__c In :Trigger.New OR Secondary_Contact__c In :Trigger.New ]; System.debug(WorkingOpportunities); for(Opportunity opp: WorkingOpportunities){ //Check Primary Contact phone numbers if ((opp.Primary_Contact__r.Phone == Null) || (opp.Primary_Contact__r.Phone_Status__c == 'Do Not Call')&& (opp.Primary_Contact__r.Normalized_Mobile_Phone__c == Null) || (opp.Primary_Contact__r.Mobile_Phone_Status__c == 'Do Not Call')&& (opp.Primary_Contact__r.Normalized_Work_Phone__c == Null) || (opp.Primary_Contact__r.Work_Phone_Status__c == 'Do Not Call')&& (opp.Secondary_Contact__r.Phone == Null) || (opp.Secondary_Contact__r.Phone_Status__c == 'Do Not Call')&& (opp.Secondary_Contact__r.Normalized_Mobile_Phone__c == Null) || (opp.Secondary_Contact__r.Mobile_Phone_Status__c == 'Do Not Call')&& (opp.Secondary_Contact__r.Normalized_Work_Phone__c == Null) || (opp.Secondary_Contact__r.Work_Phone_Status__c == 'Do Not Call') ) { //Add opp to List if fits criteria opportunitiesToClose.add(opp); } } for (Opportunity opp1 : opportunitiesToClose){ opp1.StageName = 'Closed Lost'; } if(opportunitiesToClose.size() > 0){ update opportunitiesToClose ; } }
Test Class:
@isTest private class ContactDNCOpportunityTest { private static testMethod void testCloseOpps(){ //Create Account Account newAcc = FlowTestUtils.createHouseholdAccount(); insert newAcc; //Create Contacts List<Contact> conList = new List<Contact> { new Contact(FirstName='test1',LastName='tester',AccountId = newAcc.Id, Email = 'test1@testing.com', Phone = '1234567891', Phone_Status__c = 'Do Not Call'), //Has Phone Number - Do not call, Closed Opp new Contact(FirstName='test2',LastName='tester',AccountId = newAcc.Id, Email = 'test2@testing.com'), //Blank No Numbers Closed Opp new Contact(FirstName='test3',LastName='tester',AccountId = newAcc.Id, Email = 'test3@testing.com', Normalized_Work_Phone__c = '1234567891',Work_Phone_Status__c = 'Active'), // Has Work Number, Active, Do Not Close Opp new Contact(FirstName='test4',LastName='tester',AccountId = newAcc.Id, Email = 'test4@testing.com', Phone = '1234567891', Phone_Status__c = 'Active'), //Has Phone Number, Active, Do not Close Opp new Contact(FirstName='test5',LastName='tester',AccountId = newAcc.Id, Email = 'test5@testing.com', Normalized_Mobile_Phone__c = '1234567891', Mobile_Phone_Status__c = 'Do Not Call'), //Has mobile number, DNC, Close Opp new Contact(FirstName='test6',LastName='tester',AccountId = newAcc.Id, Email = 'test6@testing.com', Normalized_Work_Phone__c = '1234567891', Work_Phone_Status__c = 'Do Not Call') //Has Worknumber DNC is checked }; insert conList; Opportunity opptest1 = new Opportunity(Name = 'Opp1', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Primary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test1'].id); Test.startTest(); insert opptest1; Test.stopTest(); System.assertEquals('Closed', opptest1.StageName); Opportunity opptest2 = new Opportunity(Name = 'Opp2', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Primary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test2'].id); Test.startTest(); insert opptest2; Test.stopTest(); System.assertEquals('Closed', opptest2.StageName); Opportunity opptest3 = new Opportunity(Name = 'Opp3', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Primary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test3'].id); Test.startTest(); insert opptest3; Test.stopTest(); System.assertEquals('Closed', opptest3.StageName); Opportunity opptest4 = new Opportunity(Name = 'Opp4', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Secondary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test4'].id); Test.startTest(); insert opptest4; Test.stopTest(); System.assertNotEquals('Closed', opptest4.StageName); Opportunity opptest5 = new Opportunity(Name = 'Opp5', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Secondary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test5'].id); Test.startTest(); insert opptest5; Test.stopTest(); System.assertEquals('Closed', opptest5.StageName); Opportunity opptest6 = new Opportunity(Name = 'Opp6', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today(), Secondary_Contact__c = [SELECT Id FROM Contact WHERE FirstName = 'test6'].id); Test.startTest(); insert opptest6; Test.stopTest(); System.assertEquals('Closed', opptest6.StageName); Opportunity opptest7 = new Opportunity(Name = 'Opp7', AccountId = newACC.id, StageName = 'Ready to Call/Schedule', CloseDate = Date.today()); Test.startTest(); insert opptest7; Test.stopTest(); System.assertEquals('Closed', opptest7.StageName); } }
Try the below one :
Thanks,
Maharajan.C
All Answers
Does the related opportunity with contact which you are inserting in test class meeting all those if conditions . Please cross check it once.
Thanks!
Hi,
Please try this below code.
Please mark it as the Best Answer if it helps you.
Thank You
Try the below one :
Thanks,
Maharajan.C