• Nick Mena
  • NEWBIE
  • -2 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 9
    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();

    }

}
I keep getting this error; There was an unexpected error while verifying this challenge. Usually this is due to some pre-existing configuration or code in the challenge Org. We recommend using a new Developer Edition (DE) to check this challenge. If you're using a new DE and seeing this error, please post to the developer forums and reference error id. even if I open it in a new DE. How can I continue?
I want to execute process builder action only when the opporunity owner changed from A to B and using the advance option of specific changes made to  the record. i do not want to execute action for any other change of the record but it is giving me error. Any Suggestions?
User-added image

Hi Community,

 

My question here is when a trigger fires after how many soql quires or DML statemnets it will hit governor limits.

 

Thanks,

Suresh.

Hi, I have a formula field on a custom object that goes to a field 2 levels up, to the the parent objects parent. When I try to save or do check syntax I get the error "Error: {0}"

 

Any ideas? The field works, but I am just trying to change it to lookup to a different field. Tried to create a new formula field and get the same error.

 

Thanks

how to create do customization through visualforce?

I have an issue with a trigger that grabs a date/time value from one field (Field 1)and populates it in another (Field 2). The issue I run into is that if Field 1 is empty I get an error which I am guessing is cause you can insert a null value in a time/date field. Is there a to leave the field blank?

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger Previous_Notes caused an unexpected exception, contact your administrator: Previous_Notes: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Previous_Notes: line 27, column 1

 

trigger Previous_Notes on Event (before insert) {
    

Set<ID> trEvent = New Set<ID>();
Map<ID, Event> mAccEvent = New Map<ID, Event>();

//Get list of WhatIDs in trigger
for(Event e : trigger.new)
    trEvent.add(e.whatID);

List<Event> lEvent = [SELECT id, Event.WhatID,Event.Previous_Completed_Date_Time__c,Event.Completed_Date_Time_-c,Event.Previous_Notes__c,Event.Relevant_Notes__c FROM Event WHERE (whatID IN :trEvent) and (Subject = 'Outside Sales Call') and  (Sales_Call_Completed__c ='Yes' ) AND (Relevant_Notes__c != ' ') ORDER BY Completed_Date_Time__c DESC];

//Populate MAP with WhatIDs to the list of events
For (Event e : lEvent){
      If(mAccEvent.get(e.whatID) == Null ) 
         mAccEvent.put(e.WhatID, e);
}

System.Debug('mAccEvent ' + mAccEvent);

for (Event updatedEvent : trigger.new) {   

System.Debug('updatedEvent.whatID = ' + updatedEvent.whatID);


If(mAccEvent.containsKey(updatedEvent.whatID) == TRUE) updatedEvent.Previous_Notes__c = mAccEvent.get(updatedEvent.whatID).Relevant_Notes__c  ; 
updatedEvent.Previous_Completed_Date_Time__c = mAccEvent.get(updatedEvent.whatID).Completed_Date_Time__c ;
}

//This should get most recent events with whatIDs that are the sames as all the whatIDs in the trigger. Then it will populate a map using those whatIDs. In the last for loop it will update each record in the trigger with the Previous_Notes__c value from the 1 event with a matching whatID. 

}

 

  • November 07, 2011
  • Like
  • 0