You need to sign in to do that
Don't have an account?

need help in Creating Test Data for Apex Tests
hello,
i am a newbie to salesforce and trying to complete this trailhead challenge and unable to find a way through apex to generate this method for creating new contacts with unique id. as one requirement to pass the challenge is "The 'generateRandomContacts' method must be capable of consistently generating contacts with unique first names."
If somebody successfully completed the task kindly help me.
regards,
i am a newbie to salesforce and trying to complete this trailhead challenge and unable to find a way through apex to generate this method for creating new contacts with unique id. as one requirement to pass the challenge is "The 'generateRandomContacts' method must be capable of consistently generating contacts with unique first names."
If somebody successfully completed the task kindly help me.
regards,
i have completed the task somehow. my friend completed it with a simple code i.e.
public class RandomContactFactory{
public static List<Contact> generateRandomContacts(integer n,string lastname){
integer n1= n;
list<contact> c=[select FirstName from contact limit : n1];
return c;
}
}
but for me it was giving error of class not created as static or either the method is not returning the first name. so for me it worked as follows:
public class RandomContactFactory{
public static List<Contact> generateRandomContacts(integer n,string LastName){
integer n1=n;
List<contact> c1 = new list<contact>();
list<contact> c2 =new list<contact>();
c1 = [select FirstName from Contact Limit : n1];
integer i=0;
for(contact cnew : c1){
contact cnew1 = new contact();
cnew1.firstname = cnew.firstname + i;
c2.add(cnew1);
i++;
}
return c2;
}
}
All Answers
In test class, you can do make use of for loop:
for(Integer i = 0 ; i< 200;i++)
{
lstContact.add(FirstName = 'Test'+i, LastName = 'Contact');
}
insert lstContact;
thanks for the quick response. Can't i do this in any other class than the test class. as the challenge asks not to use test class. and the class should return the list of contacts with incoming parameters something like this.
public static List<Contact> generateRandomContacts(Integer CNumber,string PostalCode)
how do i use the loop for both the parameters?
Thanks in advance.
hey pankaj,
i was doing the same thing and was confused on how to get it right...because ..1) The challenge asks not to insert the contact into salesforce database. 2) the challenge asks for 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. and at the same time the problem i asked in my question in the start that class must be able to genrate first name with unique names consistently. so the first name variable is should be declared as a string type first or what ? because in the code above its going to be an error because FirstName is nor declared anywhere and i can't make a parameter in the method as wel.
sorry for the long text. it has got me confused.
regards,
Please remove the DML statement and otherpostalcode field then. and assign the postal code value to LastName field. As you can see, the FirstName will be having the unique names as we are appending the integer number at the end of the 'Test'. Can you please share the screenshot of the error what you are getting at the time of verifying?
i have completed the task somehow. my friend completed it with a simple code i.e.
public class RandomContactFactory{
public static List<Contact> generateRandomContacts(integer n,string lastname){
integer n1= n;
list<contact> c=[select FirstName from contact limit : n1];
return c;
}
}
but for me it was giving error of class not created as static or either the method is not returning the first name. so for me it worked as follows:
public class RandomContactFactory{
public static List<Contact> generateRandomContacts(integer n,string LastName){
integer n1=n;
List<contact> c1 = new list<contact>();
list<contact> c2 =new list<contact>();
c1 = [select FirstName from Contact Limit : n1];
integer i=0;
for(contact cnew : c1){
contact cnew1 = new contact();
cnew1.firstname = cnew.firstname + i;
c2.add(cnew1);
i++;
}
return c2;
}
}
You can check out my below code. Hope this helps.
public class RandomContactFactory {
public static list<contact> generateRandomContacts(integer n, string m) {
list<contact> con = new list<contact>();
for(integer i=1; i<n+1; i++) {
contact c = new contact(firstname='test'+i,lastname=m);
con.add(c);
}
return con;
}
}
There's a similar challenge on the Developer Beginner trailhead.
You can try this...Its wotking for me You can execute from anonymous window as : RandomContactFactory.generateRandomContacts(2,'Pencil');
I completed the challenge!
M
public class RandomContactFactory {
public static List<Contact> generateRandomContacts(Integer numcts, String lastn){
List<Contact> cts = new List<Contact>();
for(Integer i=0;i<numcts;i++){
Contact c = new Contact(Firstname='Test'+ i);
cts.add(c);
}
return cts;
}
}
public class RandomContactFactory {
public static List<Contact> generateRandomContacts(Integer count, String name) {
List<Contact> contactList = new List<Contact>();
for(Integer index = 1; index <= count; index++) {
Contact c = new Contact();
c.FirstName = name + index;
contactList.add(c);
}
return contactList;
}
}
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;
}
}
public class RandomContactFactory
{
public static list<contact> generateRandomContacts(integer numofContacts, string LName)
{
//this is a list which will hold contacts generated
List<Contact> ListtoReturn = new List<Contact>();
for(integer i=0; i<numofContacts; i++) {
Contact con = new contact(FirstName='Test'+i , LastName=LName);
ListtoReturn.add(con);
}
return ListtoReturn;
}
}
Then i invite you to the Todaypk (https://www.todaypkmovies.in" target="_self)
website for free latest movies download
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);
https://techandskill.com/
public static List<Contact> generateRandomContacts(Integer ConNum, String LName){
List<Contact> Cons = new List<Contact>();
for(integer i=0; i<=ConNum; i++){
Contact C = new Contact(FirstName='TestContact'+i , LastName=LName);
Cons.add(c);
}
Return Cons;
}
}
public class RandomContactFactory{
public static List<Contact> generateRandomContacts(integer digit, string lastNameInput){
List<Contact> contactList = new List<Contact>();
for (integer i=0;i<digit;i++){
contactList.add(new Contact(LastName =lastNameInput, FirstName = 'Test'+ i));
}
return contactList;
}
}
public class RandomContactFactory {
public static List<Contact> generateRandomContacts(Integer numcon , String lastname){
List<Contact> conList = new List<Contact>();
for(Integer i=0;i<numcon;i++){
conList.add(New Contact(FirstName='Test '+i,LastName=lastname));
}
return conList;
}
}
I Completed By Executing the Above Code
https://surveyguide.onl/
Hey Praneeth,
The challenge itself asks us to create an Apex class but not the test class:
"Create an Apex class that returns a list of contacts based on two incoming parameters: the number of contacts to generate and the last name. Do not insert these contact records into the database."
You can however create a test class for this to test your code coverage. I'm attaching a sample test code, which does cover 100% of the code :D
But this code doesn't cover other scenarios like boundary values, negative values, invalid inputs.
Canvas FISD Login Guide at fisd.instructure.com – 2022 (https://topsurvey.onl/canvas-fisd-login/" target="_blank)
In your blog I was happy to see your article, better than last time, and have made great progress, I am very pleased. I am looking forward to your article will become better and better. Cheesecake Factory Happy Hour
hello friends,
Truliant Federal Credit Union Login – Here in this article, you will get to learn about how to login into the Truliant Federal Credit Union Login portal.
So please read this article, at last, and learn about the Truliant Federal Credit Union at www.truliantfcu.com and various other proceedings.
Let’s get started…
http://logina2z.com/truliant-federal-credit-union-login/
hello friends,
Truliant Federal Credit Union Login – Here in this article, you will get to learn about how to login into the Truliant Federal Credit Union Login portal.
So please read this article, at last, and learn about the Truliant Federal Credit Union at www.truliantfcu.com and various other proceedings.
Let’s get started…
https://salonpricelist.com/onewalmart-walmartone/
KFC Menu With Prices (https://kfcsecretmenu.info/kfc-menu-with-prices/) – KFC (Kentucky Fried Chicken) is a fast-food restaurant chain specializing in fried chicken.
hello friends,
My Estub Login – Here in this article, you will get to learn about how to login into the My Estub Login portal.
So please read this article, at last, and learn about the My Estub Login and various other proceedings.
Let’s get started…
http://logina2z.com/my-estub-login/
hello friends,
Us Assure Agent Login Portal – Here in this article, you will get to learn about how to login into the Us Assure Agent Login Portal.
So please read this article, last, and learn about the Us Assure Agent Login and various other proceedings.
Let’s get started…
http://logina2z.com/us-assure-agent-login/
So please read this article, at last, and learn about the Revoobit Login Portal and various other proceedings.
Let’s get started…
http://logina2z.com/how-to-revoobit-login-portal/
So please read this article, at last, and learn about the ALDI Shopping Login and various other proceedings.
Let’s get started...
http://logina2z.com/aldi-shopping-login/