+ Start a Discussion
KB KimKB Kim 

Need help for test code for OLI trigger

This code auto-creates OLI when a user create a new opportunity and selet a product name from the custome picklist (product__C).

I need to deploy this to our production instance, but I don't know how to write test code for this..

Please, help me.. I need to deploy this as soon as possible.


trigger AutoCreateProduct on Opportunity (after insert) { 
    List<OpportunityLineItem> OpportunityLineItems = new List<OpportunityLineItem>(); 
    for (Opportunity newOpportunity: Trigger.New) { 
        if(newOpportunity.product__C.equalsIgnoreCase('CRA')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001a8iVAAQ',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        } if (newOpportunity.product__C.equalsIgnoreCase('GALV')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001a8iQAAQ',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }
          if (newOpportunity.product__C.equalsIgnoreCase('2TFS')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBo7AAE',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }
if (newOpportunity.product__C.equalsIgnoreCase('DRTP')){
            OpportunityLineItems.add(new OpportunityLineItem(OpportunityId = newOpportunity.Id,PricebookEntryId ='01u61000001aBoRAAU',Quantity = newopportunity.volume__C,UnitPrice = newopportunity.Sales_Price__C)); 
        }
    } 
    if(!OpportunityLineItems.isEmpty()){
        Database.SaveResult[] srList = Database.insert(OpportunityLineItems, false);

        // Iterate through each returned result
        for (Database.SaveResult sr : srList) {
            if (sr.isSuccess()) {
                // Operation was successful, so get the ID of the record that was processed
                System.debug('Successfully inserted Line Item. Oppty Prodcut ID: ' + sr.getId());
            }
            else {
                // Operation failed, so get all errors                
                for(Database.Error err : sr.getErrors()) {
                    System.debug('The following error has occurred.');                    
                    System.debug(err.getStatusCode() + ': ' + err.getMessage());
                    System.debug('Opportunity Product fields that affected this error: ' + err.getFields());
                }
            }
        }
    }



Test code that has only 58% coverage.


@IsTest (seealldata=true)
public class AutoCreateProduct{
    public static testmethod void myunittest(){
               
       //create a dummy account
        Account a = new Account ();
        a.Name = 'Test Asset Account';
        insert a;
        
        //create a dummy opportunity
        Opportunity o = new Opportunity();
        o.AccountId = a.Id;
        o.Name = 'test Oppty';
        o.StageName = 'opportunity';
        o.CloseDate = system.today();
        o.product__c = 'cra';
        o.volume__c = 10;
                insert o;
  
    }
}
 
KunalSharmaKunalSharma
@IsTest (seealldata=true)
public class AutoCreateProduct{
    public static testmethod void myunittest(){
               
       //create a dummy account
        Account a = new Account ();
        a.Name = 'Test Asset Account';
        insert a;
        
        //create a dummy opportunity
        List<Opportunity> lOppty = new List<Opportunity>();
        Opportunity o = new Opportunity();
        o.AccountId = a.Id;
        o.Name = 'test Oppty';
        o.StageName = 'opportunity';
        o.CloseDate = system.today();
        o.product__c = 'cra';
        o.volume__c = 10;
        lOppty.add(o);
        
        Opportunity o1 = new Opportunity();
        o1.AccountId = a.Id;
        o1.Name = 'test Oppty1';
        o1.StageName = 'opportunity';
        o1.CloseDate = system.today();
        o1.product__c = 'GALV';
        o1.volume__c = 10;
        lOppty.add(o1);
        
        Opportunity o2 = new Opportunity();
        o2.AccountId = a.Id;
        o2.Name = 'test Oppty2';
        o2.StageName = 'opportunity';
        o2.CloseDate = system.today();
        o2.product__c = '2TFS';
        o2.volume__c = 10;
        lOppty.add(o2);        
     
       Opportunity o3 = new Opportunity();
        o3.AccountId = a.Id;
        o3.Name = 'test Oppty3';
        o3.StageName = 'opportunity';
        o3.CloseDate = system.today();
        o3.product__c = 'DRTP';
        o3.volume__c = 10;
        lOppty.add(o3);
        
       insert lOppty;

  
    }
}
Please use the above and see if it works for you. Basically you will have to cover all the 'if' conditions present in your code.