You need to sign in to do that
Don't have an account?
Newbie999
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 !
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 !
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? :)
Regarding your original code, there needed to be two changes.
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.