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
RaghuveerRaghuveer 

Average test coverage across all Apex Classes and Triggers is 72

hi,

 

i have created one Apex Trigger under Task section, that is working fine in sandbox account. when i am trying to deploy the same Trigger in to production, i am getting "Average test coverage across all Apex Classes and Triggers is 72%, at least 75% test coverage is required". i am using Eclipse for deployment.

 

my Requirement is upon clicking save buttion on new Task page, i need to fire an trigger to get last completed task comments to merge with the current comments.

 

 

Here is the my Trigger:

 

trigger Update_Comments_Task on Task (before insert) {

 

List<Id> whoIds = new List<Id>();

 

for(Task oTask : Trigger.new){

whoIds.add(oTask.WhoId);

}

 

//Get Existing Tasks

List<Task> exTask =[select Id,Description from Task where WhoId in :whoIds and status = 'Completed' Order by CreatedDate Desc];

 

for(Task oTask : Trigger.new){

if(exTask.Size() > 0){

for(Task tmpTask:exTask){

if(oTask.Description!=null && tmpTask.Description!=null){oTask.Description = tmpTask.Description +

'\n' + oTask.Description;

}

else if(oTask.Description==null && tmpTask.Description!=null){

oTask.Description = tmpTask.Description;

}

else if(oTask.Description!=null && tmpTask.Description==null){

oTask.Description = oTask.Description;

}

else{oTask.Description = '';

}

break;

}

}

 

}

}

 

 

the above Trigger is working fine in sandbox account.

Can you please help me, how can i improve my test  coverage percentage to 75%.

gv007gv007
Did you wrote an test coverage for Trigger not write a apex class(implement yours test coverage in that class).
RaghuveerRaghuveer

 

 

Thanks you for your reply. i am very new to the Apex development.

can you please help on this how can i write test coverage in apex class. 

XactiumBenXactiumBen

Create a new Apex Class and a test method inside this class -

 

e.g.

public class TaskTrigger { public TestMethod void testTaskTrigger() { Task t = new Task(); insert t; } }

Then inside the test method write code that will run your trigger.  Have a look at the Apex Documentation for more info - Link

RaghuveerRaghuveer

hi

 

Thank you for your consistant help.

 

i have created apex class as you suggested and included same Task t=new Task(); insert t; inside the TestMethod.

then save the apex class. after that i clicked the "Run all Tests" button and getting test coverage 66% for my Trigger.

 

can you please help me on this.

BDArnzBDArnz
Some of the branches in your trigger are not being tested by the test code.  Eclipse will tell you which lines in the trigger are not being tested.  You will need to create and insert mulitple records within your test code in order to ensure that each branch of the trigger gets tested.  You're not seeing any problems in sandbox since there aren't any testing requirements there...
khanWebgurukhanWebguru

