+ Start a Discussion
Krista KellyKrista Kelly 
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();

    }

}
Best Answer chosen by Krista Kelly
Andrew GAndrew G
The issue, as is often the case, is building the test data to ensure we get into every branch of the IF ... ELSE ... structure.

First, a tweak to your code, you have a loop where you are inserting the records inside the loop one by one.  This is not good for bulkification, so lets build a list to hold the new records and do the insert outside the loop.
 
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){
        //create a list so we do one insert for all the Metrics records
        List<Metrics__c> newMetrics = new List<Metrics__c>;
        for(Lead l: scope){
            if (l.Console_Last_Logins_Date_Field__c > (System.today() - 1))
            {
                Metrics__c met = new Metrics__c(
                        Lead__c=l.Id,
                        Name='ConsoleLogins',
                        Metric_Value__c=l.Console_Logins__c,
                        Metric_Date__c= System.today());
                newMetrics.add(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());
                newMetrics.add(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());
                newMetrics.add(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());
                newMetrics.add(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());
                newMetrics.add(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());
                newMetrics.add(met) ;
            }
            if (l.EP_Discovery_Date_Field__c > (System.today() - 1))
            {
                //insert record
                Metrics__c met = new Metrics__c(
                        Lead__c=l.Id,
                        Name='EPDesigner',     //should this be 'EPDiscovery'
                        Metric_Value__c=l.EP_Discovery__c,
                        Metric_Date__c= System.today());
                newMetrics.add(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());
                newMetrics.add(met) ;
            }
        }
        insert newMetrics;
        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);
    }

}

Now when we do our test class lets build more than one test so that we can hit all the branches and do some asserts that have some meaning.
 
@IsTest
private class MetricProcessorTest {
    @IsTest
    static void testInsertMetrics(){
        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 = 1;
        l1.EP_Catalog_Last_Modified__c = System.now();
        l1.EP_Catalog__c = 2;
        l1.EP_Designer_Last_Modified__c = System.now();
        l1.EP_Designer__c = 3;
        l1.EP_Discovery_Last_Modified__c = System.now();
        l1.EP_Discovery__c = 4;
        l1.LeadSource='Dreamforce';
        l.add(l1);
        insert l;

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

        System.schedule('ReassignAccountOwner', sch, lp);

        Test.stopTest();

        List<Metrics__c> newMetrics = new List<Metrics__c>();

        newMetrics = [SELECT Id, Name, Metric_Value__c, Metric_Date__c FROM Metrics WHERE Lead__c = :li.Id  ORDER BY Metric__value__c];

        System.assertEquals(4,newMetrics.size());

        System.assertEquals(1,newMetrics[0].Metric_Value__c);
        System.assertEquals('ConsoleLogins',newMetrics[0].Name);
        System.assertEquals(2,newMetrics[1].Metric_Value__c);
        System.assertEquals('EPCatalog',newMetrics[1].Name);
        System.assertEquals(3,newMetrics[2].Metric_Value__c);
        System.assertEquals('EPDesigner',newMetrics[2].Name);
        System.assertEquals(4,newMetrics[3].Metric_Value__c);
        System.assertEquals('EPDesigner',newMetrics[3].Name);

    }

    @IsTest
    static void testInsertZeroMetrics(){
        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()-2;
        l1.Console_Logins__c = 1;
        l1.EP_Catalog_Last_Modified__c = System.now()-2;
        l1.EP_Catalog__c = 2;
        l1.EP_Designer_Last_Modified__c = System.now()-2;
        l1.EP_Designer__c = 3;
        l1.EP_Discovery_Last_Modified__c = System.now()-2;
        l1.EP_Discovery__c = 4;
        l1.LeadSource='Dreamforce';
        l.add(l1);
        insert l;

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

        System.schedule('ReassignAccountOwner', sch, lp);

        Test.stopTest();

        List<Metrics__c> newMetrics = new List<Metrics__c>();

        newMetrics = [SELECT Id, Name, Metric_Value__c, Metric_Date__c FROM Metrics WHERE Lead__c = :li.Id  ORDER BY Name];

        System.assertEquals(4,newMetrics.size());

        System.assertEquals(0,newMetrics[0].Metric_Value__c);
        System.assertEquals('ConsoleLogins',newMetrics[0].Name);
        System.assertEquals(0,newMetrics[1].Metric_Value__c);
        System.assertEquals('EPCatalog',newMetrics[1].Name);
        System.assertEquals(0,newMetrics[2].Metric_Value__c);
        System.assertEquals('EPDesigner',newMetrics[2].Name);
        System.assertEquals(0,newMetrics[3].Metric_Value__c);
        System.assertEquals('EPDiscovery',newMetrics[3].Name);

    }

}

Note that in the first test method we have test data that will fulfill all the IF components of the code.   In the second test , we have data that will drive into each of the ELSE components of the code.


Also note, that when posting code, the code snippet button helps make the code more readable:
User-added image

Regards

Andrew

P.S.  all code is provided uncompiled and As-Is.
 
