function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shantanu Punekar 5Shantanu Punekar 5 

Test class on Trigger to create Task on Opportunity update.

Hello all,

I have created below trigger and a test class for it, still its not covering the trigger. What needs to be changed in this test class?
Please help

Trigger:
trigger OppTask on Opportunity (after update) {
    List <Contact> conList = [SELECT ID,AccountID FROM Contact WHERE AccountId IN (SELECT AccountId FROM Opportunity WHERE Id IN :Trigger.new)];
    map<ID,Contact> accountIDWithContactMap = new map<ID,Contact>();
    for(Contact con : conList){
        accountIDWithContactMap.put(con.AccountID, con);
    }    
    for(Opportunity Opp : Trigger.new){
        if(Trigger.newMap.get(opp.Id).StageName != Trigger.oldMap.get(opp.Id).StageName && opp.StageName == 'Closed Won' && accountIDWithContactMap.containsKey(Opp.AccountId)){
            Task T = new Task();
            T.WhatId = opp.Id;            
            T.OwnerId = opp.OwnerId;
            T.Subject = 'Call';
            T.Status = 'In Progress';
            T.IsReminderSet = TRUE;
            T.WhoId = accountIDWithContactMap.get(Opp.AccountId).ID;
            T.ReminderDateTime = DateTime.now().addHours(24);
            
            insert T;
    
        }
        
        
    }



Test Class:
@isTest

public class testOppTaskTrigger{
    static testMethod void ValidateOppTaskTrigger(){
    Test.startTest();
    Account testAcc = new Account (Name = 'Test Account');
    insert testAcc;
    
    User U = [SELECT ID, Name FROM User WHERE Profile.Name = 'System Administrator'];
    
    Opportunity testOpp = new Opportunity (Name = 'Test Name',
                                     OwnerId =U.Id,
                                     AccountId = testAcc.Id,
                                     StageName = 'Open',
                                     Amount = 50000.00,
                                     CloseDate = System.today()
                                     );
    
    insert testOpp;
    
    Opportunity testOpp2 = [SELECT Name, Id, StageName FROM Opportunity WHERE Id =: testOpp.Id];
                            if(testOpp2.StageName == 'Closed Won') {
                            update testOpp;
    
    Task testTask = new Task ( WhatId = testOpp2.Id,            
            OwnerId = testOpp2.OwnerId,
            Subject = 'Call',
            Status = 'In Progress',
            IsReminderSet = TRUE,
            ReminderDateTime = DateTime.now().addHours(24));                                
     
     
     insert testTask;
     Test.stopTest();
    }
    }

}
 
Shrikant BagalShrikant Bagal
@Shantanu, you need to create one contact belong to "Test Name" account in test Methods, because In your trigger code you are applying  "accountIDWithContactMap.containsKey(Opp.AccountId)" condition which will false through text method.
 
ManojjenaManojjena
HI Shantanu,

Try with below code it will give you 100% code coverage ,
 
@isTest
public class testOppTaskTrigger{
    static testMethod void ValidateOppTaskTrigger(){
        Account testAcc = new Account ();
            testAcc.Name = 'Test Account';
            insert testAcc;
			testAcc=[SELECT id,Name FROM Opportunity WHERE Id =:testAcc.Id];
		  System.assertEquals(testAcc.Name,'Test Account');
        Contact con=new Contact();
           con.lastname='TestCon';
           con.Email='test@gmail.com';
           con.AccountId=testAcc.Id;
           Insert con;
        con=[SELECT id,Name FROM Opportunity WHERE Id =:con.Id];
		  System.assertEquals(con.Name,'TestCon');
        Opportunity testOpp = new Opportunity ();
            testOpp.Name = 'Test Opp';
            testOpp.AccountId = testAcc.Id;
            testOpp.StageName = 'Open';
            testOpp.Amount = 50000.00;
            testOpp.CloseDate = System.today();
          insert testOpp;
		  testOpp=[SELECT id,Name FROM Opportunity WHERE Id =:testOpp.Id];
		  System.assertEquals(testOpp.Name,'Test Opp');
           testOpp.StageName = 'Closed Won';
		   Test.startTest();
				update testOpp;
		   Test.stopTest();
    }
}

Let me know if it helps .

Thanks 
Manoj

 
Shantanu Punekar 5Shantanu Punekar 5
Thank you Manoj, Shrikant. I'll try this.
Shantanu Punekar 5Shantanu Punekar 5
Hi Manoj,
Why would we need the soql on lines 7, 14 and 23 which is selecting Id from opp? Obviously it will give an error while compiling. And why do we need to create a Contact record for this?
ManojjenaManojjena
Hi Shantanu,

Have tried with the code ? It is best practice to check with Either System.assert/assertEquals/assertNotEquals to check the record is inserted or not .

I think will not give you compile time or run time error .

To know more about the test class please check below link it has a lot of links which will give you a lot of infromation about test class .

Let me if it help or still you have doubt .

Thanks 
Manoj
Shrikant BagalShrikant Bagal
if its resolved your issue,please mark as best answer so it will help to other who will serve same problem.
​Thanks!