• Waste Ventures Charities
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
I have a trigger which has test coverage upto 39% but while deploying it is giving error as my trigger coverage is only 0% . could anybody help me out.

my trigger is 

trigger employeedaysoffTrigger on SFDC_PTO_Request__c (after update,after insert) {
    Set<ID> Ids=new Set<Id>();
    Set<ID> bIds=new Set<Id>();
    Decimal d,e,f;
    List<SFDC_Employee__c> emp= new LIST<SFDC_Employee__c>();
    List<SFDC_PTO_Request__c> bbq=new LIST<SFDC_PTO_Request__c>();
    List<SFDC_PTO_Request__c> lq=new LIST<SFDC_PTO_Request__c>();
    //To avoid recursion using static boolean variable
    private static boolean run = true;
   if(TriggerContextUtility.isFirstRun())
    {
         run=false;
    //Take the ids of all newly inserting or updating records
    for(SFDC_PTO_Request__c b:Trigger.New)
    {
        if(b.Employee__c!=null)
        {
          Ids.add(b.Employee__c);
          bIds.add(b.Id);
        }
    }
  
    //list out all values from leave request
    lq= [SELECT id,employee__r.id,employee__r.total_casual_days_off__c,employee__r.total_sick_days_off__c,
         total_casual_leaves__c,total_sick_leaves__c,available_casual_leaves__c,available_sick_leaves__c,
         status__c,type_of_leave__c from SFDC_PTO_Request__c where id =:bIds];
    //Check the status of the leave request after record is approved/rejected
    if(lq[0].status__c == 'Approved')
    {
      
    for(AggregateResult ar:[select Employee__c em,sum(days_off__c) s,sum(casual_days_off__c) c,
                           sum(sick_days_off__c) dos from SFDC_PTO_Request__c
                           where Employee__c in : Ids Group BY Employee__c])
    {
    
        d= (Decimal) ar.get('s');
        e= (Decimal) ar.get('c');
        f= (Decimal) ar.get('dos');
    }
    //Update fields of total casual days off and total sick days off in employee object
    for(SFDC_Employee__c bac:[select id,total_days_off__c,total_casual_days_off__c,total_sick_days_off__c
                         from SFDC_Employee__c where id=:Ids])
    {
       if(lq[0].type_of_leave__c=='Casual Leave')
       {
           bac.total_casual_days_off__c=e;
           emp.add(bac);
          
       }
       else if(lq[0].type_of_leave__c=='Sick Leave')
       {
           bac.total_sick_days_off__c=f;
           emp.add(bac);
       }
    }
    //update available casual and sick leaves fields in leave request object
    for(SFDC_PTO_Request__c l: lq)
    {    
            if(l.type_of_leave__C=='Casual Leave')
            {   
                l.Available_Sick_Leaves__c=l.Total_Sick_Leaves__c-f;
                l.available_casual_leaves__c=l.total_casual_leaves__c-e;
               
                bbq.add(l);
            }
            else if(l.type_of_leave__c=='sick leave')
            {
                l.available_sick_leaves__c=l.total_sick_leaves__c-f;
                l.available_casual_leaves__c=l.total_casual_leaves__c-e;
                bbq.add(l);
            }
    }
    TriggerContextUtility.setFirstRunFalse();
    update bbq;
    update emp;
    }
    }
}

my test class for the above trigger is

@isTest
public class TestTrigger
{
    static testmethod void validateTrigger()
    {
         SFDC_PTO_Request__c spreq= new SFDC_PTO_Request__c(Employee__c='a0EK00000092v59',available_casual_leaves__c=20,available_sick_leaves__c=10);
         insert spreq;       
         spreq=[select id,Employee__c,available_casual_leaves__c,Available_sick_Leaves__c,status__c,type_of_leave__c from SFDC_PTO_Request__c where Employee__c='a0EK00000092v59'];
         System.assertEquals(20,spreq.Available_Casual_Leaves__c);
         System.assertEquals(10,spreq.Available_sick_Leaves__c);
         SFDC_Employee__c emp=new SFDC_Employee__c();
         emp.Total_casual_days_off__c=20;
         emp.Total_sick_days_off__c=10;
         emp.Pan_Number__c='bmplm8889m';
         insert emp;
         emp=[select id,total_days_off__c,total_casual_days_off__c,total_sick_days_off__c
                        from SFDC_Employee__c where id='a0EK00000092v59'];
         System.assertEquals(20,emp.Total_casual_days_off__c);
         System.assertEquals(10,emp.Total_sick_days_off__c);
        
    }
}