+ Start a Discussion
RossGilbertRossGilbert 

Test Coverage for After Insert Trigger

Hi-

 

Does anyone know how to write the test class for the following trigger?  Any code is much appreciated.  This is a simple trigger that spontaneously creates a new custom object record (called the Vehicle object) when a new Opportunity object record is created with a specific Type field value:

 

 

trigger createVehicleOnOpportunityX on Opportunity (after insert) {
    
List <Vehicle__c> vehToInsert = new List <Vehicle__c>  ();  

    
    for (Opportunity o : Trigger.new) {
        
        if (o.Type == 'NS-TO (New Sale - Task Order Contract)') {
        
        Vehicle__c v = new Vehicle__c (); 
        
        v.Name = o.Name;  
         v.Vehicle_HEGR__c= o.Historical_EGR__c;
          v.Vehicle_Amount__c = o.Amount;
           v.Vehicle_Recompete__c = o.Is_Recompete__c;
            v.Vehicle_Gross_Capacity__c = o.Gross_Capacity__c;
             v.Vehicle_Stage__c = o.StageName;


        
        vehToInsert.add(v);
        
        
        }//end if
        
    }//end for o
    


    try {
        insert vehToInsert; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
    
}

 

 

Ankit AroraAnkit Arora

Here is the code snippet, hope this will help you.

 

 

@isTest
private class MyTestClass
{
static testMethod void myUnitTest( )
{
Test.startTest();
Opportunity opp = new Opportunity();
opp.Name = 'TestOppFromTestClass' ;
opp.StageName = 'Test' ;
opp.CloseDate = Date.Today() ;
opp.Type = 'NS-TO (New Sale - Task Order Contract)' ;
insert opp ;
Test.stopTest() ;
}
}

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page