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
Beatriz MinguezBeatriz Minguez 

Create a contact test factory ERROR

Dear all, 
I am trying to solve one of the Trailhead Challenges in the unit: Creating Test Data for Apex Tests and is giving me the following error:

Challenge Not yet complete... here's what's wrong: 
Executing the 'generateRandomContacts' method failed. Either the method does not exist, is not static, or did not return the correct set of Contact records.

The following is the code; 
public class RandomContactFactory {

    public static List<Contact> generateRandomContacts (integer nNumContacts, string sLastName)
    {
        List<Contact> lContactList = new List<Contact>();
        
        for(integer i=0; i<nNumcontacts; i++)
        {
            Contact c = new Contact(LastName= sLastName + ' ' +i);
            lContactList.add(c);

        }    
        return lContactList;
    }
    
}

What I need to solve is the following:

 Create an Apex class that returns a list of contacts based on two incoming parameters: one for the number of contacts to generate, and the other for the last name. The list should NOT be inserted into the system, only returned. The first name should be dynamically generated and should be unique for each contact record in the list.The Apex class must be called 'RandomContactFactory' and be in the public scope.
The Apex class should NOT use the @isTest annotation.
The Apex class must have a public static method called 'generateRandomContacts' (without the @testMethod annotation).
The 'generateRandomContacts' method must accept an integer as the first parameter, and a string as the second. The first parameter controls the number of contacts being generated, the second is the last name of the contacts generated.
The 'generateRandomContacts' method should have a return type of List<Contact>.
The 'generateRandomContacts' method must be capable of consistently generating contacts with unique first names.
For example, the 'generateRandomContacts' might return first names based on iterated number (i.e. 'Test 1','Test 2').
The 'generateRandomContacts' method should not insert the contact records into the database.

Can anyone help me? 

Thanks!

Bea

 
Best Answer chosen by Beatriz Minguez
Clint Majors (Personal)Clint Majors (Personal)
It looks like you are missing the unique First Name when you create your Contacts. You are generating unique Last Names, but are not setting a value for First Name. Maybe something like this would work?

Contact c = new Contact(LastName= sLastName, FirstName = 'Test ' +i);

All Answers

Clint Majors (Personal)Clint Majors (Personal)
It looks like you are missing the unique First Name when you create your Contacts. You are generating unique Last Names, but are not setting a value for First Name. Maybe something like this would work?

Contact c = new Contact(LastName= sLastName, FirstName = 'Test ' +i);
This was selected as the best answer
Oscar Alejandro Garcia BenitezOscar Alejandro Garcia Benitez
public class RandomContactFactory 
{
	public static List<Contact> generateRandomContacts(integer numofContacts,string LastNameGen)
    {
    	List<Contact> con= new List<Contact>();
    	for(Integer i=0;i<numofContacts;i++)
    	{
        	LastNameGen='Test'+ i;
        	Contact a=new Contact(FirstName=LastNameGen,LastName=LastNameGen);
            con.add(a);
        }
        return con;
    }
}
Funciono para mi :D
Abhishek Verma 34Abhishek Verma 34
The apex class:
public class RandomContactFactory {

    public Static List<Contact> generateRandomContacts(Integer NumberOfContact, String LnameOfContact){
        List<Contact> con=new List<Contact>();
        for(Integer i=0;i<NumberOfContact;i++){
            Contact c=new Contact(FirstName='Test '+i,LastName=LnameOfContact);
            con.add(c);
        }
        
        return con;
    }
}

We can test it with:
@isTest
public class RandomContactFactoryTest {
    static testMethod void testfunction(){
    
 List<Contact> Con = RandomContactFactory.generateRandomContacts(1,'Abhishek');
    
    }}
Muhammad AlaaMuhammad Alaa
Try This
public class RandomContactFactory {
    public static List<Contact> generateRandomContacts(Integer iContacts, String sLastName){
        List<Contact> lstRndContacts = new List<Contact>();
        for(Integer i=0; i<iContacts; i++){
            Contact oContact = new Contact(LastName = sLastName, FirstName = 'FN_'+i);
            lstRndContacts.add(oContact);
        }
        return lstRndContacts;
    }
}

 
Pathma JemmyPathma Jemmy
Hi,

The above solution worked out... But I just thought to debug and see the values.
Tried using  like this all the ways possible


    public  List<Contact> generateRandomContacts(Integer iContacts, String sLastName){
        List<Contact> lstRndContacts = new List<Contact>();
        for(Integer i=0; i<10; i++){
            String FN = 'FN_' +i;
            system.debug(FN);
            system.debug('Hello World');
            
            Contact oContact = new Contact(LastName = sLastName, FirstName = FN);
            system.debug(sLastName);
             system.debug(FN);
            lstRndContacts.add(oContact);
            system.debug(oContact);
        }
         system.debug(lstRndContacts);
        return lstRndContacts;
       
    }
