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
Wes McCarthyWes McCarthy 

Help with Test Class Coverage

Hi there,

Im writing a test class to cover a task trigger.  I have 73% code coverage currently.  The lines of code that i havent written a Test for are below, as im unsure how to write Tests for this.  Im very new to APEX.  Any help is much appreciated.  Many thanks.

 List<Id> OppIds=new List<Id>();
    for(Task t:trigger.new)
    {
        if(t.Status=='Completed')
        {
            if(t.whatId != null && String.valueOf(t.whatId).startsWith('006')==TRUE) //check if the task is associated with an Opp
            {
                OppIds.add(t.whatId);
                //System.debug('In here:' + t.whatid);
            }//if 2
        }//if 1
    }//for
        
    map<id,Opportunity> OpptaskMap = new map<id,Opportunity>([SELECT Id, Situation__c, Forecast_Delivery_Start_Date__c, Forecast_Delivery_End_Date__c, Bypass_Validation_Rule__c, Problem__c, Implication__c, Need_Payoff__c , Involves_Active_Partners__c, Google_Drive_URL__c, StageName, (Select ID From OpportunityLineItems) FROM Opportunity WHERE Id IN :OppIds]);
    
    
    List<OpportunityContactRole> OCR = [select id from OpportunityContactRole where OpportunityId IN :oppIds];
    List<OpportunityPartner> OPartner = [select id from OpportunityPartner where OpportunityId in :oppIds];
Ricardo_DiasRicardo_Dias

Hello Wes MacCarthy,

To do your test class you just need to do a code that run through all lines. 

Remember that the below code is a simple and if you have more conditions to create Account, Opportunity or Task, you need to adapt the code.

@isTest 
private class TriggerTaskTest {

    static testMethod void methodValidInsertOperation() {

       //First Insert the Account 
       Account accountObj = new Account(Name='Example Account');
       insert accountObj;

       //Insert the Opportunity
       Opportunity oppObj = new Opportunity(Name = 'Opp Test', AccountId = accountObj.id, StageName = 'Qualification', CloseDate = System.today());
        insert oppObj;

        //Insert the TASK
        Task taskOBJ = new Task(WhatId = oppObj.id, Subject = 'Other', status = 'Completed', description = 'New  Work');
         insert taskOBJ;
    }


}

 

Let me know about the final solution !

Greeting ! 

Wes McCarthyWes McCarthy

Thanks Ricardo.
 

I understand the inserting of objects etc.  I have that part of my code covered.  The code im unsure about is the code that will cover the beginning of my trigger i.e.


 List<Id> OppIds=new List<Id>();
    for(Task t:trigger.new)
    {
        if(t.Status=='Completed')
        {
            if(t.whatId != null && String.valueOf(t.whatId).startsWith('006')==TRUE) //check if the task is associated with an Opp
            {
                OppIds.add(t.whatId);
                //System.debug('In here:' + t.whatid);
            }//if 2
        }//if 1
    }//for
        
    map<id,Opportunity> OpptaskMap = new map<id,Opportunity>([SELECT Id, Situation__c, Forecast_Delivery_Start_Date__c, Forecast_Delivery_End_Date__c, Bypass_Validation_Rule__c, Problem__c, Implication__c, Need_Payoff__c , Involves_Active_Partners__c, Google_Drive_URL__c, StageName, (Select ID From OpportunityLineItems) FROM Opportunity WHERE Id IN :OppIds]);
    
    
    List<OpportunityContactRole> OCR = [select id from OpportunityContactRole where OpportunityId IN :oppIds];
    List<OpportunityPartner> OPartner = [select id from OpportunityPartner where OpportunityId in :oppIds];

Addison MelissaAddison Melissa
Our seasoned authors, researchers and groups of professional are here to serve all your academic writing needs. At Weblink Bestassignmentservice (http://www.bestassignmentservice.com/pay-to-have-a-paper-written), there is no writing task we can’t handle.
Ricardo_DiasRicardo_Dias
Hi Wes,

What part exactly do you not have sure ?