d3developerd3developer 

Okay folks, I know this is a n00b question but I can't remember if or how to initialize a string with a set of arguments.

 

What I want to do is something like this

 

 

List<String> alphabet = { 'a' , 'b' , 'c' ...  };

 

How can I do this?

 

Best Answer chosen by Admin (Salesforce Developers) 
Jeremy.NottinghJeremy.Nottingh

 

List<String> alphabet = new list<String> { 'a' , 'b' , 'c' ...  };

 That should do ya.

 

jhartfieldjhartfield 

I randomly was trying a split today on a '*' field and got the following error:

 

System.StringException: Invalid regex: Dangling meta character '*' near index 0 * ^

 

Out of curiosity, I tried to split on '+' and got the same error.  

 

This is easily reproducible for me by running the following code in the execute anonymous window - can anyone else  confirm?

string sString = 'someStuff'; string[] splitted = sString.split('*');

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

The following allows the text to be split on the '*' character:

 

string sString = 'some*Stuff'; string[] splitted = sString.split('\\*'); system.debug('Splitted = ' + splitted);

 


SFTerrSFTerr 
Hi, I wrote a trigger and a test class, but when testing I get below error: 

System.DmlException: Update failed. First exception on row 0 with id 003M000000Zc1NaIAJ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, PopulateContactAreaCode: execution of BeforeUpdate
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.PopulateContactAreaCode: line 5, column 1: []

Trigger:
trigger PopulateContactAreaCode on Contact (before insert, before update) {

    for(Contact contact : Trigger.new)
    {
        string AreaCode = CountryRegion__c.getInstance(contact.MailingCountry).Area_Code__c;
        contact.Area_Code__c = AreaCode;
    }
}

test class:
@isTest
public class PopulateContactAreaCodeTest
{
    static testMethod void attTriggerTest1()
    {
        test.startTest();
        Account acct = new Account(id = '001M000000iLhTL', Name = 'Test Account ', Mailing_Country__c = 'Afghanistan');
        update acct;      
        Contact con = new Contact(id = '003M000000Zc1Na', LastName = 'Test Contact', Account = acct, Email = 'test@test.com', Mailing_Country__c = 'Afghanistan', MailingCountry = 'Afghanistan');
        update con;
        delete con;
        test.stopTest();
    }
}

any idea what I added wrong?

thank you in advance
Best Answer chosen by SFTerr
James LoghryJames Loghry

Because you're running a unit test, it doesn't see any data, and therefore no custom settings exist.  You'll need to insert the custom setting before you insert the Contact record.  Here's an example:
 

static testMethod void attTriggerTest1()
    {
        insert new CountryRegion__c(Name='Afghanistan',Area_Code__c='123');

        test.startTest();
        Account acct = new Account(id = '001M000000iLhTL', Name = 'Test Account ', Mailing_Country__c = 'Afghanistan');
        update acct;     
        Contact con = new Contact(id = '003M000000Zc1Na', LastName = 'Test Contact', Account = acct, Email = 'test@test.com', Mailing_Country__c = 'Afghanistan', MailingCountry = 'Afghanistan');
        update con;
        delete con;
        test.stopTest();
    }
Diwakar GDiwakar G 
Hi,

In Setup->Users->Fields->Lanuage only field information is available. How to see the picklist values?
Thanks in advance.
Best Answer chosen by Diwakar G
Sohan Raj GuptaSohan Raj Gupta
Hi Diwakar,

You can manage Language filed values from "Language Settings".

Go to Steup->Language Settings



Hope this will help you. Let me know if it helped or you need any more assistance. 

Please mark this is as the solution if it solved your purpose.

Thanks,
Sohan Raj Gupta 
PRADEEP YADAV 5PRADEEP YADAV 5 
List<Student__c>obj1 = [Select Name,Student_Name__c From Student__c  Where  Student_Name__c Like '_r%' And
                               isDeleted = true All Rows];
        Database.emptyRecycleBin(obj1);
Best Answer chosen by PRADEEP YADAV 5
SwethaSwetha (Salesforce Developers) 
HI Pradeep,

I tried something like this and it initially threw me the same error message you posted when there are no deleted records.

List<Account> a=[Select id,Name From Account Where Name Like '_r%' and isDeleted=true ALL ROWS];
system.debug(a+'===');
Database.emptyRecycleBin(a);
system.debug(a+'===');

However, after deleting a record that satisfied the SOQL condition , and running this same code in the dev console, it ran without error message showing the deleted record in system.debug.

I believe there were no records to be deleted from recyclebin which caused the error.Can you check the recycle bin and confirm?
 
Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful.
 
Thank you
GAURAV SETHGAURAV SETH 
Hi,

I am creating a event object using apex trigger

if(eightAM == 'true') {
                                                      DateTime startDateTime = kstartDate;
                                                    DateTime ednDateTime = kendDate;
                                                        Event eventObject = new Event();
                                                        eventObject.OwnerId = userID;
                                                        eventObject.Location = location; 
                                                        eventObject.StartDateTime = startDateTime.addHours(8);
                                                        eventObject.EndDateTime =startDateTime.addHours(9);
insert eventObject;
}