But no values is getting printed in log.
Could you pls give me some suggestion?
Olawale EpebinuOlawale Epebinu
Hi,
Below is the code I wrote and I passed the challenge. Since we are inserting the record, there is no need in including the Lastname though required:

public class RandomContactFactory {

    public static List<Contact> generateRandomContacts(Integer numOfContactGenerated, String lastnameGenerated)
        
    {
        
      List<Contact> conts = new List<Contact> ();
        
        for (Integer i = 0; i<Integer numOfContactGenerated;i++ )
        {
            
            conts.add(new Contact(firstname = 'Test'+i) ) ;
            
        } 
        
       return conts; 
        
    }   
    
}
Chetan KapaniaChetan Kapania
Just one correction, under the line 

        for (Integer i = 0; i<Integer numOfContactGenerated;i++ ) remove Integer and this line should be 


    for (Integer i = 0; i< numOfContactGenerated;i++ ) and then this code would work and assist in completing the challenge.
azaz siddiqui 10azaz siddiqui 10
Please try this out.

public class RandomContactFactory {

    public static List<Contact> generateRandomContacts (integer nNumContacts, string sLastName)
    {
        List<Contact> lContactList = new List<Contact>();
        
        for(integer i=0; i<nNumcontacts; i++)
        {
            Contact c = new Contact(LastName= sLastName, FirstName = 'Test '+i);
            lContactList.add(c);

        }    
        return lContactList;
    }
    
}
Abhishek Kumar 407Abhishek Kumar 407
You can try this simple apex class which will help you pass the challenge:-
public class RandomContactFactory 
{
    public static List<Contact> generateRandomContacts(Integer c, String last)
    {
        List<Contact> contacts=new List<Contact>();
        for(Integer i=0;i<c; i++)
        {
            Contact con=new Contact(FirstName='Test '+i, LastName=last);
            contacts.add(con);
        }
        
        return contacts;
    }

}
SandeepSandeep
// can achieve like this as well
// putting this code because new people can see that how many ways it can be written

public class RandomContactFactory {

    public static List<Contact> generateRandomContacts(Integer noOfContacts, String lastName){
        
        List<Contact> conList = new List<Contact>();
        for(Integer i=0; i<noOfContacts; i++){
            Contact c = new Contact(LastName=lastName, FirstName = 'Test ' + i);
            conList.add(c);
           
        }
        return conList;
    }
}

Thanks,
Sandeep.
ASHISH KUMAR MohanASHISH KUMAR Mohan
@Pathma Jenny (https://dfc-org-production.force.com/forums/ForumsProfile?communityId=09aF00000004HMG&userId=005F00000072VWl&showHeader=false) Since you are trying to print a list of an object , you are not able to get any logs. Hence,Try using lstRndContacts.get(i).Name
Ilya IvanovskiyIlya Ivanovskiy
User-added image
public class RandomContactFactory 
{
	public static List<Contact> generateRandomContacts(integer numofContacts,string LastNameGen)
    {
    	List<Contact> con= new List<Contact>();
    	for(Integer i=0;i<numofContacts;i++)
    	{
        	LastNameGen='Test'+ i;
        	Contact a=new Contact(FirstName=LastNameGen,LastName=LastNameGen);
            con.add(a);
        }
        return con;
    }
}

 
DAN LI 10DAN LI 10
Hi Guys,
Below code worked fine:

public class RandomContactFactory {
  public static List<Contact> generateRandomContacts(Integer numContacts, String ContactLastname) {
      List<Contact> cont1=new List<Contact>();
       for(Integer i=0;i<numContacts;i++) {
            Contact a = new Contact(FirstName='First' + i,LastName=ContactLastname);
            Cont1.add(a);
        }   
      Return cont1;
}
}
venkatesh nakkalavenkatesh nakkala
how to test this code in anonymus window? can you please help ? i am new in salesforce development.
Santhosh BovurothuSanthosh Bovurothu
System.debug(RandomContactFactory.generateRandomContacts(1,'Tom'));
 As it is a static method you can call it directly with class name and can create a contact a stated above.
USoni_SFDevUSoni_SFDev
//@isTest//test utility class
public class RandomContactFactory {
    //@isTest
    public static List<Contact> generateRandomContacts(Integer num, String str) {
        List<Contact> conList = new List<Contact>();
        for(Integer i=0;i<num;i++){
            Contact con = new Contact(FirstName = str+' '+i, LastName = str+' '+i);
            conList.add(con);
        }
        return conList;
    }
}