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
Newbie999Newbie999 

i am getting error 'The Apex class was found' while solving trailhead

Hi all,

I am getting an error 'The Apex class was found' while solving trailhead- Create Test Data for Apex Tests.
Below is my code:
public class RandomContactFactory {
    
    public static List<contact> generateRandomContacts(integer i, string s){
        List<contact> list1= new list <contact>();
        for(integer t=0; t< i; i++){
            
            contact c= new contact();
            c.FirstName=s+' '+t;
            c.LastName=s;
    
        }
        list1=[select FirstName from contact where LastName=:s];
        return list1;              
    }
    
}

need help here !
Boss CoffeeBoss Coffee
Could you try adding the annotation @isTest to the test utility class?
@isTest
public class RandomContactFactory {
...etc
If that doesn't work, could you include your test class/methods?
Newbie999Newbie999
Hi @Boss Coffee, the trailhead does not allow use of @isTest or similar annotations. here is the challenge:https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_data
It worked with the below code:
     public class RandomContactFactory {
    public static List<Contact> generateRandomContacts (Integer numOfCon, String ConLastName){
        List<Contact> conList = new List<Contact>();
        for(Integer i=0; i<numOfCon;i++){
            conList.add(new Contact(FirstName='Test ' + i, LastName= string.valueof(ConLastName)));
                   }
        system.debug(conlist);
        return conlist;
    }
}

But, I still did not understand what went wrong earlier. Can you help? :)
Boss CoffeeBoss Coffee
Ah, yes, Trailhead does not give the most helpful error messages sometimes, especially with 'The Apex class was found.'

Regarding your original code, there needed to be two changes.
// ORIGINAL CODE
public class RandomContactFactory {
    public static List<contact> generateRandomContacts(integer i, string s){
        List<contact> list1= new list <contact>();
        for(integer t=0; t< i; i++){
            
            contact c= new contact();
            c.FirstName=s+' '+t;
            c.LastName=s;
            
        }
        list1=[select FirstName from contact where LastName=:s];
        return list1;              
    }
}

1. The Contacts being made in the for loop have not been committed to the database, so the list1 query wlil not return the appropirate records the Trailhead is looking for. As you've done so in your second post, those contacts needed ot be added to the list to pass the Trailhead check.

2. In the original code, the for loop was incrementing the incorrect variable. It was increasing i instead of t.

I made these changes and it passed the Trailhead check.
// UPDATED CODE
public class RandomContactFactory {
    public static List<contact> generateRandomContacts(integer i, string s){
        List<contact> list1= new list <contact>();
        for(integer t=0; t< i; t++){ // i++ is now t++
            
            contact c= new contact();
            c.FirstName=s+' '+t;
            c.LastName=s;
            list1.add(c); // add contacts created to the list
        }
        
        return list1;              
    }
}