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
RahulRahul 

Test class for Opportunity

Please help me with the test class of following code. Iam getting only 10% coverage

trigger Opp on Opportunity (before insert,before update) {
    
    List<opportunity>  objvrList  =[select Id , stagename from opportunity where stagename='Pending' and id=:'0060k000004rrFb' ];
    boolean flagetoUpdate = false ; 
    for(Opportunity op :trigger.new){
        if(op.stagename  == 'pending')
        {
           flagetoUpdate = true ;   
        }
        
    }
    
    if(flagetoUpdate){
        for(opportunity o : objvrList) {
            o.stagename ='Lost' ; 
        }
        update objvrList ;
    }
    
}
v varaprasadv varaprasad
Hi Sumit,

Please check following sample code : 
@isTest
private class OpportunityTrigger_Test{
  static testMethod void test_OpportunityTrigger(){
   test.startTest();
    Opportunity opportunity_Obj = new Opportunity(IsPrivate = false, Name = 'Name913', StageName = 'pending', CloseDate = Date.today());
    Insert opportunity_Obj; 
     List<opportunity>  objvrList  =[select Id , stagename from opportunity where stagename='Pending' and id=: opportunity_Obj.id ];
      
    Opportunity opportunity_Obj1 = new Opportunity(IsPrivate = false, Name = 'Name913', StageName = 'pending', CloseDate = Date.today());
    Insert opportunity_Obj1; 
   test.stopTest();
  }
}


please remove following line in your trigger you will get 100% code coverage.

Hardcoding ids is not best practice and test class will not find this id.(and id=:'0060k000004rrFb')
To see organization data we need to use seealldata =True in the test class.


Hope this helps you.


Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

 
Raj VakatiRaj Vakati
trigger Opp on Opportunity (before insert,before update) {
    
    List<opportunity>  objvrList  =[select Id , stagename from opportunity where stagename='Prospecting' and id=:'0060k000004rrFb' ];
    boolean flagetoUpdate = false ; 
    for(Opportunity op :trigger.new){
        if(op.stagename  == 'Prospecting')
        {
            flagetoUpdate = true ;   
        }
        
    }
    
    if(flagetoUpdate){
        for(opportunity o : objvrList) {
            o.stagename ='Closed Lost' ; 
        }
        update objvrList ;
    }
    
}
 
@isTest
private class OpportunityTriggerTest {
    static testmethod void testcase(){
        Account acct = new Account(Name = 'tesAcctName'+ ((Integer) (Math.floor(Math.random() * (100)))));
        insert acct ;
        Contact con = new Contact(AccountId = acct.Id,email = 'test@gmail.com' , Lastname = 'LastName');
        insert con ;
        
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Oppty';
        opp.AccountId = acct.Id;
        opp.Amount = 1000;
        opp.CloseDate = date.today();
        opp.StageName = 'Prospecting';
        insert opp;
        opp.StageName = 'Closed Lost';
        update opp;
        
        
    }
    
}

 
RahulRahul
Hi Varaprasad and Raj thanks for your Answers. The actual code does not have an Id in the Query. I have Put there by mistake. Iam still not able to get the code coverage. Please Find below the code.Whenever Iam trying to insert or update a record its going to check if there are any old records created 90 days back with stage as pending and if so then its going to convert those stage as Loss.

trigger Opp on Opportunity (before insert,before update) {
    
    List<opportunity>  objvrList  =[select Id , stagename from opportunity where stagename='Pending' and createddate < LAST_90_DAYS ];
    boolean flagetoUpdate = false ; 
    for(Opportunity op :trigger.new){
        if(op.stagename  == 'pending')
        {
           flagetoUpdate = true ;   
        }
        
    }
    
    if(flagetoUpdate){
        for(opportunity o : objvrList) {
            o.stagename ='Loss' ; 
        }
        update objvrList ;
    }
    
}