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
sudhirn@merunetworks.comsudhirn@merunetworks.com 

code coverage for opportunity trigger

Hi,

 I wrote a trigger on opportunity trigger which will update the custom object which is working fine 
 
trigger Insert_Order_checklist on Opportunity (After Insert, After Update) 
{
/* This trigger is used to insert into order checklist when opportunity stage name = 1 - closed won */

   list<Opportunity> OP = [select id,name from opportunity where id = :Trigger.newMap.keySet() and StageName = '1 - Closed Won' limit 1];
   List<Order_Processing_Checklist__c> OPCi = new list<Order_Processing_Checklist__c>();
   
  // Integer CL = [select count()  from opportunity where id = :Trigger.newMap.keySet() and StageName = '1 - Closed Won'];

  if ( [select count()  from opportunity where id = :Trigger.newMap.keySet() and StageName = '1 - Closed Won'] > 0 ) 
    { 
    if ( [select count()  from Order_Processing_Checklist__c where Opportunity_Name__c = :Trigger.newMap.keySet() ] == 0 ) 
    { 
    
     for ( opportunity opp : op ) 
      {
        
       //list<Order_Processing_Checklist__c> delOP = [select id from Order_Processing_Checklist__c where Opportunity_Name__c = :opp.id ];
       
       //Delete delOp;
        
      Order_Processing_Checklist__c OPC = new Order_Processing_Checklist__c();
        
         OPC.Opportunity_Name__c = opp.id;
         OPC.name = opp.name;
         OPCi.add(OPC);      
        }             
        
      Insert OPCi;
      }
   }


}
I also wrote a test class for above trigger it is not giving code coverage please suggest me what is the mistake in this test class
 
@isTest(SeeAllData = true)
private class Test_Insert_Order_checklist {


public static testmethod void testorderchecklist()
{
  
 test.startTest();
  
 list<Opportunity> OP = [select id,name from opportunity 
                         where
                               StageName = '1 - Closed Won' limit 1];
  
 List<Order_Processing_Checklist__c> OPCi = new list<Order_Processing_Checklist__c>();
  
 for ( opportunity opp : op ) 
 {
       Order_Processing_Checklist__c OPC = new Order_Processing_Checklist__c();
        
         OPC.Opportunity_Name__c = opp.id;
         OPC.name = opp.name;
         OPCi.add(OPC);     
         
        
    
    
    }     
 Insert OPCi;
 
test.stopTest(); 
}


}

Thanks
Sudhir
 
Best Answer chosen by sudhirn@merunetworks.com
Chandra Sekhar CH N VChandra Sekhar CH N V
what I meant was you need to insert test records in the testmethod , insert them and then run the test. There is no need to seeAllData annotation as your data varies from sandbox to production and might fail.
 
@isTest 
private class <your test class name>{
    static testMethod void <your method>() {
       Opportunity opp = new Book__c(name='test' ,StageName = '1 - Closed Won',closedate = System.today());
      
       insert opp;
    
        // Insert your order processing reocrd with relation to above opportunity record

    }
}

 

All Answers

Chandra Sekhar CH N VChandra Sekhar CH N V
If its displaying 0% coverage try running the test class from standard UI (setup-->develop--> apex test execution)
sudhirn@merunetworks.comsudhirn@merunetworks.com
Problem is not regarding the execution code coverage is 0% for the test class which I created need a suggest what is the issue in the code. 

Thanks
Sudhir
Chandra Sekhar CH N VChandra Sekhar CH N V
Remove seeAllData annotation.Should work. 
sudhirn@merunetworks.comsudhirn@merunetworks.com
Tried no change its same. 
Chandra Sekhar CH N VChandra Sekhar CH N V
what I meant was you need to insert test records in the testmethod , insert them and then run the test. There is no need to seeAllData annotation as your data varies from sandbox to production and might fail.
 
@isTest 
private class <your test class name>{
    static testMethod void <your method>() {
       Opportunity opp = new Book__c(name='test' ,StageName = '1 - Closed Won',closedate = System.today());
      
       insert opp;
    
        // Insert your order processing reocrd with relation to above opportunity record

    }
}

 
This was selected as the best answer