You need to sign in to do that
Don't have an account?
Retreiving string values from a list
Hi,
This is probably a very simple question but I cant seem to find an answer to it. I have a list of type String and I have a loop which assigns the relevent item in the list to the string but I always get an error.
It is looking for a list type variable, an example is below:
List<String> task_names = new List<String>();
for (Integer task_number = 0; i < task_names.size(); i++) {
Task t1 = new task();
t1.Subject = task_names.get[i];
insert(t1);
}
I have tried a few ways of assign the value in the list to the string but it always seems to cause an error. When I used just task_names[i] on its own there was no error when I compliled but there was an array out of bounds exception when I tested it on our sandbox?
Hi,
The exception is caused because of this line :
for (Integer task_number = 0; i < task_names.size(); i++)
{ ..... }
Here in your for loop, you are not defining integer i but you are incrementing it!
So try changing this line to :
for (Integer i = 0; i < task_names.size(); i++)
All Answers
Try this,
List<String> task_names = new List<String>();
List<Task> TaskList = new List<Task>();
if(task_names.size() >0){
for (Integer i=0; i < task_names.size(); i++) {
Task t1 = new task();
t1.Subject = task_names[i].getValue()];
TaskList.add(t1);
}
}
if(!TaskList.isEmpty()){
insert TaskList;
}
If the reply resolves your problem, please mark it as the solution to the post so that others may benefit.
Hi,
Try the below code as reference:
List<String> task_names1 = new List<String>();
for (Task task_names :[select id, subject from task])
{
task_names1.add(task_names.subject);
}
System.debug('@@@@@@@@@@@@@@@' +task_names1);
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Hi,
The exception is caused because of this line :
for (Integer task_number = 0; i < task_names.size(); i++)
{ ..... }
Here in your for loop, you are not defining integer i but you are incrementing it!
So try changing this line to :
for (Integer i = 0; i < task_names.size(); i++)
thanks Vishal, I cant believe that I didnt spot that!