If it is 8 AM, I am adding 8 hours to create event entry and it is getting inserted but Calendar always show time 4 hours less.

I have check the settings , my time zone and company profile time zone is same.How can I resolve this issue ?

Thanks,
Best Answer chosen by GAURAV SETH
SidPSidP
@GAURAV SETH

Where these (kstartDate, kendDate ) are coming from? I am assuming somewhere you are creating an new instance using Datetime.now()
This method creates the current DateTime as per GMT and not the user's local timezone, that's how you see the difference in hours. You can use the following two ways to return the new instance in the user's local timezone.

1) Datetime.now().format() ==> this returns the  a local time in the String datatype
2) 
Datetime dt = Datetime.now();
Integer offset = UserInfo.getTimezone().getOffset(dt);
Datetime localDT = dt.addSeconds(offset/1000);
System.debug ('local ==> ' + localDT);


You have to use convert kstartDate and kendDate into the local timezone before using them.

Mark this as answer if this resolve your issue! 
Prady01Prady01 
Hi Forum.

     I am working on some development and I am facing few hicups with creating an algorithm for it. Please let me know if anyone has done this.

I have a list of object and this list contains object with duplicate values, I would like to eliminate the duplicates based on the Created date. Example imagine in the list I have two entity like this
Record1==> {obj.Name: Kat, Created Date: 6th july 2015}
Record2==>{obj.Name: Kat, Created Date: 7th july 2015}.
I would like to eliminate Record1 and keep Record2.

Many thanks
Prady01
 
Best Answer chosen by Prady01
Krishna SambarajuKrishna Sambaraju
If this List is created through a custom class you need to implement Comparable interface on that class and then sort the list in descending order of Created Date. Here is an example of implementing Comparable interface.
public class Employee implements Comparable{    
    public string employeeName;
    public date dateOfEmployment;
    public Employee(string name, Date empDate)
    {
        employeeName = name;
        dateOfEmployment = empDate;
    }
    public integer compareTo(object compareTo)
    {
        Employee emp = (Employee)compareTo;
        if (dateOfEmployment <= emp.dateOfEmployment) return 1;
        if (dateOfEmployment > emp.dateOfEmployment) return 0;
        return -1;
    }
}
Here is the example of using the above class to sort the records and eliminate duplicates.
List<Employee> employees = new List<Employee>();
employees.add(new Employee('Kat', Date.today().addDays(-2)));
employees.add(new Employee('Kat', Date.today()));
employees.sort();

Map<string, Employee> empMap = new Map<string, Employee>();
for (Employee emp : employees)
{
    if (empMap.get(emp.employeeName) == null)
    {
    	empMap.put(emp.employeeName, emp);
    }
}
system.debug('empMap : ' + empMap);
Hope this helps.

 
SandrineSandrine 
Hello,

I have a map with Name(String) and Ranks(Integer),
Name is the Pricebook name and Rank is Order by.
map<String,Integer> mapOfNameAndRank = new map<String,Integer>();

Name1, 1
Name2, 2
Name3, 3
I am populating SelectOption in below manner: 
public list<SelectOption> listOfOpt {get;set;}
for(priceBook2 prc : [select id,Name from priceBook2 ){
            listOfOpt.add(new SelectOption(prc.Id,prc.Name));
        }

My usecase is that, the SelectOption should display the Name is the order defined in map.

How can i reiterate the SelectOption inorder to order it.

thank you for suggestion
Best Answer chosen by Sandrine
Maharajan CMaharajan C
Hi Sandrine,

Try like below:
 
map<String,Integer> mapOfNameAndRank = new map<String,Integer>();
Name1, 1
Name2, 2
Name3, 3

public list<SelectOption> listOfOpt {get;set;}

Map<String,Id> pricebookMap = new Map<String,Id>();
for(priceBook2 prc : [select id,Name from priceBook2 ){
    pricebookMap.put(prc.Name , prc.Id);        
    }	
for(String key : mapOfNameAndRank.keySet())
{
	if(pricebookMap.containsKey(key)){
       listOfOpt.add(new SelectOption(pricebookMap.get(key), key));
   }
}

Thanks,
Maharajan.C
James George 00700James George 00700 
Hi Friends,
Is it possible for APEX to refer formula fields for example refering to a roll-up summary field.

Please let me know your answers.

Thanks,
JG
Best Answer chosen by James George 00700
Ram Chand HeerekarRam Chand Heerekar
if you are asking regarding the access then yes we can access Roll-up summary fields in apex code in below mentioned code rc1__Count_of_opp__c is a rollup summary field.

if this answers your question Mark the solution as best
List<account> acc=[select id,rc1__Count_of_opp__c from account where id='0012v000030hjY3AAI'];
system.debug('acc'+acc);

Output:
USER_DEBUG [2]|DEBUG|acc(Account:{Id=0012v000030hjY3AAI, rc1__Count_of_contacts__c=1})