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
NandhuNandhu 

can anybody explain me for loop with better explanation. I am confused with soql query and list

Best Answer chosen by Nandhu
Will OkanumeWill Okanume
So an SQL query can return single or list of record. Using below syntax:
for ( some_variable : [soql query]) {
      Do something e.g create, update or delete a record
}

You'll have to recreate your soql query to return the list of the record you want to user and then iterate your query result using a loop function.
//soql query to return a list of account where Type is "Banking"
List<Account> accs = [SELECT Id FROM Account WHERE Type = 'Banking'];

//This will return a list of sObject Accounts and we'll need to iterate through the list to get and use individual account record in that list

for(Account a : accs){
    a.Name = "Test Account";
};

Hope this helps

All Answers

Andy Kallio 13Andy Kallio 13
Hello. 
You can loop through the results of a soql query. Here is a quick example:
 
list<Account> acts = new list<Account>();
for(Account act : [select Id, Name, Custom_Field__c from Account limit 1]) {
    act.Name = 'New Name';
    acts.add(act);
    system.debug(act);
}

update acts;

 
Deepali KulshresthaDeepali Kulshrestha
Hi Nandhini,

You can create a list with the following Syntax:

List<Object_Name> List_Name = new List<Object_Name>();

Object_Name - replace this with the type of object you want to add to the list

List_Name - this will be your list name which you can use to reference your list anywhere in the program.


Now, to store the data of the query you can do it like this:


Suppose you want to query some data from Account.

List<Account> account_List = new List<Account>();

account_List = [SELECT Id, Name, NumberOfEmployees FROM Account];

This will get you the list of all the accounts in your org with data from Id, Name and NumberOfEmployees field.

Now, if you want to extract the name from this list then you can use for loop.

You can use this type of loop: for(Integer i=0;i<account_List.size();i++) but this will take some time. You can use for each loop like this:

List<String> accName = new List<String>();

for(Account ac : account_List){

accName.add(ac.Name);

}

You'll better understand the syntax by this:


for(ObjectName variablename : your_List_Name){

}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Will OkanumeWill Okanume
So an SQL query can return single or list of record. Using below syntax:
for ( some_variable : [soql query]) {
      Do something e.g create, update or delete a record
}

You'll have to recreate your soql query to return the list of the record you want to user and then iterate your query result using a loop function.
//soql query to return a list of account where Type is "Banking"
List<Account> accs = [SELECT Id FROM Account WHERE Type = 'Banking'];

//This will return a list of sObject Accounts and we'll need to iterate through the list to get and use individual account record in that list

for(Account a : accs){
    a.Name = "Test Account";
};

Hope this helps
This was selected as the best answer