You need to sign in to do that
Don't have an account?

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:
*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?
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:
- Public class OlderAccountsUtility
- public static void updateOlderAccounts() {
- Account [ ] oldAccounts = [SELECT Id, Description FROM Account ORDER BY CreatedDate ASC LIMIT 5];
- for (Account acct : oldAccounts) {
- acct.Description = 'Heritage Account'; }
- update oldAccounts; }
*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?
- for (Account oldAccounts) {
- oldAccounts.Description = 'Heritage Account'; }
You have written code absolutly correct.
yes, you can use list instead of array of account also as below
when you are iterating on colection of record, you should use for each loop and its syntax is as below
Also you can use as below
List Syntax Type 1 is
thx
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: 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:
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': ex:
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];