Skip to content

Instantly share code, notes, and snippets.

@olgaloza
Created April 12, 2018 19:23
Show Gist options
  • Save olgaloza/906a5ff3b5697dffccc4a1e4e584f3c5 to your computer and use it in GitHub Desktop.
Save olgaloza/906a5ff3b5697dffccc4a1e4e584f3c5 to your computer and use it in GitHub Desktop.
Create Test Data for Apex Tests Challenge
//@isTest
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);
}
//insert contactList;
System.debug(contactList.size());
return contactList;
}
}
@filibert666
Copy link

This is nice to give an idea of what to do, but it will not complete the challenge. It needs a random FirstName and a provided LastName.

@quadrimajed
Copy link

tq

@Ronak-Toshniwal
Copy link

//This will work

public class RandomContactFactory{
public static List generateRandomContacts(Integer noOfContacts, String conList){
List ContactList = new List();
for(Integer i = 1; i <= noOfContacts; i++){
Contact con = new Contact(firstName = ''+i, LastName = 'Test');
ContactList.add(con);
}

    return ContactList;
}

}

@baxtery
Copy link

baxtery commented Aug 22, 2022

//correct answer should be
//@istest
public class RandomContactFactory{
//@istest
public static List generateRandomContacts(Integer noOfContacts, String LName){
List ContactList = new List();
for(Integer i = 1; i <= noOfContacts; i++){
Contact con = new Contact(FirstName = 'Test'+i, LastName = LName);
ContactList.add(con);
}

    System.debug(contactList.size());
    return ContactList;
}

}

@pcadoppi
Copy link

The below passes the test:

public class RandomContactFactory{
public static List generateRandomContacts(Integer numCtts, String LastName){
List ctts = new List();
for(Integer i = 0; i <numCtts; i++){
Contact con = new Contact(firstName = 'Test'+i, LastName = 'Test');
ctts.add(con);
}
insert ctts;
return ctts;
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment