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
SalesforceLearnerNewbieSalesforceLearnerNewbie 

Load the Task elements based on Subject and then print the element

Hi,

I wish to load the list inside the Task while at the same time I wish to understand some of the basics. 

Example,

public List<Task> allSubject {get;set;} // why do we use List<Task> and not List<String>?

allSubject = [select Subject from Task];
for (Task a : allSubject){
      //what to use here so that it will take the element inside Object/Dictionary 'Task'?
      // After taking the Task, then I wish to only take the values for 'Subject'
}

System.debug(//here I wish to check the value for 'Subject')

thank you. 
Best Answer chosen by SalesforceLearnerNewbie
GovindarajGovindaraj
Hi,

1. public List<Task> allSubject {get;set;}       // why do we use List<Task> and not List<String>?
                    This is because, In SOQL you're querying the Task object and storing into list, so ofcourse the list should be of type Task or sObject not others.

2. List<Task > lstTask = [SELECT Subject FROM Task];
for(Task a: lstTask) {
         a.Subject = 'Some Value';                  //Using the object(a) created for Task we can access the queried values (like : a.Subject)
         System.debug(' Subject :' +a.Subject);
}

Please let us know if this helps

Thanks,
Govindaraj.S

All Answers

GovindarajGovindaraj
Hi,

1. public List<Task> allSubject {get;set;}       // why do we use List<Task> and not List<String>?
                    This is because, In SOQL you're querying the Task object and storing into list, so ofcourse the list should be of type Task or sObject not others.

2. List<Task > lstTask = [SELECT Subject FROM Task];
for(Task a: lstTask) {
         a.Subject = 'Some Value';                  //Using the object(a) created for Task we can access the queried values (like : a.Subject)
         System.debug(' Subject :' +a.Subject);
}

Please let us know if this helps

Thanks,
Govindaraj.S
This was selected as the best answer
SalesforceLearnerNewbieSalesforceLearnerNewbie
Thank you so much sir. This really helps!