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
PaddybPaddyb 

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?

Best Answer chosen by Admin (Salesforce Developers) 
vishal@forcevishal@force

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

Arish_KhanArish_Khan

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.

Navatar_DbSupNavatar_DbSup

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. 

vishal@forcevishal@force

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++)

This was selected as the best answer
PaddybPaddyb

thanks Vishal, I cant believe that I didnt spot that!