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
mritzmritz 

how to store a field value from list/sObject in a string

Following is my sample code

public list<Account> myAccount;
str='select id,name,Phone from Account where name like \'%'+usr+'%\'+ limit 10;
myAccount=Database.query(str);
private str1;

I want to store 'id' from myAccount in str1.

I tried using str1=String.valuedOf(myAccount.id);
But its throwing error:
Compile Error: Initial term of field expression must be a concrete SObject: List<Account>

Any help will be appreciated.
Best Answer chosen by mritz
sandeep sankhlasandeep sankhla
Hi mritzi,

You can get the id with Index value..

here myacount will contain list of accounts ..you query can return more than one records so in this case to get the first record id you can use somthing like this..

str1 = myAccount [0].Id;

but if this list contains more records and you need all of them ids then you can iterate them in loop and get like below

for(Account objAcc : myAccount )
{
       this below statement will give you Id of all accounts one by one and you can collect them in collection
       //objAcc .Id
}


P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer