• alltvsong gaane
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hello, 

I have a Apex class that select records where a field > 0. It then loops through these leads and creates a record in a custom object (Metrics__c). Based on the date it creates a record with a 0 value or with a value > 0. The class works great (tested in Dev Console as a batch) but I'm going in cirlces trying to get the test class past 61%. Any help would be appreciated!

Apex Class

global class MetricProcessor implements Database.Batchable <SObject>,Schedulable {
//START METHOD
global Database.QueryLocator start(Database.BatchableContext bc){

    
    String Query= 'select Id, Console_Logins__c, Console_Last_Logins_Date_Field__c, EP_Catalog__c, EP_Catalog_Date_Field__c, EP_Designer__c, EP_Designer_Date_Field__c, EP_Discovery__c,EP_Discovery_Date_Field__c From Lead where Console_Logins__c > 0';

    return Database.getQueryLocator(Query);

        }

//EXECUTE METHOD

global void execute(Database.BatchableContext bc, List<Lead> scope){

    for(Lead l: scope){

        IF (l.Console_Last_Logins_Date_Field__c > (system.today() - 1))
   {
        //insert record
         
    Metrics__c met = new Metrics__c(
    
         Lead__c=l.ID,

        Name='ConsoleLogins',

        Metric_Value__c=l.Console_Logins__c,

        Metric_Date__c= system.today());

       
    insert met;

    }
    Else
       {//insert 0 record
         Metrics__c met = new Metrics__c(
    
         Lead__c=l.ID,

        Name='ConsoleLogins',

        Metric_Value__c= 0,

        Metric_Date__c= system.today());
        
         insert met;
    }
            IF (l.EP_Catalog_Date_Field__c > (system.today() - 1))
   {
        //insert record
         
    Metrics__c met = new Metrics__c(
    
         Lead__c=l.ID,

        Name='EPCatalog',

        Metric_Value__c=l.EP_Catalog__c,

        Metric_Date__c= system.today());

       
    insert met;

    }
    Else
       {//insert 0 record
         Metrics__c met = new Metrics__c(
    
         Lead__c=l.ID,

        Name='EPCatalog',

        Metric_Value__c= 0,

        Metric_Date__c= system.today());
        
         insert met;
    }
            IF (l.EP_Designer_Date_Field__c > (system.today() - 1))
   {
        //insert record
         
    Metrics__c met = new Metrics__c(
    
         Lead__c=l.ID,

        Name='EPDesigner',

        Metric_Value__c=l.EP_Designer__c,

        Metric_Date__c= system.today());

       
    insert met;

    }
    Else
       {//insert 0 record
         Metrics__c met = new Metrics__c(
    
         Lead__c=l.ID,

        Name='EPDesigner',

        Metric_Value__c= 0,

        Metric_Date__c= system.today());
        
         insert met;
    }
            IF (l.EP_Discovery_Date_Field__c > (system.today() - 1))
   {
        //insert record
         
    Metrics__c met = new Metrics__c(
    
         Lead__c=l.ID,

        Name='EPDiscovery',

        Metric_Value__c=l.EP_Discovery__c,

        Metric_Date__c= system.today());

       
    insert met;

    }
    Else
       {//insert 0 record
         Metrics__c met = new Metrics__c(
    
         Lead__c=l.ID,

        Name='EPDiscovery',

        Metric_Value__c= 0,

        Metric_Date__c= system.today());
        
         insert met;
    }
    }

    update scope;

}


   

//FINISH METHOD

global void finish(Database.BatchableContext bc){

    Id job= bc.getJobId();

    System.debug(job);

}

 

 global void execute(SchedulableContext SC) {

 MetricProcessor l= new MetricProcessor();

ID batchprocessid = Database.executeBatch(l);

}

}

Test Class

@istest
private class MetricProcessorTest {
    @istest

    static void tetslead(){
        List<Lead> l= new List<Lead>();
        lead l1= new Lead();
        l1.LastName='Chaytor';
        l1.Company='Solace';
        l1.Status='Working';
        l1.Console_Logins_Last_Modified__c = system.now();
        l1.Console_Logins__c = 2;
        l1.EP_Catalog_Last_Modified__c = system.now()-2;
        l1.EP_Catalog__c = 0;
        l1.EP_Designer_Last_Modified__c = system.now()-2;
        l1.EP_Designer__c = 0;
        l1.EP_Discovery_Last_Modified__c = system.now();
        l1.EP_Discovery__c = 1;
        l1.LeadSource='Dreamforce';
        l.add(l1);
        insert l;
        
      List<Metrics__c> m= new List<Metrics__c>();
        Metrics__c m1= new Metrics__c();
        m1.Lead__c = l1.Id;
        m1.Name = 'ConsoleLogins';
        m1.Metric_Value__c = 2;
       // m1.Metric_Date__c = system.today();
        
        m.add(m1);
        insert m;
        
        List<Metrics__c> m2= new List<Metrics__c>();
        Metrics__c m3= new Metrics__c();
        m3.Lead__c = l1.Id;
        m3.Name = 'EPCatalog';
        m3.Metric_Value__c = 0;
        //m3.Metric_Date__c = system.today()-;
        
        m2.add(m3);
        insert m2;
        
        List<Metrics__c> m4= new List<Metrics__c>();
        Metrics__c m5= new Metrics__c();
        m5.Lead__c = l1.Id;
        m5.Name = 'EPDesigner';
        m5.Metric_Value__c = 0;
        //m3.Metric_Date__c = system.today()-;
        
        m4.add(m5);
        insert m4;
        
        List<Metrics__c> m7= new List<Metrics__c>();
        Metrics__c m8= new Metrics__c();
        m8.Lead__c = l1.Id;
        m8.Name = 'EPDiscovery';
        m8.Metric_Value__c = 1;
        //m3.Metric_Date__c = system.today()-;
        
        m7.add(m8);
        insert m7;

        
        
    Test.startTest();
    MetricProcessor lp= new MetricProcessor();
    Id jobid= Database.executeBatch(lp);
        String sch = '0 0 * * * ?';
 
system.schedule('ReassignAccountOwner', sch, lp);

       
     
    
 
    Test.stopTest();

    }

}