You need to sign in to do that
Don't have an account?
Trailhead- Apex Basics & Database- Getting Started with Apex- Challange Error
Greetings:
I have been trying to do check the challenge of the the first part of the "Apex Basics & Database" module. I have been restructuring my apex code but nothing seems to work. This is the error that I'm getting:

I have been trying to do check the challenge of the the first part of the "Apex Basics & Database" module. I have been restructuring my apex code but nothing seems to work. This is the error that I'm getting:
I agree with you and I generally urge people to post their code and then prefer to guide them for learning but this time I was bit cozy to Francis :)
Thanks for pointing this out!
- Apex Basics & Database:
- Getting Started with Apex Challenge
You are doing a minor mistake here you have decleared the list and line 3 but you are not assiging any value to it and not returning any list of strings as asked by the challenge.
so you need to do follow below steps and then you good to go :)
1. fill the myList with values inside for loop.
2. return mylist after for loop.
Thanks,
Himanshu
Thank you so much for your response! I'm closer, but when I add the values inside the for loop, it becomes an infinite loop error (code set 1)... unless i specifically declare a stop point (as shown in the second set of code)... Almost there. Thank you for your help in getting me this far.
or
yes second one is correct. you need to explictly stop the loop. the basic idea of this challenge to learn how you can call a static method, it doesn't metter how many values you have inside the list.
you are missing return statement after the for loop add that and you are good to go :)
I was feeling confident that this last attempt was going to have it... Now I feel like I'm stuck in a loop.
I added the return statement, and got this error: "Void method must not return a value"
so then I removed the void statement and got the "Contructors cannot be static"...
Sorry, I feel like this should be an easy fix, but I'm just not quite there. I appreciate your time and patience with me.
If you will read challenge details it says that method should return a list of strings but when you will see your method it is not returnining any value
Method can be two types
1. A Method which doesn't return any value
syntax
2. A Method must return a value
syntax
so in this challenge you need to define a return type which is List<String> which makes your method definition as
Makes sense ?
Thanks,
Himanshu
You are a rock star! Thank you so much for your help... however, at this point, I feel like i've got the answer, but it's not validating correctly.
This is what I've got. I'm beginning to go crazy why it's not working. I want you to know I've been trying many many different possibilities to try and make this work, and I'm trying to refrain from posting every single time I have a question. But I do appreciate your help so so much! Thank you!
It was my bad you need to define it as public static String[] generateStringArray
- Create a public class StringArrayTest
- Create a public static method generateStringArray of list<String> type
- It should accept a parameter n of type integer as specified (Mentioned in challenge)
- Create a list and add values using for loop
- return the array or list after the loop
^ This is what you missedThis is the hardest trailhead (for me) and I just can't seem to get it right. I've read through this post in detail but must be missing something basic. This is the last set of code I used which I think is pretty much the same as Umesh. If anyone has any ideas it would be great.
public class StringArrayTest{
public static List<string> generateStringArray(Integer n){
List<String> myList = new List<String>();
for(Integer i=0;i<6;i++){
myList.add('Test '+i);
}
return myList;
}
}
public class StringArrayTest{
public static List<string> generateStringArray(Integer n){
List<String> myList = new List<String>();
for(Integer i=0;i<n;i++){
myList.add('Test '+i);
}
return myList;
}
}
Thank you, Thank you, Thank you - it worked. I've managed 50,000 Trailhead Points and 69 Badges so far and this was the one that had me absoloutly stumped. I really appreciate your help - I'll continue with the module and hopefully can improve my skills along the way.
Best Regards
Mark
Would it be possible to complete this challenge using a standard array, rather than the Salesforce <list>? I thought of using the following:
The problem I see here is that I want to return all the array values (Test[0] to Test[10]) and not just the specific array value (Test[num]).
As a second issue, I'd appreciate any pointers on running the code in the Developer Console. This is my first time using the Developer Console and I wasn't able to find a way to run this code with example values. For example, in NetBeans I can run my Java to see the output. Would that be possible here as well?
This code worked for me.
Thanks friends for your help.
Hi, anyone knows why this is unable to work? :(
public class StringArrayTest {
// Public method which returns a list of string and each string has the data type as int
public static List<String> generateStringArray(Integer n){
//a list
List<String> GSA = new List <String>();
for (Integer i = 0; i<n; i++) {
GSA.add('Test' + i);
}
system.debug('oiadjskl': + GSA);
return GSA;
}
}
Here is my code:
------------------------------------------------------------------------------------------------------------------------
public class StringArrayTest { // Apex Class called StringArrayTest
public static string[] generateStringArray(Integer n){ // Apex Class must have a public static method called 'generateStringArray
List<String> result = new List<String>();
for (Integer i=0; i<n; i++);{
result.add('Test '+i);
}
system.debug(result);
return result;
}
}
-------------------------------------------------------------------------------------------------------------------------
Do you know what is missing here?
I just came across this Trailhead and found if you do not use "10" as the number, it will fail. I tried using 7 and 8, but both failed. I cannot find anywhere in the "Challenge" where it specifies how many records must exist, but "The number of returned strings is specified by an input parameter of an Integer type".
The code that works for me is as follows (which is the same as above, but with different variable name). The only thing I used from the above code was the "10" which seemed to resolve the issue.
public class StringArrayTest {
public static List<string> generateStringArray (integer n){
List<string> moreText = new List<string>();
for(integer i = 0; i < 10; i++){
moreText.add('Test ' + i);
}
system.debug('String: ' +moreText);
return moreText;
}
}
. My code is
public static List<String>generateStringArray(Integer n){
List<String>myArray = new List<String> ();
for(i=0;I<n;i++){
myArray.add('Test' +I);
System.debug(myArray[I]);
}
return myArray;
}
}
public static List<String> generateStringArray(Integer n)
{ List<String> myArray = new List<String>();
for(Integer i=0;i<n;i++)
{
myArray.add('Test '+i);
System.debug(myArray[i]);
}
return myArray;
}
}