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
Siddhant Singh 5Siddhant Singh 5 

how to retrive list of account names from list of account id

Raj VakatiRaj Vakati
from Set Of Ids

Set<Id> accs =new Set<Id.>();
accs.add('001xxxxxxxxxxx');
accs.add('001xxxxxxxxxxx');


List<Account> acclist = [Select Id from Account where Id in :accs ]; 

from List Of Ids

List<Id> accsLst =new List<Id.>();
accs.add('001xxxxxxxxxxx');
accs.add('001xxxxxxxxxxx');

Set<Id> idsSet = new Set<Id>();
idsSet.addAll(accsLst );
List<Account> acclist = [Select Id from Account where Id in :idsSet]; 
HARSHIL U PARIKHHARSHIL U PARIKH
Let's say you have a list of account Id as this: 
List<Id> actIds; // actIds are filled with account ids.
You can do something like this to retrive account records 
List<Account> acts = [SELECT Id, Name FROM Account WHERE Id =: actIds];

and then, you can retrieve the name in an another list like below,
 
List<String> accountNames = New List<String>();
For(Account EveryAccount : acts){
   accountNames.add(EveryAccount.Name);
}
Hope this helps!


 
Umesh RathiUmesh Rathi
Let's say you have List of Account Id as lstAccountID
Simply write a SOQL query to fetch the account using these IDs.
List<Account> lstAcount = [Select, id,name from Account where id =: lstAccountID];
for(Account acc : lstAcount ){
System.debug(acc.name);
}

Thanks! :)
Hans PeterHans Peter
Thanks :)
Ankit SatnalikaAnkit Satnalika
List<Account> accID = [select id from Account limit 10];
List<Account> accName = [select name from Account where id = :accID];
system.debug(accName);

Hope this helps
Suraj Tripathi 47Suraj Tripathi 47
Hi Siddhant,
"Try this code."
 
public Class FetchAccount{
   public static void fetchName(){
    try{
         List<Account> account1= [SELECT Id,Name FROM Account];
          Set<Id> accountIdSet=new Set<Id>();
          for(Account acc:account1){
           accountIdSet.add(acc.Id);
         }
         List<Account> accName =[SELECT Name FROM Account where Id                   In:accountIdSet];
         for(Account ac:accName){
         system.debug(ac.Name);
          } 

        }catch(Exception e){
System.debug('Exception due to-->: '+e.getMessage());
   }
 }
}

If you find your Solution then mark this as the best answer. 

Thank you!

Regards,
Suraj Tripathi