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
Vamsi DyanaVamsi Dyana 

Apex Beginner Level queries

(Total New for Programming...so pls bear with me..thx)
Q:        Create updateOlderAccounts method in Class OlderAccountsUtlity which gets the first five Account records ordered by the date created. It then updates the description field to say that this is a “heritage account,”
Sol:     
  1. Public class OlderAccountsUtility
  2. public static void updateOlderAccounts() {
  3. Account [ ] oldAccounts = [SELECT Id, Description FROM Account ORDER BY CreatedDate ASC LIMIT 5];
  4. for (Account acct : oldAccounts) {
  5. acct.Description = 'Heritage Account';  }
  6. update oldAccounts;  }
I overall get the functioning and output of the method. But have following doubts
*In Line 3 What is the syntax principle – oldAccounts Sobject of type Account is created?right?
Can’t we write this instead-?  
List<Account> oldAccounts=new List<Account>( SELECT Id, Description FROM Account ORDER BY CreatedDate ASC LIMIT 5);
Or
Account[ ] oldAccounts = new List<Account>( SELECT Id, Description FROM Account ORDER BY CreatedDate ASC LIMIT 5);
*In Line5 why to again create acct variable? Can’t we write like this?
  1. for (Account oldAccounts) {
  2. oldAccounts.Description = 'Heritage Account';  }
(Total New for Programming...so pls bear with me..thx)
 
DebasisDebasis
Hi Vamsi,
You have written code absolutly correct.
yes, you can use list instead of array of account also as below
List<Account> oldAccounts=[ SELECT Id, Description FROM Account ORDER BY CreatedDate ASC LIMIT 5];

when you are iterating on colection of record, you should use for each loop and its syntax is as below 
for(datatype variableName : CollectionofRecords){
// here each record from collection can be referd using variableName
}

Also you can use as below 
Public class OlderAccountsUtility{
    public static void updateOlderAccounts() {
        //Account [ ] oldAccounts = [SELECT Id, Description FROM Account ORDER BY CreatedDate ASC LIMIT 5];
        list<account> accountToUpdate = new list<account>();
        for (Account acct : [SELECT Id, Description FROM Account ORDER BY CreatedDate ASC LIMIT 5]) {
            acct.Description = 'Heritage Account';
            accountToUpdate.add(acct);
        }
        if(accountToUpdate.size()>0){
            update accountToUpdate;
        }
            //update oldAccounts;  
    }
}

 
Vamsi DyanaVamsi Dyana
Hey thx Can you pls clarify further.......
List Syntax Type 1  is 
Account[] oldAccounts = new List<Account>();
and Type 2 is 
List<Account> oldAccounts = new List<Account>();
What is this format ? no new keywords?
List<Account> oldAccounts=[ SELECT Id, Description FROM Account];
and is this also creation of Lists?
Account [ ] oldAccounts = [SELECT Id, Description FROM Account];

thx
 
DebasisDebasis
Hi Vamsi,
Apex has a list collection type that holds an ordered collection of objects. List elements can be accessed with an index or can be sorted if they’re primitive types, such as Integers or Strings. You’ll typically use a list whenever you want to store a set of values that can be accessed with an index. As you’ll see in later tutorials, lists are also used to hold the results of queries.
You can access an element of a list using a zero-based index, or by iterating over the list.  Here's how to create a new list, and display its size:
List<Integer> myList = new List<Integer>();
System.debug(myList.size());
Arrays in Apex are synonymous with lists—Apex provides an array-like syntax for accessing lists. Here is an alternative way to create exactly the same list:
Integer[] myList = new List<Integer>();


You can also define a list variable and initialize it at the same time as shown in the following example, which displays the string 'two':
List<String> myStrings = new List<String> { 'one', 'two' };
ex:
Integer[] myList = new List<Integer>{10, 20};

// Modify the value of the first element
// using the array notation
myList[0] = 15; 
// or using the set method
myList.set(0,15);
System.debug ('Updated value:' + myList[0]);

// Return the size of the list
System.debug ('List size: ' + myList.size());

in a single line, array and list are synonumous and can be interchangeable.
but list can be a multilevel but array cannot.

List<Account> oldAccounts=[ SELECT Id, Description FROM Account];
when you wrote like above, that means if there is any record exists in databse or fteching from soql can be represents as oldAccounts(list varablename).
you need to use new keyword if you want to cretae memory location, here as already records fetching from object you can directly use 
list<sobject> listname=[your soql];