I have problem regarding Package deployment in sales-force :(  Its very strange that I have just 4 triggers and have not any single Apex Class so what I have to do for deployment I need TEST Methods for my Triggers. I created test methods for my all four triggers when I am going to upload option of package I am getting following ERROR :(

 



Average test coverage across all Apex Classes and Triggers is 1%, at least 75% test coverage is required

 

Thats very strange because I have not Apex class even that I am getting this problem please help me in this problem :(

 

My Triggers are mentioned below:

 

 

Trigger 1:

 


trigger UpdateHiringManagerFieldAfterInsert on Job_Application__c (after insert) 
{
    LIST<Job_Application__c> objJobApp = [select Position__c From Job_Application__c where Id =  :Trigger.new[0].Id limit 1];
    LIST<Position__c> objPos = [select Hiring_Manager__c,Name From Position__c where ID =  :objJobApp[0].Position__c limit 1];
    objJobApp[0].HiringManager__c = objPos[0].Hiring_Manager__c;
    update objJobApp[0];

 

Trigger 2:

 

trigger insertPosition on Recruitment_Request__c (after update) 
{
    if(Trigger.new[0].Approved__c != Trigger.old[0].Approved__c && Trigger.new[0].Approved__c == True)
    {
        Recruitment_Request__c rrc = [select PositionId__c From Recruitment_Request__c where Id =  :Trigger.new[0].Id limit 1];
        if(rrc.PositionId__c == '' || rrc.PositionId__c == Null)
        {
            Position__c position = new Position__c();
            position.Name = Trigger.new[0].Name;
            position.Hiring_Manager__c = Trigger.new[0].CreatedById;
            
            insert position;
            rrc.PositionId__c = position.Id;
            update rrc;
        }
    }
}

 

 

Trigger 3:

 

trigger onInsertAndUpdateInterview on Interview__c (after insert, after update) 
{
    if(Trigger.new[0].JobApplicationId__c == '' || Trigger.new[0].JobApplicationId__c == Null)    
    {
        LIST<Interview__c> objInterview = [select JobApplicationId__c,OwnerUser__c From Interview__c where Candidate__c =  :Trigger.new[0].Candidate__c AND Job_Position__c =  :Trigger.new[0].Job_Position__c AND JobApplicationId__c != ''  limit 1];
        
        LIST<Interview__c> objInterview1 = [select JobApplicationId__c,OwnerUser__c From Interview__c where ID =  :Trigger.new[0].ID  limit 1];
        
        objInterview1[0].JobApplicationId__c = objInterview[0].JobApplicationId__c;
        objInterview1[0].Job_Application__c = objInterview[0].Job_Application__c;
        objInterview1[0].OwnerUser__c = objInterview[0].OwnerUser__c;
        objInterview1[0].OwnerId=objInterview[0].OwnerUser__c;
        
        
        update objInterview1[0];
        
        
    }
    if(Trigger.new[0].Create_Another_Interview__c == True && Trigger.new[0].InterviewType__c == 'Technical')
    {     
        
        Interview__c interview = new Interview__c();
        interview.Candidate__c = Trigger.new[0].Candidate__c;
        interview.Job_Position__c = Trigger.new[0].Job_Position__c;
        interview.JobApplicationId__c = Trigger.new[0].JobApplicationId__c;
        interview.Job_Application__c = Trigger.new[0].Job_Application__c;
        interview.OwnerUser__c = Trigger.new[0].OwnerUser__c;
        interview.InterviewType__c = Trigger.new[0].InterviewType__c;
        interview.OwnerId = Trigger.new[0].OwnerUser__c;
        interview.HiringManager__c = Trigger.new[0].HiringManager__c;
        interview.IsCreatedByCloneOption__c = True;
        interview.Date__c = date.today();
        insert interview;
        
        LIST<Interview__c> objInterview1 = [select JobApplicationId__c,OwnerUser__c From Interview__c where ID =  :Trigger.new[0].ID  limit 1];
        objInterview1[0].Create_Another_Interview__c  = false;
        update objInterview1[0];
        
     
        
    }
    if(Trigger.new[0].Create_Another_Interview__c == True && Trigger.new[0].InterviewType__c == 'HR')
    {     
        
        Interview__c interview = new Interview__c();
        interview.Candidate__c = Trigger.new[0].Candidate__c;
        interview.Job_Position__c = Trigger.new[0].Job_Position__c;
        interview.JobApplicationId__c = Trigger.new[0].JobApplicationId__c;
        interview.OwnerUser__c = Trigger.new[0].OwnerUser__c;
        interview.InterviewType__c = Trigger.new[0].InterviewType__c;
        interview.OwnerId = Trigger.new[0].OwnerUser__c;
        interview.HiringManager__c = Trigger.new[0].HiringManager__c;
        interview.IsCreatedByCloneOption__c = True;
        interview.Date__c = date.today();
        insert interview;
        
        LIST<Interview__c> objInterview1 = [select JobApplicationId__c,OwnerUser__c From Interview__c where ID =  :Trigger.new[0].ID  limit 1];
        objInterview1[0].Create_Another_Interview__c  = false;
        update objInterview1[0];
        
        
     
    }
}

 

 

Trigger 4:

 

trigger sendApprovalRequestToManager on Job_Application__c (after update) 
{
   
    if(Trigger.new[0].Picklist__c != Trigger.old[0].Picklist__c && Trigger.new[0].Picklist__c == 'Short Listed')
    {
        
  
        LIST<Job_Application__c> objJobApp1 = [select Position__c,CreatedById From Job_Application__c where Id =  :Trigger.new[0].Id limit 1];
        
        Interview__c interview = new Interview__c();
        interview.Candidate__c = Trigger.new[0].Candidate__c;
        interview.Job_Position__c = Trigger.new[0].Position__c;
        interview.JobApplicationId__c = Trigger.new[0].Id;
        interview.Job_Application__c = Trigger.new[0].Id;
        interview.InterviewType__c = 'Technical';
        interview.OwnerUser__c = objJobApp1[0].CreatedById;
        interview.HiringManager__c = Trigger.new[0].HiringManager__c;
        interview.OwnerId=objJobApp1[0].CreatedById;        
        interview.Date__c = date.today();
        
        
        
        
        
        
     
    }
    if(Trigger.new[0].Picklist__c != Trigger.old[0].Picklist__c && Trigger.new[0].Picklist__c == 'Recommended')
    {
                
   
        LIST<Job_Application__c> objJobApp2 = [select Position__c,CreatedById From Job_Application__c where Id =  :Trigger.new[0].Id limit 1];
        
        Interview__c interview1 = new Interview__c();
        interview1.Candidate__c = Trigger.new[0].Candidate__c;
        interview1.Job_Position__c = Trigger.new[0].Position__c;
        interview1.JobApplicationId__c = Trigger.new[0].Id;
        interview1.Job_Application__c = Trigger.new[0].Id;
        interview1.InterviewType__c = 'HR';
        interview1.OwnerUser__c = objJobApp2[0].CreatedById;
        interview1.HiringManager__c = Trigger.new[0].HiringManager__c;
        interview1.OwnerId=objJobApp2[0].CreatedById;
        interview1.Date__c = date.today();
        insert interview1;
  
    }

}

 


 

Please help me in this

 

Thanks,

 

Asif Khan

 

 

 

 

 


khanWebgurukhanWebguru

Now I created these test Cases against above mentioned Triggers :(

 

public class PalmRecruitingTesting
{
    static testMethod void UpdateHiringManagerFieldAfterInsert()
    {                           
       Job_Application__c obj = new Job_Application__c();        
        LIST<Job_Application__c> objJobApp = [SELECT HiringManager__c, Position__c,Candidate__c FROM Job_Application__c limit 1];        
        LIST<Position__c> objPos = [select Hiring_Manager__c,Name From Position__c where ID =  :objJobApp[0].Position__c limit 1];
        objJobApp[0].HiringManager__c = objPos[0].Hiring_Manager__c;
        obj.HiringManager__c = objJobApp[0].HiringManager__c;
        obj.Position__c = objJobApp[0].Position__c;
        obj.Candidate__c = objJobApp[0].Candidate__c;
        
        test.startTest();                
        insert obj;
           
        
        obj.Cover_Letter__c = 'Test';
        
        
        update obj;
        System.assertEquals(obj.Cover_Letter__c,'Test');
         LIST<Job_Application__c> objJobApp1 = [SELECT Cover_Letter__c,HiringManager__c, Position__c,Candidate__c FROM Job_Application__c];
       Integer j=0;
        for (Job_Application__c objJAP :objJobApp1) 
        {
        
            ++j;
            objJAP.Cover_Letter__c = string.valueof(j);            
            System.assertEquals(objJAP.Cover_Letter__c,string.valueof(j));
            
        
        }
        update objJobApp1;
        
        test.stopTest();
        
        
    }

    static testMethod void onInsertAndUpdateInterview()
    {                                   
        Interview__c c = [SELECT Id FROM Interview__c limit 1];        
        
        test.startTest();
        update c;
        
        List<Interview__c> c1 = [SELECT Id, Final_Comments__c FROM Interview__c];
        Integer j=0;
        for (Interview__c c11 :c1) 
        {
            ++j;
            c11.Final_Comments__c = string.valueof(j);                        
            System.assertEquals(c11.Final_Comments__c,string.valueof(j));
            
        
        }
        update c1;
        test.stopTest();   
    }
    
    static testMethod void insertPosition()
    {                                   
        Recruitment_Request__c c = [SELECT Id FROM Recruitment_Request__c limit 1];        
        
        test.startTest();
        update c;
        
        
        List<Recruitment_Request__c> c1 = [SELECT Id,Comments__c FROM Recruitment_Request__c];
        Integer j=0;
        for (Recruitment_Request__c c11 :c1) 
        {
            ++j;
            c11.Comments__c = string.valueof(j);                        
            System.assertEquals(c11.Comments__c,string.valueof(j));
            
        
        }
        update c1;
        test.stopTest();   
    }
    
    static testMethod void sendApprovalRequestToManager()
    {                                   
        //Interview__c c = [SELECT Id FROM Interview__c limit 1];        
        
        LIST<Position__c> objPos1 = [select Id, Name From Position__c limit 1];

       LIST<Candidate__c> objCand1 = [select Id, First_Name__c,Last_Name__c From Candidate__c limit 1];
       
       LIST<Job_Application__c> objJobApp2 = [select Id, Position__c,CreatedById,HiringManager__c From Job_Application__c limit 1];
       
       
        Interview__c interview1 = new Interview__c();
        interview1.Candidate__c = objCand1[0].Id;
        interview1.Job_Position__c = objPos1[0].Id;
        interview1.JobApplicationId__c = objJobApp2[0].Id;
        interview1.Job_Application__c = objJobApp2[0].Id;
        interview1.InterviewType__c = 'HR';
        interview1.OwnerUser__c = objJobApp2[0].CreatedById;
        interview1.HiringManager__c = objJobApp2[0].HiringManager__c;
        interview1.OwnerId=objJobApp2[0].CreatedById;
        interview1.Date__c = date.today();
        
        test.startTest();
        insert interview1;
        
        
        
        List<Interview__c> c1 = [SELECT Id, Final_Comments__c FROM Interview__c];
        Integer j=0;
        for (Interview__c c11 :c1) 
        {
            ++j;
            c11.Final_Comments__c = string.valueof(j);                        
            System.assertEquals(c11.Final_Comments__c,string.valueof(j));
            
        
        }
        update c1;
        test.stopTest();    
    }

}

 

 

But on uploading of package I am getting this error

 


Average test coverage across all Apex Classes and Triggers is 1%, at least 75% test coverage is required

 

Please help me in this

 

Thanks,

 

Asif Khan

KalluruKalluru

Hi I am trying to deploy one apex code in production from sanbox,in sanbox it showing as 88% coverage in production I am getting fallowing error.

 

Average test coverage across all Apex Classes and Triggers is 68%, at least 75% test coverage is required.

 

But not telling me which class is not covering can some please help me solving this issue 

rgilrgil

Kalluru i am having the same issue.

 

I have over 80% code coverage when i run all tests in both the sandbox and in produciton.

 

My new triggers both have coverage over 75% each but when i deploy it i get an error that i have only 69% test coverage. 

 

Did you find a solution on this?