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
Joshua Graham 13Joshua Graham 13 

Trouble figuring out the right test code to cover enough of my trigger

Hi All - I am new to this forum and new to coding so I hope someone can help because I am racking my brains to figure out what I need to do to the test code to make it cover 75% or more. I modified a trigger that someone had previously written to add an if else statement and also code to run the case assignment rules which normally are not triggered with a trigger.  When testing the trigger it works well except for if the case isn't assigned to a person based on the assignment rules it is assigned to a queue.  I would rather it be assigned to the opportunity owner but I don't see that as an option in the assignment rules.  Is there a way to assign the cases that are created to the owner if it doesn't meet the assignment rules to assign to a different user?

I have the following trigger which will create a case and add some fields based on an if statement:

trigger CreateImplementationCaseOnWin on Opportunity (before update) {

AssignmentRule AR = new AssignmentRule();
AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];
Database.DMLOptions dmlOpts = new Database.DMLOptions();
dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;
List<Case> CaseList = new List<Case> ();

for(Opportunity o: Trigger.new) {
String opptyId = o.AccountId;
Opportunity oldopp = trigger.OldMap.get (o.id);
       if (o.isWon != oldopp.isWon)

if(o.isWon == true && o.Type == 'New Business' && o.HasOpportunityLineItem == true && o.StageName == '90 - Closed/Won' ){
  Case c = new Case();
  c.AccountId = o.AccountId;
  c.OwnerId = o.OwnerId;
  c.Subject = o.Name;
  c.RecordTypeId = o.Case_Creation_RT__c;
  c.Implementation_Type__c = o.Investor_Development_Required__c;
  c.OB_Signed_Contract_Closed_Won__c = o.CloseDate;
  c.setOptions(dmlOpts);
  CaseList.add(c);
}

else if(o.Type == 'Existing Business' && o.HasOpportunityLineItem == true && o.StageName == '08 - Contract Executed'){
  Case c = new Case();
  c.AccountId = o.AccountId;
  c.OwnerId = o.OwnerId;
  c.Subject = o.Name;
  c.RecordTypeId = o.Case_Creation_RT__c;
  c.Implementation_Type__c = o.Investor_Development_Required__c;
  c.OB_Signed_Contract_Closed_Won__c = o.CloseDate;
  c.setOptions(dmlOpts);
  CaseList.add(c);
}

if(CaseList.size() > 0)
insert(CaseList);
}
}

Before adding the else if statment and the Assignment Rule code, I had 100% coverage.  When adding the else if statment I go down to 62% and when adding the Assignment Rule code I am at 6% coverage.  I can't figure out what code I need to add to test and cover 75% or more.  Can someone please please point me in the right direction?  I have the test code below:

/**
* This class contains unit tests for validating the behavior of Apex classes
* and triggers.
*
* Unit tests are class methods that verify whether a particular piece
* of code is working properly. Unit test methods take no arguments,
* commit no data to the database, and are flagged with the testMethod
* keyword in the method definition.
*
* All test methods in an organization are executed whenever Apex code is deployed
* to a production organization to confirm correctness, ensure code
* coverage, and prevent regressions. All Apex classes are
* required to have at least 75% code coverage in order to be deployed
* to a production organization. In addition, all triggers must have some code coverage.
*
* The @isTest class annotation indicates this class only contains test
* methods. Classes defined with the @isTest annotation do not count against
* the organization size limit for all Apex scripts.
*
* See the Apex Language Reference for more information about Testing and Code Coverage.
*/
@isTest (SeeAllData=true)
private class Test_Class_CreateImplementationCaseOnWin {


    static testMethod void CreateImplementationCaseOnWin() {
  

               
        Account aa = new Account();
        aa.Name = 'Test Account';
        aa.Type = 'Customer';
        aa.BDM__c = 'Test User';
        aa.Time_Zone__c = 'Pacific';
        aa.Legacy_Solution__c = 'OB';
        insert aa;
       
       
        Opportunity oo = new Opportunity();   
        oo.AccountId = aa.Id;
        oo.Name = 'test';
        oo.Amount = 1000;
        oo.StageName = 'Prospecting';
        oo.CloseDate = date.today();
        insert oo;
       
                      
        //For production test class use 01u50000001F4l9
       
        OpportunityLineItem oll = new OpportunityLineItem();
        oll.OpportunityId = oo.Id;
        oll.Quantity = 1;
        oll.UnitPrice = 2.00;
        oll.PricebookEntryId = [SELECT Id FROM PricebookEntry WHERE isActive = true LIMIT 1].id;
       
        insert oll;
       
        oo.StageName= '90 - Closed/Won';
        update oo;
       
    //Verify that a case record was created
    for(Case cr : [SELECT Subject, Id FROM Case WHERE AccountId = :aa.Id LIMIT 1]){
    //System.assertEquals(cr.Subject = o.Name); 
       
        }
       
        delete oll;
        delete oo;
       
       
       
    }
   
   
}
pradeep naredlapradeep naredla
Hi Joshua,
     Nothing to worry u have inserted a record in the test class which satisfies the IF condition. You have to insert another record in which it has to satisfy the IF ELSE condition.

thanks.
Vinit_KumarVinit_Kumar
Add the below lines to your test class after you are updating Opportunity(i.e. update oo;) line :-

// Updating opportunity to satisfy Else if condition
oo.StageName='08 - Contract Executed';
oo.Type='Existing Business';
update oo;

If this helps,please mark it as best answer to help others :)