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
Chaitra GVChaitra GV 

Executing the 'generateRandomContacts' method failed

I've written the below code which looks fine as per the instructions given in the challenge.
But I'm facing an error when trying to resolve the challenge. Please help.

Error: Executing the 'generateRandomContacts' method failed. Either the method does not exist, is not static, or did not return the correct set of Contact records.

Code:

public class RandomContactFactory {
    
    public static List<Contact> generateRandomContacts(Integer n, String Lastname)
    {
        List<Contact> ct = new List<Contact>();
         Contact c = new Contact();
        for(Integer i=0; i<n; i++)
        {
           c.FirstName = 'Test '+i;
           c.LastName = Lastname;
           ct.add(c);
        }
        
        return ct;
    }

}
Steven NsubugaSteven Nsubuga
public class RandomContactFactory {
    
    public static List<Contact> generateRandomContacts(Integer n, String Lastname)
    {
        List<Contact> ct = new List<Contact>();
         
        for(Integer i=0; i<n; i++)
        {
           Contact c = new Contact();
           c.FirstName = 'Test '+i;
           c.LastName = Lastname;
           ct.add(c);
        }
        return ct;
    }

}

 
himanshu Chaurasia 4himanshu Chaurasia 4
Try this code it will work--




public class RandomContactFactory {
public static List<Contact> generateRandomContacts(Integer numContactsToGenerate, String FName) {
List<Contact> contactList = new List<Contact>();
for(Integer i=0;i<numContactsToGenerate;i++) {
Contact c = new Contact(FirstName=FName + ' ' + i, LastName = 'Contact '+i);
contactList.add(c);
System.debug(c);
}
System.debug(contactList.size());
return contactList;
}
}
Mauricio MontenegroMauricio Montenegro
The challenger does not say FirstName, and FirstName is 
required 

It will work.

public class RandomContactFactory  {             
       public static List <Contact> generateRandomContacts (Integer numAccts, String nombre) {
      // system.debug('paso 1');
        List <Contact> accts = new List <Contact>();
      //     system.debug('paso 2');
     
        for(Integer i=0;i<numAccts;i++) {
        //  system.debug('paso 3');
            Contact a = new Contact(LastName=nombre + ' ' + i,  FirstName =nombre + ' ' + i);
       // system.debug('paso 4');
            accts.add(a);
         // system.debug('paso 5');
        }
    //  system.debug('paso 6');
        insert accts;
      //  system.debug('paso 7');
        return accts;
    }
    
    
      
}