+ Start a Discussion
Chinna SfdcChinna Sfdc 

Test class for below trigger

Hi Team,

Can any one please help me to create test class for below trigger.

rigger Task on Opportunity (after insert,after update){    
    List<Opportunity> listOpp = Trigger.new;
    List<Task> listTask = new List<Task>();    
       if(Trigger.isInsert){
        for(Opportunity opp:listOpp){              
           if(opp.Help__c != null){
                Task t = new Task();
                t.OwnerId = opp.OwnerId;
                t.WhatId = opp.Id;
                t.Subject ='Help' + ': ' +opp.Help__c ; 
                t.ActivityDate = System.Today()+7;  
                t.Description = opp.Description__c;
                t.Status = 'In Progress';
                t.Priority = 'Normal';
                listTask.add(t);
            } 
        }
    }   

     if(Trigger.isUpdate){
         Map<Id,Opportunity> oldOppMap = Trigger.oldmap;
        for(Opportunity opp:listOpp){
            if(Opp.Help__c != oldOppMap.get(opp.Id).Help__c || Opp.Description__c!= oldOppMap.get(opp.Id).Description__c ){
            Task t = new Task();
            t.WhatId = opp.Id;   
            t.Subject = 'Help' + ': ' +opp.Help__c ; 
            t.Description = opp.Description__c;  
            t.ActivityDate = System.Today()+7;
            t.Status = 'In Progress';
            t.Priority = 'Normal';        
            listTask.add(t);
         }
        }   
     }  
     insert listTask;
 }

Thanks in Advance
Best Answer chosen by Chinna Sfdc
swati_sehrawatswati_sehrawat
Try this and let me know it it works:
 
@isTest
public class testOpportunity  {
    static testMethod void updateOpportunity () {
        
        Account acc = new Account();
        acc.Name = 'Test Account';
        
        insert acc;
        
        Opportunity opp = new Opportunity();
        opp.Name = 'TestOpportunity';
        opp.AccountId = acc.Id;
        opp.CloseDate = System.Today().addDays(10);
        opp.StageName = 'Negotiation';
        opp.help__c = 'Test';
              
        insert opp;
        
        opp.help__c = 'Test Again';
        update opp;
        
       
    } 
}

 

All Answers

swati_sehrawatswati_sehrawat
Try this and let me know it it works:
 
@isTest
public class testOpportunity  {
    static testMethod void updateOpportunity () {
        
        Account acc = new Account();
        acc.Name = 'Test Account';
        
        insert acc;
        
        Opportunity opp = new Opportunity();
        opp.Name = 'TestOpportunity';
        opp.AccountId = acc.Id;
        opp.CloseDate = System.Today().addDays(10);
        opp.StageName = 'Negotiation';
        opp.help__c = 'Test';
              
        insert opp;
        
        opp.help__c = 'Test Again';
        update opp;
        
       
    } 
}

 
This was selected as the best answer
Chinna SfdcChinna Sfdc
Hi swati,

Working Fine..Thanks for your help.

Thanks
swati_sehrawatswati_sehrawat
Great, Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution.